Button and text size

I have a problem with the text size in a button. I have a layout where I use weightSum, and the width of my button corresponds to the parent, and the height is 30%. Everything works fine, but when I resize the screen on the emulator, my text is still small. How to change the text size depends on the screen size? This is xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="100" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" android:layout_weight="30" /> </LinearLayout> 

Is there any way to do this automatically?

+2
source share
4 answers

Use sp as a block for button text

 <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button" android:textSize="20sp" android:layout_weight="30" /> 

If this does not solve your problem, create a folder like

1) layout 2) layout-small 3) layout large 4) layout-xlarge

as suggested by Nikhil, but at the same time consider this

Same as the folders suggested above, you can create folders with different values ​​for each type of screen

1) values ​​2) values ​​are small 3) values ​​are large ...

create an xml file with the name diameter.xml in each of them and specify the textSize of your button with a link to the resources of the size below:

 <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button" android:textSize="@dimen/small_font" android:layout_weight="30" /> 

This will set the font size according to the screen size.

Hope this will be helpful for you.

+3
source

if you add a button to the XML:

 <Button android:id="@+id/button1" android:text="click me" android:textSize="22dp" /> 

this should do the trick.

if you want to dynamically change the size of the text, you should get the screen size, and then set textSize using button.setTextSize(size);

+1
source

There is a different layout for Diffrent screens in android, if you want the user interface to view all screens, then you need to make 4 folders for the layout

 1) layout 2) layout-small 3) layout-large 4) layout-xlarge 

for more information u can link to this link:

http://developer.android.com/guide/practices/screens_support.html

+1
source
 <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button" android:textSize="22dp" android:layout_weight="30" /> 
-2
source

Source: https://habr.com/ru/post/915965/


All Articles