Layout container for a view hierarchy that can be scrolled
by the user, allowing it to be larger than the physical display. A
HorizontalScrollView is a FrameLayout,
meaning you should place one child in it containing the entire contents to
scroll; this child may itself be a layout manager with a complex hierarchy of
objects. A child that is often used is a LinearLayout
in a horizontal orientation, presenting a horizontal array of top-level items
that the user can scroll through.
Code - Scrollview
ScrollView is a type of
layout, hold larger data bigger than physical display and make it scrollable. It can be scrolled by the user. Some
rule for scrollview
1.
Scrollview can not hold more than one child. If you have
more than one child then you must place it inside another layout like -Linear
Layout, Relative Layout, Table Layout.
2.
Scrollview is ideal for screens where scrolling is required,
but it is an overhead when scroll view is used to render a larger list of data.
Android provides specialized adapter views like ListView, GridView and Recycler View (Introduced in
Android Lollipop) are recommended for long lists.
3.
You should never use a ScrollView with a ListView or
GridView, because they both takes care of their own vertical scrolling.
4.
TextView also takes care of its own
scrolling, so does not require a ScrollView, but using the two together is
possible to achieve the effect of a text view within a larger container.
5.
It only supports
vertical scrolling not horizontal scrolling.
6.
Use HorizontalScrollView for
horizontal scrolling.
Example
-XML
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button1"
android:padding="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button2"
android:padding="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button3"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button4"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button5"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button6"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button7"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button8"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button9"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button10"
android:padding="30dp"
/>
</LinearLayout>
</HorizontalScrollView>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button1"
android:padding="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button2"
android:padding="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button3"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button4"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button5"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button6"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button7"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button8"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button9"
android:padding="30dp"
/> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="button10"
android:padding="30dp"
/>
</LinearLayout>
</HorizontalScrollView>
***************************************************************************
Programattically ScrollView
XML
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll1" > </HorizontalScrollView>
Java
public class MainActivity extends AppCompatActivity { HorizontalScrollView layout1; HorizontalScrollView.LayoutParams params; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout1=(HorizontalScrollView) findViewById(R.id.ll1); HorizontalScrollView horizontalScrollView=new HorizontalScrollView(this); params=new HorizontalScrollView.LayoutParams(HorizontalScrollView.LayoutParams.WRAP_CONTENT,HorizontalScrollView.LayoutParams.WRAP_CONTENT); Button button=new Button(this); button.setLayoutParams(new HorizontalScrollView.LayoutParams(HorizontalScrollView.LayoutParams.WRAP_CONTENT,HorizontalScrollView.LayoutParams.WRAP_CONTENT)); // button.setId(button); /* button.setWidth(200); button.setHeight(200);*/ button.setText("Program"); layout1.addView(button); } }
ScrollTo and ScrollBy
1. Scroll To - Set the scrolled
position of your view.
2. Scroll By - scroll smoothly instead of immediately.
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello" /> <Button android:id="@+id/hScrollRight" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Scroll Right" /> <Button android:id="@+id/hScrollLeft" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Scroll Left" /> <Button android:id="@+id/hScrolltoStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Scroll To Start" /> <HorizontalScrollView android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#A0A0A0" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="40dp" android:text="Android is a mobile operating system (OS) currently developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets. Android's user interface is mainly based on direct manipulation, using touch gestures that loosely correspond to real-world actions, such as swiping, tapping and pinching, to manipulate on-screen objects, along with a virtual keyboard for text input. In addition to touchscreen devices, Google has further developed Android TV for televisions, Android Auto for cars, and Android Wear for wrist watches, each with a specialized user interface. Variants of Android are also used on notebooks, game consoles, digital cameras, and other electronics. Android has the largest installed base of all operating systems of any kind. Android has been the best selling OS on tablets since 2013, and on smartphones it is dominant by any metric. Initially developed by Android, Inc., which Google bought in 2005,[16] Android was unveiled in 2007, along with the founding of the Open Handset Alliance – a consortium of hardware, software, and telecommunication companies devoted to advancing open standards for mobile devices. [17] As of July 2013, the Google Play store has had over one million Android applications published, and over 50 billion applications downloaded.[18] An April–May 2013 survey of mobile application developers found that 71% of developers create applications for Android,[19] and a 2015 survey found that 40% of full-time professional developers see Android as their priority target platform, which is comparable to Apple's iOS on 37% with both platforms far above others.[20] In September 2015, Android had 1.4 billion monthly active users.[21]" /> </LinearLayout> </HorizontalScrollView> </LinearLayout>
JAVA
public class MainActivity extends AppCompatActivity { HorizontalScrollView layout1; Button buttonScrollRight,buttonScrollLeft,buttonScrollToStart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonScrollRight = (Button)findViewById(R.id.hScrollRight); buttonScrollLeft = (Button)findViewById(R.id.hScrollLeft); buttonScrollToStart = (Button)findViewById(R.id.hScrolltoStart); layout1 = (HorizontalScrollView)findViewById(R.id.myview); buttonScrollRight.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { layout1.scrollBy(+10, 0); }}); buttonScrollLeft.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { layout1.scrollBy(-20, 0); }}); buttonScrollToStart.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { layout1.scrollTo(0, 0); }}); } }
*************************************************************************
fillViewReport
Defines whether the scrollview should stretch its content to fill the viewport.
code.
It's value must be a boolean "true or false ".
android:fillViewport="true/false"
*******************************************************
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroller" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="6dip" android:paddingRight="6dip" android:paddingTop="6dip" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Welcome to My Application" /> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#ff106510" android:layout_marginLeft="6dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_marginBottom="12dip" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:paddingLeft="6dip" android:paddingRight="6dip" android:paddingBottom="6dip" android:text="@string/hello" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/bottom_bar" android:gravity="center_vertical"> <Button android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" android:text="Accept" /> <Button android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" android:text="Refuse" /> </LinearLayout> </LinearLayout> </HorizontalScrollView>
************************************************************************
Fling
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/hello" android:textStyle="bold" /> <TextView android:id="@+id/tvTap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" > </TextView> <TextView android:id="@+id/tvTapEvent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" > </TextView> <HorizontalScrollView android:id="@+id/scroll" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button7" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button8" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button9" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button10" /> </LinearLayout> </HorizontalScrollView> </LinearLayout>
java
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener { private GestureDetector gd; private TextView tvTap; String TAG = getClass().getSimpleName(); private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvTap = (TextView)findViewById(R.id.tvTap); gd = new GestureDetector(this); gd.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() { @Override public boolean onDoubleTap(MotionEvent e) { return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { return false; } }); } @Override public boolean onTouchEvent(MotionEvent event) { return gd.onTouchEvent(event);//return the double tap events } @Override public boolean onDown(MotionEvent e) { return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { if (Math.abs(e1.getX() - e2.getY()) > SWIPE_MAX_OFF_PATH) { return false; } if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { tvTap.setText("Bottom to Top"); Log.v(TAG, "Bottom to Top"); } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { tvTap.setText("Top to Bottom"); Log.v(TAG, "Top to Bottom"); } } catch (Exception e) { } return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } }
Full Scroll ( focus - Up ,Down)
Handles scrolling in response to a
"home/end" shortcut press. This method will scroll the view to the
top or bottom and give the focus to the topmost/bottommost component in the new
visible area. If no component is a good candidate for focus, this scrollview
reclaims the focus.
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Align Start" android:onClick="goHscrollLeft" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Align End" android:onClick="goHscrollRight" /> <HorizontalScrollView android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#A0A0A0" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="40dp" android:text="Android is a mobile operating system (OS) currently developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets. Android's user interface is mainly based on direct manipulation, using touch gestures that loosely correspond to real-world actions, such as swiping, tapping and pinching, to manipulate on-screen objects, along with a virtual keyboard for text input. In addition to touchscreen devices, Google has further developed Android TV for televisions, Android Auto for cars, and Android Wear for wrist watches, each with a specialized user interface. Variants of Android are also used on notebooks, game consoles, digital cameras, and other electronics. Android has the largest installed base of all operating systems of any kind. Android has been the best selling OS on tablets since 2013, and on smartphones it is dominant by any metric. Initially developed by Android, Inc., which Google bought in 2005,[16] Android was unveiled in 2007, along with the founding of the Open Handset Alliance – a consortium of hardware, software, and telecommunication companies devoted to advancing open standards for mobile devices. [17] As of July 2013, the Google Play store has had over one million Android applications published, and over 50 billion applications downloaded.[18] An April–May 2013 survey of mobile application developers found that 71% of developers create applications for Android,[19] and a 2015 survey found that 40% of full-time professional developers see Android as their priority target platform, which is comparable to Apple's iOS on 37% with both platforms far above others.[20] In September 2015, Android had 1.4 billion monthly active users.[21]" /> </LinearLayout> </HorizontalScrollView> </LinearLayout>
Java
public class MainActivity extends AppCompatActivity { HorizontalScrollView hScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hScrollView = (HorizontalScrollView) findViewById(R.id.myview); hScrollView.post(new Runnable() { @Override public void run() { hScrollView.fullScroll(ScrollView.FOCUS_DOWN); } }); } public void goHscrollLeft(View view) { hScrollView.fullScroll(ScrollView.FOCUS_LEFT); } public void goHscrollRight(View view) { hScrollView.fullScroll(ScrollView.FOCUS_RIGHT); } }
22
22
No comments:
Post a Comment