5

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.

asked Sep 28, 2012 at 9:44
1
  • using weight will always modify element size. since you are using a fixed value for text and variable value for button it will never work. i would recommend fixed value for button also. Commented Sep 28, 2012 at 9:46

4 Answers 4

7

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

answered Sep 28, 2012 at 9:50
Sign up to request clarification or add additional context in comments.

1 Comment

Gave me a better understanding of how to handle multiple resolutions.
1

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.

answered Sep 28, 2012 at 9:49

Comments

0

make different xml layout file for tabs and phone and try this for button

android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" 
EstevaoLuis
2,5887 gold badges36 silver badges43 bronze badges
answered Aug 27, 2018 at 15:35

Comments

0

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:)

answered Mar 8, 2022 at 13:15

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.