Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different textsize for each screen orientation?

Tags:

java

android

I'm working on a calculator, on landscape I'm adding more buttons so each button gets a bit smaller to fit the extra ones.

At this point I'm just using a small font size so they fit the smaller buttons in landscape mode, however I'd like to have bigger text on portrait than in landscale.

I've been trying to figure out a simple way to use different theme depending on screen orientation but I haven't found an easy way to do it. Creating a different folder for landscape styles /values-land/style.xml doesn't work since the theme is only applied when app is restarted.

How can I use different font sizes/themes depending on screen orientation?

like image 689
lisovaccaro Avatar asked Dec 20 '22 10:12

lisovaccaro


1 Answers

I'd recommend using directories that define font size for both the portrait and landscape orientation. For example:

values/dimen.xml
values-land/dimen.xml

In each of the dimen.xml files, you would have the font size defined for your buttons.

values/dimen.xml:
    <dimen name="button_fontSize">12sp</dimen>

values-land/dimen.xml
    <dimen name="button_fontSize">24sp</dimen>

When an activity is launched or the device is rotated, your xml layout will grab the font size appropriate for the current orientation.

Edit: In response to your use of android:configChanges to capture the calculator display, I'd like to offer an alternative that should be easier to implement and simplify coding.

In the example below, I am assuming your calc screen consists of a TextView named calcDisplay.

protected void onSaveInstanceState(Bundle outState) {
    // Save the calculator display text.
    outState.putString("displayText", calcDisplay.getText());
}

protected void onCreate(Bundle savedInstanceState) {

    // ...

    // Restore the calculator display text.
    if (savedInstanceState != null) {
        String displayText = savedInstanceState.getString("displayText");
        calcDisplay.setText(displayText);
    }
}

Using this code, the device will not only automatically restore the calculator display text on rotation, but since the activity is destroyed and recreated, the font size you want will be loaded as well.

like image 53
happydude Avatar answered Dec 29 '22 00:12

happydude