- Firstly we will get the Resources object of application and.
- Then we will get the configuration object that is in effect with resource object .
- The configuration class contains a constants whose values may be one of ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT. using this constant we will get to know what is the current orientation.
Resources res = getResources();
Configuration conf = res.getConfiguration();
int flag = conf.orientation;
Configuration conf = res.getConfiguration();
int flag = conf.orientation;
1. XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text=""
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text=""
android:textSize="25sp" />
</LinearLayout>
2. Activity
package com.pavan.screenorientationdemo;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.textView1);
Resources res = getResources();
Configuration conf = res.getConfiguration();
int flag = conf.orientation;
if (flag == Configuration.ORIENTATION_PORTRAIT) {
display.setText("In PORTRAIT MODE");
} else if (flag == Configuration.ORIENTATION_LANDSCAPE) {
display.setText("In LANDSCAPE MODE");
}
}
}
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.textView1);
Resources res = getResources();
Configuration conf = res.getConfiguration();
int flag = conf.orientation;
if (flag == Configuration.ORIENTATION_PORTRAIT) {
display.setText("In PORTRAIT MODE");
} else if (flag == Configuration.ORIENTATION_LANDSCAPE) {
display.setText("In LANDSCAPE MODE");
}
}
}
1 comment:
One of my whtsapp cntct is showing custom but all others are mobile why is it
Post a Comment