Monday, April 19, 2021

Screen Orientation

 Android Screen Orientation Example

The screenOrientation is the attribute of activity element. The orientation of android activity can be portrait, landscape, sensor, unspecified etc. You need to define it in the AndroidManifest.xml file.

Syntax:

  1. <activity android:name="package_name.Your_ActivityName"  
  2.       android:screenOrientation="orirntation_type">  
  3. </activity>  

Example:

  1. <activity android:name=" example.tranning.com.screenorientation.MainActivity"  
  2.      android:screenOrientation="portrait">  
  3. </activity>  
  1. <activity android:name=".SecondActivity"  
  2.      android:screenOrientation="landscape">  
  3. </activity>  

The common values for screenOrientation attribute are as follows:

ValueDescription
unspecifiedIt is the default value. In such case, system chooses the orientation.
portraittaller not wider
landscapewider not taller
sensororientation is determined by the device orientation sensor.

Orientation attribute in activity and provides its orientation. In this example, we provide "portrait" orientation for MainActivity and "landscape" for SecondActivity.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="example.tranning.com.screenorientation">  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:roundIcon="@mipmap/ic_launcher_round"  
  10.         android:supportsRtl="true"  
  11.         android:theme="@style/AppTheme">  
  12.         <activity  
  13.             android:name="example.tranning.com.screenorientation.MainActivity"  
  14.             android:screenOrientation="portrait">  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.         <activity android:name=".SecondActivity"  
  22.             android:screenOrientation="landscape">  
  23.         </activity>  
  24.     </application>  
  25.   
  26. </manifest>  

Output:

android screen orientation example output 1 android screen orientation example output 2

Audio/ Sound Project

 ..Mainfest.xml <? xml version ="1.0" encoding ="utf-8" ?> < manifest xmlns: android ="http://schemas.andr...