I was also facing the same issue, then I found a solution to it.
First you have to declare layout folders for your tablet, for example:
layout-sw360dp/ : 720p screens
and layout-sw540dp/ : 1080p screens
Then, in these folder define your layout files. But it won't fix the portrait and landscape issue.
In order to make your app responsive you have to decrale some XML files also.
Put a bool
resource in res.values
to check for screen size.
bool.xml (mobile)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">true</bool>
<bool name="landscape_only">false</bool>
</resources>
// bool.xml (sw600dp)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
<bool name="landscape_only">true</bool>
</resources>
// bool.xml (sw720dp)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
<bool name="landscape_only">true</bool>
</resources>
After adding these resources go to you MainActivity.java
file and above:
setContentView(R.layout.activity_main);
Add this check:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// locking out landscape screen orientation for mobiles
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
// locking out portait screen orientation for tablets
if(getResources().getBoolean(R.bool.landscape_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
setContentView(R.layout.activity_main);
}
For changing the orientation correctly, you should add following to the activity tag in manifest:
<activity
android:name="com.ABC"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
This will do.