Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Relative layout width and height dynamically

Hi i am using following layout structure inside LinearLayout

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv1"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:paddingLeft="10dp"
            android:textColor="#000" />

        <TextView
            android:id="@+id/tv2"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="right"
            android:paddingRight="10dp"
            android:textColor="#000" />
    </TableRow>
</TableLayout>

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320px"
    android:layout_height="320px"
    android:gravity="center" >
</RelativeLayout>

and want to set relative layout width and height dynamically from java file instead of setting it to 320px in the xml file but not able to do that , Orientation change is not an issue for as i restricting it to only in Portrait mode. It is possible to set the relative layout on full screen by using match_parent but i have to put another views on the screen so is it possible or i have to achieve it another way...

like image 316
Vikas Panwar Avatar asked Mar 15 '13 05:03

Vikas Panwar


People also ask

What is the difference between relative layout and FrameLayout?

RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...


5 Answers

Android does NOT refresh layout of views with wrap_content once it has been displayed. Even with invalidate() or requestLayout(). So if you add a child view, or modify the content dynamically, you're screwed.

getLayoutParams().height = x plus requestLayout() are a good solution if you know the "x", and if you need to do it ONLY ONCE. After that the wrap_content in LayoutParams is lost, since you have overridden it.

To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with wrap_content. The code and instructions to use are available here:

https://github.com/ea167/android-layout-wrap-content-updater

Enjoy!

like image 63
Eric Avatar answered Oct 27 '22 00:10

Eric


try using this

getLayoutParams().height= x;
requestLayout(); or invalidate(); 
like image 28
Anu Avatar answered Oct 27 '22 00:10

Anu


give an id to your relative layout in .xml :

android:id="@+id/relativelayout"

now in your java file write this:

RelativeLayout Rl = (RelativeLayout) findViewById(R.id.relativelayout);

RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedwidth,
                        RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedHeight);

Rl.setLayoutParams(layout_description);
like image 23
KDeogharkar Avatar answered Oct 26 '22 23:10

KDeogharkar


From below code u can get device height and width:-

Display display = getWindowManager().getDefaultDisplay();
    int width = (display.getWidth() );
    int height = (display.getHeight() );

paramsTop ur realative layout:-

Now u can set height or width what you want.

        paramsTop = new RelativeLayout.LayoutParams(width, height);
like image 35
duggu Avatar answered Oct 26 '22 23:10

duggu


You can set views dimensions dynamically using LayoutParams. Something like this:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
like image 28
Prati Avatar answered Oct 27 '22 00:10

Prati