9

I want to support both small screens and large screens

I have an image view in my layout - that in small screens it needs a layout height of "wrap_content"

and in large screens it needs 400dp (wrap content is too small)

instead of creating another layout, i wanted to create a dimension

<dimen name="layout_height">wrap_content</dimen>
<dimen name="layout_height">400dp</dimen>

and assign them to the right folders and in my imageview write

layout_height=@dimen/layout_height

is this possible in any way, without creating another layout ?

duggu
38.5k12 gold badges121 silver badges114 bronze badges
asked Jun 19, 2014 at 7:34
1
  • in some layouts "wrap_content" is needed Commented Jun 19, 2014 at 8:16

4 Answers 4

20

To assign wrap_content or match_parent from resources you need to create these items in dimens.xml file:

<item name="match_parent" format="integer" type="dimen">-1</item>
<item name="wrap_content" format="integer" type="dimen">-2</item>

Then you can simply use it like this:

<dimen name="layout_height">@dimen/wrap_content</dimen>
answered Dec 9, 2015 at 10:14
Sign up to request clarification or add additional context in comments.

2 Comments

your approach is not working... its giving error as android.view.InflateException: Binary XML file line #57: Can't convert to dimension: type=0x10
i hope you were using this in styles
10

you can have a different style for small and large screen, overriding the layout_height property. Unfortunately you can not set wrap_content inside dimen

answered Jun 19, 2014 at 7:40

1 Comment

there is a way to do it see answer here. stackoverflow.com/a/19027178/2968401
1

APPROACH 1 - Doing it programmatically:

STEP 1. Add the following to the onCreate() of your Activity:

ImageView i = (ImageView)findViewById(R.id.myimageview);
int screenSize = getResources().getConfiguration().screenLayout &
 Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
 i.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,dpi(400)));
 break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
 i.setLayoutParams(new ImageView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
 break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
 i.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
 break;
}

STEP 2. The function dpi() is defined as below:

private int dpi(int i) {
 int value = (int) TypedValue.applyDimension(
 TypedValue.COMPLEX_UNIT_DIP, i,
 getResources().getDisplayMetrics());
 return value;
}

NOTE: Make sure you don't set android:layout_height and android:layout_width for this ImageView in your XML layout.

APPROACH 2 - Doing through XML:

In your /res directory, make two folders layout-small and layout-large. In these two folders, put the same XML layout file and manually set android:layout_width="wrap_content" in the XML file in the layout-small folder and android:layout_height="400dp" in the XML file in the layout-large folder. The existing layout folder will contain the XML layout for normal sized screens.

References:

1. Supporting Multiple Screens

answered Jun 19, 2014 at 7:54

1 Comment

adding needless code in every activity (i have 10) and a bunch of fragments, is not something i want to do...
0

you can use layout_height and minHeight at the time. And for the small screen, set minHeight as 0dp, however, for the big screen, set it as 400dp. Always set layout_height as warp_content.

I think this will work for your case.

answered Feb 18, 2019 at 7:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.