18

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...

Vikas Panwar
  • 399
  • 1
  • 3
  • 14
  • To set the layout width dynamically you will have to extend the RelativeLayout class from a class that you created and override the onMeasure method – the-ginger-geek Mar 15 '13 at 05:12
  • Thanks for reply , I have tried every thing and it's giving class cast exception when i try to set the width and height dynamically. The relative layout also contain other child as image views so i take it as View Group in java file but unable to do that . – Vikas Panwar Mar 15 '13 at 06:05
  • Please post the exception and the code where it points to – the-ginger-geek Mar 15 '13 at 06:10
  • this is the code .. ViewGroup container = (ViewGroup) this.findViewById(R.id.containerr); i am accessing that Relative layout as ViewGroup where containerr is the id of the relative layout in the xml file. – Vikas Panwar Mar 15 '13 at 06:51
  • if i set the layoutparams to container it shows error.. Java.lang.ClassCastException:RelativeLayout&LayoutParams cannot be cast to LinearLayout&LayoutParams – Vikas Panwar Mar 15 '13 at 07:02
  • You are getting a LinearLayout in your xml for the id you are getting according to the Error. Also why are you using ViewGroup and not View? – the-ginger-geek Mar 15 '13 at 07:04
  • Actually i am making an puzzle game and have put 16 pieces of an image into that Relative layout to swap, that's why i am using ViewGroup. I am getting Linear Layout for the id according to the error that may be because of the xml file's parent view is LinearLayout. – Vikas Panwar Mar 15 '13 at 07:18

8 Answers8

26

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!

ahmed hamdy
  • 5,096
  • 1
  • 47
  • 58
Eric
  • 795
  • 9
  • 15
18

try using this

getLayoutParams().height= x;
requestLayout(); or invalidate(); 
Anu
  • 553
  • 5
  • 14
8

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);
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
2

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);
duggu
  • 37,851
  • 12
  • 116
  • 113
  • This is deprecated code. Please refer to the following thread for non-deprecated code: http://stackoverflow.com/questions/4743116/get-screen-width-and-height – Simon Jul 25 '15 at 18:27
2

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;
Prati
  • 283
  • 2
  • 8
2

I faced this problem, the best solution for Change Relative layout width and height dynamically like this

RelativeLayout relMain = (RelativeLayout) convertView.findViewById(R.id.relMain);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.width =200;
 params.height =200;
 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        relMain.setLayoutParams(params);
Binesh Kumar
  • 2,763
  • 1
  • 16
  • 23
1

If you intending to change Height, then this would make a trick.

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
shaiban
  • 331
  • 5
  • 12
0

I encountered same issue when working with LinearLayout which has wrap_content and one child as TextView which had match_parent.

Remove the TextView programatically and then add it again.

linearLayout.removeView(textView)
linearLayout.addView(textView)

I know it sound stupid but it works. In my case calling invalidate didn't work, only this worked.

Depending on your implementation you need to take care of view index inside its parent

Ultimo_m
  • 4,724
  • 4
  • 38
  • 60