How To Prevent Orientation Change For Just A Layout, Not The Whole Screen/activity
I would need a child layout (can be any layout, like FrameLayout or RelativeLayout) to ignore orientation change and remain always in landscape orientation. But not its parent or a
Solution 1:
It probably depends what you mean by "not change orientation". However, I think that best place to start would be to create your own class for the part that shouldn't change. So the layout xml now has two files:
main_layout.xml
RelativeLayout (Parent)
TextView
MyNonChangingLayout
my_non_changing_layout.xml
RelativeLayout
FrameLayout
Button
Where you have created
MyNonChangingLayout extendsFrameLayout {
MyNonchangingLayout(Context content) {
super(context);
myContext = context;
makeFromXML();
}
privatevoidmakeFromXML() {
LayoutInflaterinflater= (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
topView = inflater.inflate(MyR.layout.my_non_changing_layout, this, false);
// Get all the sub Views here using topView.findViewById()// Do any other initiation of the View you need here// Make sure you this otherwise it won't actually appear!super.addView(topView);
}
/*
* Then, you can override quite a lot of the layout's calls and
* enforce behaviour on the children. Two examples:
*/// To specifically catch orientation changes@Overridge
onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// You could create the layout here by removing all views and rebuilding them// Perhaps by having a two xml layouts, one of which is "90 degrees out" ...// If you do make the layot here, make sure you don't clash with the constructor code!switch (newConfig.orientation) {
case ORIENTATION_LANDSCAPE:
// Make the layout for this orientation (as per above)break;
case ORIENTATION_PORTRAIT:
// Make the layout for this orientation (as per above)break;
case ORIENTATION_SQUARE:
// Make the layout for this orientation (as per above)break;
}
}
//to handle size changes to enforce aspect ratios on children:@overrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
intviewWidth=//something I've determineintviewHeight=//something I've determined
setViewsize(viewToHaveSizedControlled, viewWidth, viewheight);
}
// The post thing means that it doesn't crash no matter which thread it is// executed on ...privatevoidsetViewsize(final View v, finalint w, finalint h) {
post(newRunnable() {
publicvoidrun() {
ViewGroup.LayoutParamslp= v.getLayoutParams();
lp.width = w;
lp.height = h;
v.setLayoutParams(lp);
}});
}
}
You can then enforce pretty well anything you want. If you can be more specific about what behaviour you want to enforce on the sub region I might be able to suggest more specific code.
One thing you may be wanting to do is to keep
Solution 2:
Extend the layout class that you want to keep portrait orientation and override the dispatchConfiurationChanged(Configuration) method as follows.
publicvoiddispatchConfigurationChanged(Configuration newConfig) {
Configurationc=newConfiguration(newConfig); // copy
c.orientation = ORIENTATION_PORTRAIT; // lock to portraitsuper.dispatchConfigurationChanged(c);
}
Be sure that your application is configured not to restart on orientation changes.
Post a Comment for "How To Prevent Orientation Change For Just A Layout, Not The Whole Screen/activity"