In Activity file :
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
checkOrientation(newConfig);
}
private void checkOrientation(Configuration newConfig){
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d(TAG, "Current Orientation : Landscape");
// Your magic here for landscape mode
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Log.d(TAG, "Current Orientation : Portrait");
// Your magic here for portrait mode
}
}
AND in Manifest file:
<activity android:name=".ActivityName"
android:configChanges="orientation|screenSize">
I hope it'll help you ..!
use this method
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
Toast.makeText(getActivity(),"PORTRAIT",Toast.LENGTH_LONG).show();
//add your code what you want to do when screen on PORTRAIT MODE
}
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Toast.makeText(getActivity(),"LANDSCAPE",Toast.LENGTH_LONG).show();
//add your code what you want to do when screen on LANDSCAPE MODE
}
}
And Do not forget to Add this in your Androidmainfest.xml
android:configChanges="orientation|screenSize"
like this
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:configChanges="orientation|screenSize">
</activity>