Android - ActionBar doesn't resize with onConfigurationChanged (AppCompat)

In my application manifest, I added android: configChanges to prevent reload / restart activity during rotation

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

it works, but supportActionBar (I use AppCompat) retains its height with a small font size.

The ActionBar should be larger in the portrait and smaller in the landscape, but it retains the initial value:

  • If I start with the landscape, the action bar will remain thin in the portrait.
  • If I start with the portrait, the action bar will remain large in the landscape.

Removing android: configChanges = "orientation | keyboardHidden | screenSize" is the only solution I found, but restarting the application on rotation, and I need to save the contents of the application

Starting with portrait enter image description here

Starting from the landscape enter image description here

Starting from the landscape and turning the screen to the portrait (a small action bar and a small font height) enter image description here

+6
source share
3 answers

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

You declare that you will handle these configuration changes yourself. In normal cases, you should not install this, and Android can recreate your activity.

Edit:

If you want to save the android:configChanges , you need to override onConfigChanged() and change everything you need yourself, for example. size of ActionBar / ToolBar.

+2
source

If you want to save android:configChanges , you can use this to force a 56dp panel height increase , align the icons and fix a small text problem:

XML Toolbar:

  <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" /> 

XML Styles:

 <style name="titleTextAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"> <item name="android:textSize">20sp</item> </style> 
0
source

As others have pointed out, you should save and restore the state of the instance instead of changing the configuration settings if possible. If you have every reason not to do this, you can try updating the height and text of the toolbar after changing the configuration.

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

The code assumes that you have the custom attribute appToolbarStyle declared in attrs.xml . If you do not need that, you can adapt the code to use R.style.Widget_AppCompat_Toolbar directly.

 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(); } 
0
source

All Articles