Scrollview Vertical and Horizontal in Android

How can I make my layout scroll both horizontally and vertically?

I was able to find a simple way to achieve both scrolling behaviors.

Here is the xml for it:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical">

<HorizontalScrollView
android:layout_width="320px" android:layout_height="fill_parent">

<TableLayout
android:id="@+id/linlay" android:layout_width="320px"
android:layout_height="fill_parent" android:stretchColumns="1"
android:background="#000000"/>

</HorizontalScrollView>

</ScrollView>

How to make layout scrollable both horizontally and vertically?

ScrollView and HorizontalScrollView are layout container for a view hierarchy that can be scrolled vertically or horizontally by the user, allowing it to be larger than the physical display. A ScrollView/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.

Here is 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="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Inside 1st HorizontalScrollView" />

<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="Button A1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A3" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A4" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A5" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Inside 2nd HorizontalScrollView" />

<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="Button B1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button B2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button B3" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button B4" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button B5" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button B6" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Inside ScrollView" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button C" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button D" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button E" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button F" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button G" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button H" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button I" />
</LinearLayout>
</ScrollView>
</LinearLayout>

for more detail visit this : http://android-coding.blogspot.in/2011/01/scrollview-and-horizontalscrollview.html

Update :

use this xml to scroll vertical as well as horizontal.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical">

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320px" android:layout_height="fill_parent">



</HorizontalScrollView>

</ScrollView>

Android -- How to allow horizontal and vertical scrolling

Try this

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent">

<TableLayout
android:id="@+id/amortization"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TableRow
android:background="#ffff00">
<TextView
android:text="@string/amortization_1"
android:padding="3dip"/>
<TextView
android:text="@string/amortization_2"
android:padding="3dip"/>
<TextView
android:text="@string/amortization_3"
android:padding="3dip"/>
<TextView
android:text="@string/amortization_4"
android:padding="3dip"/>
<TextView
android:text="@string/amortization_5"
android:padding="3dip"/>
<TextView
android:text="@string/amortization_6"
android:padding="3dip"/>
<TextView
android:text="@string/amortization_7"
android:padding="3dip"/>
</TableRow>
</TableLayout>
</HorizontalScrollView>
</ScrollView>

Center Vertical and Horizontal TextView in ScrollView Android XML

Use android:fillViewport="true" in scrollview

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottomShadow"
android:fillViewport="true"
android:background="@color/red"
android:layout_below="@+id/topShadow">
<LinearLayout
android:id="@+id/quote"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:gravity="center"
android:text="In order to succeed ..."
android:textColor="#e8d8a7"
android:lineSpacingMultiplier="1.5"
android:textStyle="bold"
android:textSize="22sp"/>
</LinearLayout>
</ScrollView>

Android - Horizontal Scrolling Views Inside Vertically Scrolling View

So for your first two views I would recommend you using HorizontalListView, then for other items, if they are always 6 squares in a row, create some simple LinearLayout with horizontal orientation and 6 inner views. As for the whole layout use ordinary ListView.

There are really nothing special in using HorizontalListView inside ListView because they scroll on different axis, however in your adapter you would have to implement getItemViewType(int position) and getViewTypeCount(), because you have two different types of views in your layout, using this methods android knows what kind of views to supply for reuse in your convertView in getView(int position, View convertView, ViewGroup parent).
And using this mechanism - view recycling would work perfectly, because it would create at most 2 HorizontalListView and about 8 other view items.



Related Topics



Leave a reply



Submit