5

I have some image that have resolution 320x320.

For the layout_width it set the value fill_parent

but I do not know what should I set for the layout_height

I want to have equal size for height and width.

If I set layout_height=wrap_content the width is twice bigger then the height...

How can I tall some layout to fill all the space in x but to have y size just as the size of x

2
  • Why don't you set layout_width to wrap_content too? and then position the view wherever you want? Commented Jul 2, 2012 at 9:20
  • because that is not what I want to achieve, I want to be the full size for the weight (fill_parent) and to be the same size for the y axis as the x axis Commented Jul 2, 2012 at 13:55

4 Answers 4

2
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int width = metrics.widthPixels; 

now set the height dynamically using width.

this is the width of the device screen, as you are using layout_width = fill parent.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried explicitly setting the layout_height

eg

android:layout_height="320dip" 

1 Comment

yes, that is one trick , but I should support this for any screen size and density
1

If I set layout_height=wrap_content the width is twice bigger then the height...

I think that is because Whenever you set height/width to WRAP_CONTENT it makes the layout as much wide or tall that it can accomodate the Background of the layout, the child views of the layout so it will grow even bigger if you add some more child to the layout

And when set to FILL_PARENT then it takes the whole visible left screen area.

Solutions

  1. Either you can give some fix size using dp to your layout, and then you will have to make different XML file for diff size of devices.
  2. you can try and use the LinearLayout's weightSum and layout_weight attributes and manage the height and width of your layout by appllying proper weight. [I personally recommand to use weight]

Comments

1

From within your activity's onCreate(...) method, immediately after calling setContentView(...);

Use this:

ImageView image = (ImageView)findViewById(R.id.image); image.getLayoutParams().width = image.getHeight(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.