Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fullscreen layout with additional layouts underneath, which are scrollable

I have a layout displaying some images and text, with buttons at the bottom.

When a button is pressed, I need to display some new information below the buttons.

So the initial content needs to remain at the same height (height of the device screen), and the new content needs to be added beneath it, allowing the user to scroll down.

When a button is pressed it will ideally need to show the new content like a page anchor, but the part I'm having difficulty is getting the initial content to be fullscreened, and maintain that size when new content is added whilst also making the whole thing scroll-able.

I have been playing with different layouts, different height parameters, android:fillViewport="true" or not etc.

I can provide some XML / further explanation if necessary. But I'm not sure whether what I am aiming to achieve is possible or not. At least I'd like to get a scrollable overall view, with the top layout as fullscreen and some layouts underneath which the user can scroll to.

image:

enter image description here

like image 718
Twentyonehundred Avatar asked Dec 31 '25 03:12

Twentyonehundred


1 Answers

Try this:

  1. Make ScrollView container and add your layout #1 into it

  2. Set height of layout #1 into the code according per screen height

  3. After button click add layout #2 into ScrollView

UPDATED: Ok, I can suggest you only this solution (it worked for me in emulator). XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/container" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout 
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff0000">

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

        </RelativeLayout>

    </LinearLayout>

</ScrollView>

Code:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnClick(View v){
        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        ScrollView scroll = (ScrollView)findViewById(R.id.scroll);
        RelativeLayout layout1 = (RelativeLayout)findViewById(R.id.layout1);

        RelativeLayout layout2 = new RelativeLayout(this);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 300);
        layout2.setBackgroundColor(Color.YELLOW);

        layout1.getLayoutParams().height = scroll.getHeight();
        scroll.setFillViewport(false);
        container.addView(layout2, params);
    }

}
like image 103
Dimmerg Avatar answered Jan 02 '26 17:01

Dimmerg