How to Check orientation of your device and change on button click into your android studio app

Checking orientation and is most common task when we are working with video files . so here we are checking orientation and changing orientation from vertical to horizontal and vise versa

Step 1: add android:configChanges into your manifest file into activity tag in which you want to perform this task

         <activity
            android:name=".MainActivity"
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" 
           // add  this line in which activity you want to change oriantion
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

             </activity>
          

Step 2: fun_check_oriantation_block_sc() is a on button click function so on click on button this method check and change orientation of your device



        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        int oriantaiton = getResources().getConfiguration().orientation;
        if (oriantaiton == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            Toast.makeText(this, "Landscape oriantaiton", Toast.LENGTH_SHORT).show();
        } else if (oriantaiton == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(this, "Portrait  oriantaiton", Toast.LENGTH_SHORT).show();
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        } else {
            //auto oriantation change
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

            Toast.makeText(this, "Undefind oriantaiton", Toast.LENGTH_SHORT).show();

        }
    }


Thank You Keep Learning Keep Connected #AndroidShortCode

Comments