I have a layout with a few buttons that takes the space of the entire layout using weight properties.
Each button has some text on it with a fixed size which is set by android:textSize="30dp" so on a screen size of a small mobile device (A phone) the text's size if about the size of the button. But on a large device (A tablet) the button size expands yet the text size remains the same (30dp).
I want the button size and text size ratio to be maintained in every resolution, can it be done using the layout XML code? I would like to avoid doing it programmatically.
The solution is very simple
You should use a dimension.xml with diferent sizes each density/size and place it on:
values-mdpi
Values-hdpi
values-xhdpi
values-large (5.1' 5.4' y nexus7 tablets/phones )
values-xlarge (bigger tablets)
You can use values-sw600dp (nexus 7) and values-sw720dp(10') too.
Ah! And use sp for text, not dp. SP put the same size for all the fonts. Text Allways with SP
Remember, you allways can create values/drawables/resources in general for a lot of separate configurations: Landscape portrati, langauge, sizes, densities, night or day, version of android .... And you can merge it!
http://developer.android.com/intl/es/guide/topics/resources/providing-resources.html#SmallestScreenWidthQualifier
text size should be mentioned in sp, not in dp try like this
android:textSize="30sp"
Size of the text. Recommended dimension type for text is "sp" for scaled-pixels.
make different xml layout file for tabs and phone and try this for button
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Large"
you can fix the size of buttons by giving weight and for the text of a button you can do this to auto size them:
android:autoSizeTextType="uniform"
android:autoSizeMaxTextSize="24sp"
android:autoSizeMinTextSize="20sp"
Set the min and max size what you need and put these codes inside a button body:)
Related
I my Android app I have a TextView with a width of 50% of the screen width, and a height of 10% of the screen height. I've set the textSize to 44sp, but on big devices (screen size) it renders the text too small, and too big on small devices. How can I solve this?
use folder like for dimens.xml file
values
values-sw600dp
values-sw720dp
I suggest you set both width and height of your TextView to wrap_content and use sp for your text size.
Setting your text size to 44sp with wrap_content should work pretty well for most devices.
If you still need to adjust certain devices (densities) you can extract your text size into its own dimen resource and override its value for default configurations, like shown here
app screen on galaxy s4 it skip right and bottom part because of small size mobile
I am having problem in Design different screen size.
how should all 5 categories will support most of the mobile screen?
my Android App Design Normal screen works on 5 inch mobile screen where as for 4 inch screen it skip below content.
for example it work nice on samsang galaxy s4 (5 inch) also on nexus 4.
but as shown in image when i see on nexus S(4 inch screen) it skip some bottom and right part .it cannot hold full layout mostly top and left part it display.
how to design normal screen which support these screen size changes?
is it compulsory to use wrap_content or Match_parent or fill_parent only to get rid of it.
for that also i am confused of specifying image size for different screen size in drawable.
also is normal screen be of different density such as hdpi,mdpi,ldpi etc.
how to solve this issue is it sufficient to use dp and sp?
and last one, in Android Studio we design 5 types of layout.for example activity_channel_list.xml(small). etc( for normal,large,xLarge).what last just activity_channel_list.xml represent.is it like if not any layout is selected.this will be selected by default.
Thanks in advance.
If you want to design layouts for different screens then make folders accordingly link
layout-mdpi
layout-hdpi
layout-xhdpi
layout-xxhdpi
If you don't want create different layout for different screens then simply use dimension file and create different dimens in that like
values-hdpi (dimen)
values-xhdpi (dimen)
values-xxhdpi (dimen)
and the difference between dp and sp is, we use dp for give dimensions to the image, and for size purpose and sp is use for define text size
the screen size id efferent and you cant make a layout for every one you need to try categorize the screen with layout selector
if you use sp for text the text size will change when user change the android font size and it is better to use dp for text
and last Question is like first one you need to categorize layout for size and density normaly
How should all 5 categories will support most of the mobile screen?
Take care if these following aspects while designing layouts:
1.Always design layouts inside scrollview, so that for smaller screens user will be able to view complete layout by scrolling
2.Use sp for text Sizes
3.Use dip(density independent pixels) for all widget sizes, usage of dip makes the size of widget according to mobile profile(like 240*320 may use ldpi so according widget size will change)
4.All the icons you are using you need to generate for all profiles(ldpi,hdpi,xxhdpi,xhdpi) and store in drawable-xxxx folder
If you dont want to generate images for all profiles you can use font icons (like font awesome,iconmoon)
font icons appear according to screen sizes.
5.Last but not least, avoid using hardcoded margins between widgets use attributes like gravity,center_in_parent etc.. so that screen looks uniform in all devices.
I am having a trouble with text size
I am using the sp unit
hdpi and mdpi the font looks great and readable
on x & xxhdpi the font is too small
I tried to put a larger font for the later 2 in values-large , values-xhdpi , values-sw320dp
what happens is either the new sizes is applied to all screens or to none but never to the screens I want
if anyone knows where to put the dimens for this two screen size I'd appreciate it or even another way to handle this font behavior
and thanks in advance
You can create different layouts and put them into different folder.
layout-small
layout-normal
layout-large
layout-xlarge
In that layouts you can put different text sizes according to the screen size.
you can use dp instead of sp .. its the density pixel.. it will keep the ratio same for all the screen..
Or you can add this line to ur textview
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAppearance="?android:attr/textAppearanceSmall"
It will automatically adjust the text size according to the screen size.. depending on the line u added..
I want to set the text size in my app. I have created different layouts (layout, layout-small, layout-normal, layout-large, layout-xlarge) and for values (values, values-ldpi, values-mdpi, values-nodpi, values-hdpi, values-xhdpi). But the problem is some devices in emulator have different sizes but having same dpi. So I am little confuse how to write different text sizes in dimens depending upon screen sizes not on dpi value. And how its work?
I think depending on device size android pick layout and then depending on dpi its choose values. Is it correct or not?
Edit-
I have one activity in my app to show text. I have scroll on text. And I set the values of text size in values-mdpi. But when I run my app on emulator 3.2"QVGA(ADP2)(320 x 480:mdpi) scroll on text work. But when I run my app on emulator 5.1"WVGA(480 x 800:mdpi) all text on half screen. I want size of text big on large screen although they belong mdpi.
Yes it is correct. Depending on the device screen size and specification, android automatically loads the desired layout and all its elements and fonts.
style="#android:style/TextAppearance.DeviceDefault.Medium"
no need to create all layouts, just see this example it is working for me
Note :textviewtwo.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
http://polamreddyn.blogspot.in/2014/01/adjust-text-size-in-all-android-devices.html
I know this has been answered in other questions, but nothing is working for me. I have a text view where I want the text to appear the same size in a variety of density/screen size combinations. I have tried specifying
android:textSize="30dp"
and
android:textSize="30sp"
and have also used
TextView text = (TextView) findViewById(R.id.text);
text.setTextSize(30 * (getResources().getDisplayMetrics().density));
30 is my font size *****
the text doesn't scale properly. It becomes way too small at lower densities and as screen size increases. Are there additional techniques I can use to scale my font?
create layout-large, layout-small, layout-xlarge etc.. versions of your layout xml file.
You can set the size differently in each so that the font will look right across a variety of screen sizes.
After years of constantly having everything slightly off on different devices, I recently discovered that the inches and cm measurements for dimensions are literally that.
So even on devices where 160dp isn't quite an inch, specifying 1inch will give exactly that, on everything, even if they have a custom scaled ROM.
I now specify anything I want very tight control over with these.
If you just want to have separate font sizes for separate screens, you can create dimens in your value.xml for sw600dp and sw720dp folder too and put different values there. You don't need to create separate xml layout for each, if you just need to change font sizes.