How to prevent orientation changes only for the layout, not for the entire screen / activity

I will need a child layout (there can be any layout, for example FrameLayout or RelativeLayout ), to ignore the orientation change and always stay in landscape orientation. But not his parent or any other layouts / views for the sisters, they should change their orientation accordingly.

Therefore, I cannot use setRequestedOrientation() or android:screenOrientation="landscape" . Or it will block the orientation for the entire screen or activity. I need to block only this single layout.

My XML layout:

 RelativeLayout (Parent) TextView FrameLayout/RelativeLayout <-- this need to be locked FrameLayout Button 
+4
source share
2 answers

This probably depends on what you mean by "unchanging orientation." However, I think that the best place to start would be to create your own class for the part that should not change. So, the xml layout now has two files:

main_layout.xml

 RelativeLayout (Parent) TextView MyNonChangingLayout 

my_non_changing_layout.xml

  RelativeLayout FrameLayout Button 

Where did you create

 MyNonChangingLayout extends FrameLayout { MyNonchangingLayout(Context content) { super(context); myContext = context; makeFromXML(); } private void makeFromXML() { LayoutInflater inflater = (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 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: @override protected void onSizeChanged (int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int viewWidth = //something I've determine int viewHeight = //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 ... private void setViewsize(final View v, final int w, final int h) { post(new Runnable() { public void run() { ViewGroup.LayoutParams lp = v.getLayoutParams(); lp.width = w; lp.height = h; v.setLayoutParams(lp); }}); } 

}

After that, you can effectively use whatever you want. If you can be more specific about what behavior you want to apply in the subregion, I could suggest a more specific code.

One thing you might want to do is save

+1
source

Extend the layout class that you want to retain with portrait orientation, and override the dispatchConfiurationChanged (Configuration) method as follows.

 public void dispatchConfigurationChanged(Configuration newConfig) { Configuration c = new Configuration(newConfig); // copy c.orientation = ORIENTATION_PORTRAIT; // lock to portrait super.dispatchConfigurationChanged(c); } 

Make sure your application is configured to restart orientation changes.

+1
source

All Articles