Friday, June 17, 2011

How to set horizontal and vertical scroll bars

Here is how to make the horizontal and vertical scroll bars appear on the screen WHEN the the application becomes wider  than the phone screen. Scroll bars will appear only when the user needs them; will hide when not needed.


Note: The horizontal scroll bar will appear at the bottom of the screen (not at the bottom of the outer/main view), so the app looks professional.

<?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="@string/heading_title"
    />
   
<ScrollView
    android:id="@+id/layout"
    android:layout_height="fill_parent"    
    android:layout_alignParentBottom="true"    
    android:scrollbars="horizontal|vertical"
    android:layout_width="fill_parent"    
    android:layout_marginTop="5dip"    
    android:scrollbarStyle="outsideInset"
    android:fillViewport="true">

<HorizontalScrollView
    android:id="@+id/horizontalView"
    android:layout_height="fill_parent"    
    android:scrollbars="horizontal|vertical"
    android:layout_width="wrap_content"    
    android:layout_marginTop="5dip">



............Place all your layouts, views, such as linear layouts, table layouts, etc, that are needed for your app. Note that the TextView for heading/title (see above) will be outside the scrollview, so it will always stay visible at the top. Remove this if not needed, or add there more views as needed .............
   


    </HorizontalScrollView>
</ScrollView>

   
   
</LinearLayout>

1 comment:

  1. very informative post...!!!
    check out my blog http://cslearners.blogspot.com/

    ReplyDelete

Please do not hesitate to leave your comments.