I have created a layout for an activity. The layout contains some image buttons and text views. The problem is that when I am setting the orientation of my device to portrai
first you have to create following folder for different layout.....
Folder Name
layout-ldpi
layout-land-ldpi
layout-mdpi
layout-land-mdpi
layout-hdpi
layout-land-hdpi
layout-xlarge
layout-xlarge-land
1...Change in AndroidManifest.xml
android:configChanges="orientation|keyboardHidden"
2....Create layout_land.xml in layout Folder.
3....Create layout_port.xml in layout-land Folder.
4...Create Following Method
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.layout_land);
set_all_id();
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.layout_port);
set_all_id();
}
}
5....Change in OnCreate Method (only this content)
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == 1)
{
setContentView(R.layout.layout_port);
}
if (getResources().getConfiguration().orientation == 2)
{
setContentView(R.layout.layout_land);
}
set_all_id();
}
6...Create set_all_id() method for initialization of view objects
public void set_all_id()
{
//define id for all view objects..
//textview = (textview)findViewById(R.id.textview);
}
- It seems you screen size
is creating a problem here.
- You will have to create another folder under res
directory as layout-land
, and in it create a .xml
file just like you did in layout
folder, and make it look the way you want it in landscape mode
.
- When your app goes into landscape
mode, automatically this .xml
file will be selected (ie from the layout-land) folder.
Due to less height in the landscape
mode, the layout will display like this.
According to me the good way is maintain the layout-land in the res folder and design the layout for the landscape mode. so that it will be nice look