Android scrollable layout

In this Android tip, I am going to show you to make a layout scrollable. When you place many elements in an interface or you have many lines of text in a TextView, some elements or part of the text will be invisible as the size of the screen is not big enough to view all elements or text. This problem can be solved by making the layout scrollable to allow the user to scroll the layout and see the rest elements or text.


To make the layout scrollable, you need to place the layout inside the ScrollView. In the example code below, I place a LinearLayout inside the ScrollView. In the LinearLayout, there are four elements or widgets: one TextView, two RadioButton, and one Button. Before placing the layout in the ScrollView, if TextView has many lines of text, the RadioButton and Button are not visible in phones that have small screen sizes. After placing the layout in the ScrollView, you can scroll the layout to see the rest elements and text.

<?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:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:background="@drawable/backstylelogin"
  
>
<ScrollView
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<LinearLayout
     android:id="@+id/relative1"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     android:background="#ffffff"
    
     >
<TextView
     android:id="@+id/txtdes"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:padding="5dip"
    
     />

<RadioGroup
     android:id="@+id/radioips"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" >

    
     <RadioButton
        android:id="@+id/radiopublicip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_publicip"
        android:checked="true"
         />
        
     <RadioButton
        android:id="@+id/radiolocalip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_localip"
        />


   </RadioGroup>
   <Button
      android:id="@+id/btok"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:onClick="saveConfig"
      android:layout_gravity="center"
      android:text="@string/bt_ok"
      android:background="@drawable/state"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
      android:textColor="#ffffff"
      android:layout_marginTop="30dip"
      />  
  
    </LinearLayout>

    </ScrollView>


</RelativeLayout>


You should note that you can place only one direct child element in the ScrollView. Otherwise, you will get a run-time error. If you have many elements, you have to group the elements in a layout view (e.g. LinearLayout, or RelativeLayout) and then place the layout inside the ScrollView as the example code above.

Android-scrollable-layout

Posted by: Dara | post date: 06-04-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.