mike
mike

Reputation: 31

Android Rotation Problem with onConfigurationChanged

I gave android:configChanges="orientation|keyboard" to my activity in manifest and when I rotate my device, onConfigurationChanged is always called. No problem with it.

What I want is when device is rotated, font size of widget become changed.

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/my_font_size" />

Like above, textSize attribute refer the value in resource and the xml file defining my_font_size is in values-land, values-port folders.

Ok. I'm ready. Build it, run it, and rotate the device, and there is no change. requestLayout() in onConfigurationChanged() doesn't work. Any advise about it?

Thanks.

Upvotes: 3

Views: 5607

Answers (3)

Ashterothi
Ashterothi

Reputation: 3282

You can actually specify two different layout files for the two layout types, and configure each with what you want.

Some documentation: Application Resources

If all you want is to make cosmetic design changes based on orientation that is probably the preferred method

Upvotes: 0

inazaruk
inazaruk

Reputation: 74780

You shouldn't use configChanges attribute unless its absolutely necessary (this attribute is introduced to enable some performance-sensitive scenarios). Please carefully read Handling Runtime [Congiuration] Changes.

This technique [using configChanges] should be considered a last resort and is not recommended for most applications.


Having warned you, here is explanation why you do not see changes when rotating the device.

  • If you do not use configChanges="orientation" on your activity, you activity is destroyed and recreated with resources for new orientation. In this case your code in onCreate() will automatically pick values from correct resources (*-land or *-port).

  • If you do use configChanges="orientation" you basically saying to Android to not apply changes automatically, and that you will do this by yourself in onConfigurationChanged().

Assuming you really need to keep configChanges, here is what you can do to fix issue with font size:

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

     int fontSize = getResources().get(R.id.font_size);     
     mTextView.setTextSize(fontSize);
}

But again, you really should not use configChanges in most cases.

Upvotes: 4

aromero
aromero

Reputation: 25761

If you specify android:configChanges="orientation|keyboard" in your activity then Android won't handle this changes for you (meaning that it won't recreate your activity using the proper resource files). That's why you don't get the change in the font size. If you want Android to handle this, then don't add the configChanges to your Activity in the Manifest.

Upvotes: 0

Related Questions