17 July, 2013

Android Orientation

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SimpleOrientationActivity" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Umesh_String"
        android:textSize="50sp"
        android:textColor="#454545"
        android:gravity="top|center_horizontal" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txt1"
        android:layout_below="@+id/txt1"
        android:layout_marginTop="22dp"
        android:layout_marginLeft="35dp"
        android:src="@drawable/ums" />


</RelativeLayout>


SimpleOrientationActivity.java

package com.example.ums_Package;

import com.example.simpleorientationactivity.R;

import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.OrientationEventListener;
import android.widget.Toast;

public class SimpleOrientationActivity extends Activity 
{
OrientationEventListener mOrientationListener;
String DEBUG_TAG = "Debug Report";
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) 
{

@Override
public void onOrientationChanged(int orientation) 
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Orientation change to : " + orientation, Toast.LENGTH_LONG).show();
Log.v(DEBUG_TAG, "Orientation change to : " + orientation);
}
};

if(mOrientationListener.canDetectOrientation() == true)
{
Log.v(DEBUG_TAG, "Can detect Orientation");
mOrientationListener.enable();
}
else
{
Log.v(DEBUG_TAG, "Cannot detect Orientation");
mOrientationListener.disable();
}
}

protected void onDestroy()
{
super.onDestroy();
mOrientationListener.disable();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

Screen-shots
Orientation_Portrait
Orientation Portrait
Orientation_Landscape
Orientation Landscape

No comments:

Post a Comment