Search code examples
android-layoutandroid-scrollview

What layout should i use for this android UI


i want to make something like this in android

-----------------------------
|                            |
|                            |
|        arrow up here       |
|----------------------------|
|                            |
|     scroll-able with       |
|     components             |
|----------------------------|
|        arrow down here     |
|                            |
|                            |
-----------------------------

the scroll-able in the middle is't possible to use a ScrollView? the arrow up should scroll up the scroll-able in the middle(if the layout for this is really scroll view) and the same in the arrow down but downwards

the background should be fix and the middle scroll should only scroll the components is't possible? what layout should i use? thanks in advance.


Solution

  • This is your layout if you really want to use a scrollview (and not a ListView).

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="up"
            android:text="UP" />
    
        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
                <!-- ADD YOUR COMPONENTS HERE -->
    
            </LinearLayout>
        </ScrollView>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="down"
            android:text="DOWN" />
    
    </LinearLayout>
    

    and this is how to manage "up" and "down" events in your activity. Add these methods:

    public void up(View v) {
        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
        scrollView.scrollBy(0, +20);
    }
    
    public void down(View v) {
        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
        scrollView.scrollBy(0, -20);
    }
    

    In this example I'm just scrolling of 20 pixels. Adjust these values to obtain what you want (for example scroll in the middle of the scrollview, or scroll by the half of the visible scrollview height).