How to detect orientation change and get the current orientation?

Orientation changes are not that good for applications but sometimes this is inevitable and this little method helps us :)
onConfigurationChanged method let us detect orientation changes and we can get current orientation with Configuration.ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT. Just one if-else statement and ta-daaa :) Just put your logic in here and you’re ready to go!

Orientation Changes :

Markdown:
@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("OrientationMyApp", "Current Orientation : Landscape");
     // Your magic here for landscape mode
 } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
     Log.d("OrientationMyApp", "Current Orientation : Portrait");
     // Your magic here for portrait mode
 }
}


Gist:
java
@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("OrientationMyApp", "Current Orientation : Landscape");
     // Your magic here for landscape mode
 } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
     Log.d("OrientationMyApp", "Current Orientation : Portrait");
     // Your magic here for portrait mode
 }
}


Get Orientation Any Time :

public String getScreenOrientation(Context context){
    final int screenOrientation = ((WindowManager)  context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
    switch (screenOrientation) {
        case Surface.ROTATION_0:
        return "SCREEN_ORIENTATION_PORTRAIT";
        case Surface.ROTATION_90:
        return "SCREEN_ORIENTATION_LANDSCAPE";
        case Surface.ROTATION_180:
        return "SCREEN_ORIENTATION_REVERSE_PORTRAIT";
        default:
        return "SCREEN_ORIENTATION_REVERSE_LANDSCAPE";
    }}
fun getScreenOrientation(context: Context): String {
    val screenOrientation = (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay.orientation
    when (screenOrientation) {
        Surface.ROTATION_0 -> return "SCREEN_ORIENTATION_PORTRAIT"
        Surface.ROTATION_90 -> return "SCREEN_ORIENTATION_LANDSCAPE"
        Surface.ROTATION_180 -> return "SCREEN_ORIENTATION_REVERSE_PORTRAIT"
        else -> return "SCREEN_ORIENTATION_REVERSE_LANDSCAPE"
    }
}

Manifest:

<activity
    android:name=".base.activities.MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:label="@string/app_name"/>
Also, we need to add:
android:configChanges=”keyboardHidden|orientation|screenSize”
for the orientation change.

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...