[android] Android: How to Programmatically set the size of a Layout

As part of an Android App I am building a button set. The buttons are part of a nested set of LinearLayouts. Using weight I have the set resizing itself automatically based on the size of the containing parent LinearLayout. The idea is, based on the pixel count and density of the screen, to set the size of the containing layout to a number of pixels; and have the button set resize itself based on that change.

The question then is: How to resize the layout.

I have tried several suggested techniques, and none come close to working. Here is a subset of the XML that builds the button set:

    <LinearLayout android:layout_height="104pt" android:id="@+id/numberPadLayout" android:orientation="horizontal" android:layout_width="104pt"
    android:background="#a0a0a0"
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"
>

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="1" android:id="@+id/button1" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="4" android:id="@+id/button4" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="7" android:id="@+id/button7" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="-" android:id="@+id/buttonDash" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="2" android:id="@+id/button2" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="5" android:id="@+id/button5" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="8" android:id="@+id/button8" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="0" android:id="@+id/button0" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout3" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="3" android:id="@+id/button3" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="6" android:id="@+id/button6" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="9" android:id="@+id/button9" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="." android:id="@+id/buttonDot" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout4" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="/" android:id="@+id/buttonBack" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="\" android:id="@+id/buttonEnter" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>

</LinearLayout>    

The two questions are: 1) how do I get access to numberPadLayout from Java. And once I have access to the view, 2) how do I change the height and width of the layout.

Any suggestions will be appreciated.

This question is related to android layout resize android-linearlayout

The answer is


my sample code

wv = (WebView) findViewById(R.id.mywebview);
wv.getLayoutParams().height = LayoutParams.MATCH_PARENT; // LayoutParams: android.view.ViewGroup.LayoutParams
// wv.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
wv.requestLayout();//It is necesary to refresh the screen

You can get the actual height of called layout with this code:

public int getLayoutSize() {
// Get the layout id
    final LinearLayout root = (LinearLayout) findViewById(R.id.mainroot);
    final AtomicInteger layoutHeight = new AtomicInteger();

    root.post(new Runnable() { 
    public void run() { 
        Rect rect = new Rect(); 
        Window win = getWindow();  // Get the Window
                win.getDecorView().getWindowVisibleDisplayFrame(rect);

                // Get the height of Status Bar
                int statusBarHeight = rect.top;

                // Get the height occupied by the decoration contents 
                int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();

                // Calculate titleBarHeight by deducting statusBarHeight from contentViewTop  
                int titleBarHeight = contentViewTop - statusBarHeight; 
                Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop); 

                // By now we got the height of titleBar & statusBar
                // Now lets get the screen size
                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);   
                int screenHeight = metrics.heightPixels;
                int screenWidth = metrics.widthPixels;
                Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);   

                // Now calculate the height that our layout can be set
                // If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also 
                layoutHeight.set(screenHeight - (titleBarHeight + statusBarHeight));
                Log.i("MY", "Layout Height = " + layoutHeight);   

            // Lastly, set the height of the layout       
            FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams)root.getLayoutParams();
            rootParams.height = layoutHeight.get();
            root.setLayoutParams(rootParams);
        }
    });

return layoutHeight.get();
}

LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                       /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
               /*height*/ 100,
               /*weight*/ 1.0f
                );
                YOUR_LinearLayout.setLayoutParams(param);

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to layout

This view is not constrained What's the difference between align-content and align-items? CSS Flex Box Layout: full-width row and columns Fill remaining vertical space with CSS using display:flex What is setContentView(R.layout.main)? How to change line color in EditText Scrolling a flexbox with overflowing content UICollectionView - Horizontal scroll, horizontal layout? How to style a div to be a responsive square? 100% width Twitter Bootstrap 3 template

Examples related to resize

Numpy Resize/Rescale Image AngularJS $watch window resize inside directive Input type "number" won't resize How can I make all images of different height and width the same via CSS? How to get full width in body element Rerender view on browser resize with React How to resize image automatically on browser width resize but keep same height? PHPExcel auto size column width Automatically resize images with browser size using CSS Change form size at runtime in C#

Examples related to android-linearlayout

Android LinearLayout : Add border with shadow around a LinearLayout How to add a fragment to a programmatically generated layout? How to center the content inside a linear layout? How to add (vertical) divider to a horizontal LinearLayout? Fit Image in ImageButton in Android How can I create a border around an Android LinearLayout? Put buttons at bottom of screen with LinearLayout? How to show shadow around the linearlayout in Android? How to add border around linear layout except at the bottom? How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView