I'd like my VIEW layout to be adjusted on orientation change.
My Manifest is set up with: android:configChanges="keyboardHidden|orientation"
on the activity.
I have res/layout-land
, res/layout-port
.
I have onConfigurationChanged
in my Activity
(it get's called on rotation).
In my Activity
I have a ViewFlipper
and inside it I have LinearLayouts
.
When I START the Activity
in port or land mode, the View's layout is correct (according
to my res/layout-????
files).
But when I'm already in the Activity
and I rotate the phone, then my View layout is not changed. The screen is rotated, and the onCreate
of the activity is not called, as it's supposed to be, the onConfigurationChanged
is called in the Activity
, but the screen is always redrawn with the LinearLayout
that was loaded when the Activity
started.
I have a feeling that I miss out something in the onConfigurationChanged
, but don't know what. I guess I need to call some function on the ViewFlipper
or on the LinearLayout
that's inside. What functions should I call?
ViewFlipper flipper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
flipper = new ViewFlipper(getApplicationContext());
setContentView(flipper);
initLayout();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(flipper);
}
void initLayout() {
MyView myview = createMyView(0);
flipper.addView(myview, 0);
myview = createMyView(1);
flipper.addView(myview, 1);
myview = createMyView(1);
flipper.addView(myview, 1);
}
I tried to add flipper = new ViewFlipper(getApplicationContext());
to onConfigurationChanged()
but that made everything black after rotation. I guess I'll have to call initLayout, but that would recalculate everything (and that's exactly what I don't want to do, and that's why I'm trying to do it with onConfigurationChanged
, as opposed to the android default "dropMyActivity & createANewActivity"
SOLUTION: Previously I thought I can trigger the layout to be refreshed on rotation, but it seems I have to re-create the layout with the current context (after the orientation change)
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.category_flipper);
ViewFlipper oldFlipper = viewFlipper;
viewFlipper = (ViewFlipper)findViewById(R.id.categoryFlipper);
for (int i = 0; i < 3; i++) {
final CardLayout cl = new CardLayout(this, cardData[i]);
viewFlipper.addView(cl);
}
cardData[viewIndex].restorePlayer();
viewFlipper.setDisplayedChild(viewIndex);
oldFlipper.removeAllViews();
}
You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.
Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.
When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.
You could lock the activity in one orientation by adding android:screenOrientation="portrait" (or "landscape" ) to <activity> in your manifest. You could tell the system that you meant to handle screen changes for yourself by specifying android:configChanges="orientation|screenSize" in the <activity> tag.
If flipper
is a reference to the view that was initially installed, that's what's causing the problem. You need to pass the resource ID of the layout you want, not the old view from before the phone was rotated. The content view will then be bound to the new resource. Maybe something like this (modulo my guess at your resource identifiers):
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.flipper);
flipper = (ViewFlipper) findViewById(R.id.flipper);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With