f you wish some views to have different layout properties in landscape and portrait when the orientation changes
or if you wish few views to have different layout properties when activity inflating the layout containing the views opens in particular orientation, following are the steps:
if you wish some views to have different layout properties in landscape and portrait when the orientation changes
1.override onConfigurationChanged(Configuration config) method of Activity class.
2. add attribute “android:configChanges=”orientation” in the corresponding activity tag in AndroidManifest.xml
3. Within method onConfigurationChanged(Configuration config)
you can check if the orientation has changed to desired orientation by checking if config.orientation is holding Configuration.ORIENTATION_LANDSCAPE for orientation changed to landscape or Configuration.ORIENTATION_PORTRAIT constant for orientation changed to portrait.
4.We can get the LayoutParams object of a view with getLayoutParams().
5. We can modify the properties of LayoutParams object of the view
For example if you wish a TextView to have layout width to be wrap_contents when orientation changes to portrait and fill_parents when the orientation changes to landscape following is the implementation:
Lets say the reference of the TextView is held in textView reference variable.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
{
LayoutParams layoutParams=textView.getLayoutParams(); //View.getLayoutParams() returns the LayoutParams object of the view
layoutParams.width=LayoutParams.FILL_PARENT; //modify the properties of the LayoutParams
}
else
{
LayoutParams layoutParams=textView.getLayoutParams();
layoutParams.width=LayoutParams.WRAP_CONTENT;
}
if you wish few views to have different layout properties when activity inflating the layout containing the views opens in particular orientation
1. Get Display object as follows:
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
getSystemService() method is a method of Activity class(in fact it has been inherited from Context class)
2. Get the android sdk version because method to get orientation is different from Android 2.2 as follows:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
3. call method according to sdk version to get the screen rotation degree as follows:
int rotation=0; // holds the screen orientation degree .
if (currentapiVersion >= 8)
{
rotation = display.getRotation(); // getRotation() returns the screen orientation degree
}
else
{
rotation =display.getOrientation(); // getOrientation() returns the screen orientation degree
}
4. determine if screen orientation degree pertains to landscape or portrait.
if(rotation==Surface.ROTATION_90||rotation==Surface.ROTATION_270)
{
// orientation is landscape
// write code to modify the layout of a view as done in earlier case
}
else
{
// orientation is portrait
// write code to modify the layout of a view as done in earlier case
}