Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In this above code, we have declare Linear layout as parent and added Vertical Scroll view.
Step 1: Choose your Parent layout. In the example I have choose a LinearLayout with with horizontal orientation. Step 2: Add HorizontalScrollView as a sub-view with layout_width and layout_height as match_parent. You may add padding if you want.
You need to place ScrollView as the first child of Layout file and now put your linearlayout inside it. Now, android will decide on the basis of content and device size available whether to show a scrollable or not. Make sure linearlayout has no sibling because ScrollView can not have more than one child.
Place your layout in a ScrollView.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView ...>
<LinearLayout ...>
...
...
</LinearLayout>
</ScrollView>
<ScrollView 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="match_parent"
android:gravity="center|left"
android:orientation="vertical">
Your views go here...
</LinearLayout>
</ScrollView>
Put your whole content in linear layout which is placed inside ScrollView
.
ScrollView
takes only one layout as its child.
isScrollContainer="true"
This property is used when your softkey in android pops up and still you want your view to scroll.
You can implement it using View.scrollTo(..) also.
postDelayed(new Runnable() {
public void run() {
counter = (int) (counter + 10);
handler.postDelayed(this, 100);
llParent.scrollTo(counter , 0);
}
}
}, 1000L);
you can make any layout scrollable. Just under <?xml version="1.0" encoding="utf-8"?>
add these lines:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
and at the end add </ScrollView>
example of a non-scrollable activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:verticalScrollbarPosition="right"
tools:context="p32929.demo.MainActivity">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:id="@+id/textView"
android:textSize="30sp" />
</RelativeLayout>
After making it scrollable, it becomes like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:verticalScrollbarPosition="right"
tools:context="p32929.demo.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:text="TextView"
android:textSize="30sp" />
</RelativeLayout>
</ScrollView>
Place all your layouts inside a ScrollView with width and height set to fill_parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With