Chinmay Dabke
Chinmay Dabke

Reputation: 5140

Android - Button text size for all screen sizes

I am developing an android app where I have 3 buttons at the top of the screen and i have defined them using the layout_weight property. The buttons adjust to all screen sizes including tablets but its text does not.

This is what it looks like on a nexus 5:

This is what it looks like on a nexus 10:

See the difference?
On the nexus 5, It is quite legible but on the nexus 10 you can hardly see anything!

Here is my XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button 1"
        android:layout_weight="1"
        android:id="@+id/button" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button 2"
        android:layout_weight="1"
        android:id="@+id/button2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button 3"
        android:layout_weight="1"
        android:id="@+id/button3" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:background="#000000"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

So how do I get the button text size to adjust automatically on different screen sizes without having to make different layouts for different screen sizes?

Please help me out with the code as I am pretty much a beginner!

Thanks in advance!

Upvotes: 2

Views: 5098

Answers (2)

fifarunnerr
fifarunnerr

Reputation: 617

You're currently trying to scale up the whole application, instead of building a proper tablet UI. You set the width of the buttons to 1/3th of the screen, which is working as intended. You didn't change the textSize of the buttons, so Android picks the default textSizes. The default textSizes are a specific size, independent of the View's dimensions. You can't (and don't want) to scale up the full UI to be the same on a 4" phone and a 10" tablet.

You can't build a (proper) UI for both phones and tablets in just a single layout, since you should display more information on a tablet than on a phone. Although you can re-use most of the layout. You might want to read the Supporting Different Screen Sizes training on the Android Developers site.

Upvotes: 3

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

You can set button textSize in sp (i.e.: 24sp).

Keeping in mind that this value is referred to an mdpi screen, it will be scaled up or down accordingly to the screen resolution.

So, on an ldpi screen it would be scaled down to 18sp, while on an xhdpi screen it would be scaled up to 48sp.

Other than this you can define a dimension value in your dimens.xml file in your values folder (but you have to make several values folders, one for each resolution you'll support).

Upvotes: 2

Related Questions