Search code examples
androidxmlscrollviewsubviews

ScrollView with Items of ScreenSize


i need a vertical ScrollView which contains 4 Buttons. Each Button should be of the Size of the Phone.

Something like this:

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

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

    <Button
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="First Content."
        android:textSize="50sp"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="Second Content."
        android:textSize="50dip"/>

</LinearLayout>
</ScrollView>

I tried a lot of options but dont get how to manage this.


Solution

  • All you have to do a dynamic layout

    keep a layout in your xml

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

    and then in your activity/fragment java file

    LinearLayout lnLayoutContainer=(LinearLayout)findViewById(R.id.lnr_layout_container);
    

    Calculate height of your device

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int height = displayMetrics.heightPixels;
    

    Now Inflate and add Buttons to your layout container

         for(int i=0;i<4;i++)
                   {
    
                    Button btnView= new Button(context);
                    <set your button properties and listener here>
    
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height);
                    layoutParams.weight = 1;
                    btnView.setLayoutParams(layoutParams);
    
                    lnLayoutContainer.addView(txtViewChar);
                    }