Android - ActionBar not resizing with onConfigurationChanged ( AppCompat )
Asked Answered
C

3

8

In my application manifest I've add android:configChanges to prevent activity reload/restart on rotate

<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:configChanges="orientation|keyboardHidden|screenSize" >

it works, but supportActionBar ( I'm using AppCompat ) preserves his height with small font size.

ActionBar should be bigger in portrait and smaller in landscape, but it keeps the initial value:

  • if I start in landscape, the actionbar stay thin in portrait
  • if I start in portrait, the actionbar stay big in landscape

Removing android:configChanges="orientation|keyboardHidden|screenSize" is the only solution I've found, but the app restart on rotate, and I need to preserve application content

Starting in portrait enter image description here

Starting in landscape enter image description here

Starting in landscape and rotating screen to portrait (small action bar and small font height) enter image description here

Craggy answered 7/9, 2015 at 8:52 Comment(0)
S
4

By setting android:configChanges="orientation|keyboardHidden|screenSize"

You declare that you will handle these config changes by yourself. In normal cases, you should not set that, and let Android recreate your Activity.

Edit:

If you want to keep the line android:configChanges, you have to override onConfigChanged() and change everything needed by yourself, e.g. the size of the ActionBar/ToolBar.

Sprat answered 7/9, 2015 at 8:54 Comment(3)
Yes, but i need to preserve my content.. What should i do?Craggy
What is the content your preserving? You should use onSaveInstanceState()Sprat
developer.android.com/training/basics/activity-lifecycle/… check this linkSprat
O
3

As others have pointed out you should save and restore the instance state instead of handling configuration changes yourself if possible. If you have good reason not to do that you can try to update the toolbar's height and text appearance after the configuration change.

The following code should work for the support library version of Toolbar. The attributes actionBarSize, titleTextAppearance and subtitleTextAppearance are provided by the support library.

The code assumes that you have a custom attribute appToolbarStyle declared in attrs.xml. If you don't need that you can adapt the code to use R.style.Widget_AppCompat_Toolbar directly instead.

import android.support.v7.widget.Toolbar;

...

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_activity);

    toolbar = findViewById(R.id.toolbar);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    updateToolbar();
}

private void updateToolbar() {
    if (toolbar == null)
        return;

    final Context context = toolbar.getContext();

    int[] attr = new int[] { R.attr.actionBarSize, R.attr.appToolbarStyle };
    int idxActionBarSize = 0;
    int idxAppToolbarStyle = 1;
    TypedArray a = context.obtainStyledAttributes(attr);
    int actionBarSize = a.getDimensionPixelSize(idxActionBarSize, 0);
    int appToolbarStyle = a.getResourceId(idxAppToolbarStyle, R.style.Widget_AppCompat_Toolbar);
    a.recycle();

    if (actionBarSize != 0) {
        ViewGroup.LayoutParams layoutParams = toolbar.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.height = actionBarSize;
        }

        toolbar.setMinimumHeight(actionBarSize);
    }

    attr = new int[] { R.attr.titleTextAppearance, R.attr.subtitleTextAppearance };
    int idxTitleTextAppearance = 0;
    int idxSubtitleTextAppearance = 1;
    a = context.obtainStyledAttributes(appToolbarStyle, attr);
    int titleTextAppearance = a.getResourceId(idxTitleTextAppearance, 0);
    int subtitleTextAppearance = a.getResourceId(idxSubtitleTextAppearance, 0);
    a.recycle();

    if (titleTextAppearance != 0) {
        toolbar.setTitleTextAppearance(context, titleTextAppearance);
    }

    if (subtitleTextAppearance != 0) {
        toolbar.setSubtitleTextAppearance(context, subtitleTextAppearance);
    }

    toolbar.requestLayout();
}
Omen answered 10/3, 2018 at 12:36 Comment(0)
P
0

If you want to keep android:configChanges, you can use this to force 56dp toolbar height, align icons and fix small text issue:

Toolbar XML:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:minHeight="56dp"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:titleTextAppearance="@style/titleTextAppearance" />

Styles XML:

<style name="titleTextAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">20sp</item>
</style>
Pug answered 29/1, 2016 at 19:2 Comment(1)
beware, the default for sw600dp should be 64dpMonocyte

© 2022 - 2024 — McMap. All rights reserved.