Friday, December 5, 2014

Android: adjust scrollview's height with dialog view for different size of Device Programatically

The Source Code in xml layout file:
File: dialog_alert_scrollable.xml:

 <LinearLayout
        android:id="@+id/linearLayoutScrollViewContentScrollable"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
        >

    <TextView ...  
        />
    </ScrollView>
    </LinearLayout>


The Source code in java file for Dialog:
ScrollView_Dialog.java

1. Create a Dialog:
final Dialog dialog = new Dialog(context);

        Window window = dialog.getWindow();

        window.requestFeature(Window.FEATURE_NO_TITLE);

        dialog.setContentView(R.layout. dialog_alert_scrollable);

2. Get Width and Height of Screen in order to adjust height programmatically:
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        Display display = wm.getDefaultDisplay();

        Point size = new Point();

        display.getSize(size);

        int width = size.x;

        int screenHeight = size.y;


3. Set up height of Linearlayout programmatically base on Height of Screen:

   LinearLayout linearLayoutScrollView= (LinearLayout) dialog.findViewById(R.id.linearLayoutScrollViewContentScrollable);

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) linearLayoutScrollView.getLayoutParams();

   // here I set up scrollview height as half of screen size, you can set up any height you want     

   lp.height=screenHeight / 2;



You should all set now, Enjoy, Please, use comment below if you have any question or concern !



No comments:

Post a Comment