Android - Screen Orientation Example - ANDROID - Helper

Tuesday, August 16, 2011

Android - Screen Orientation Example

SCREEN ORIENTATION

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
android:layout_width="fill_parent"
                android:layout_height="fill_parent">
               
                <TextView android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="@string/hello" />

                <Button android:text="Set PORTRAIT"
android:id="@+id/portrait"
                                android:layout_width="fill_parent"
android:layout_height="wrap_content">
                </Button>

                <Button android:text="Set LANDSCAPE"
android:id="@+id/landscape"
                                android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>

</LinearLayout>



SOURCE CODE [ScreenOrientationExample.java] is


package com.ScreenOrientationExample;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ScreenOrientationExample extends Activity
{
               
                int o;

                public void onCreate(Bundle savedInstanceState)
{

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                Button buttonSetPortrait = (Button) findViewById(R.id.portrait);
                                Button buttonSetLandscape = (Button) findViewById(R.id.landscape);

                                buttonSetPortrait.setOnClickListener(new Button.OnClickListener()
{
                                                public void onClick(View arg0)
{
                                                                o = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                                                                setRequestedOrientation(o);
                                                }
                                });

                                buttonSetLandscape.setOnClickListener(new Button.OnClickListener()
{
                                                public void onClick(View arg0)
{
                                                                o = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                                                                setRequestedOrientation(o);
                                                }
                                });

                }
}

The OUTPUT will be

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuOZ7bPDsq3cI6c-L4_Fo6YOZsQSivvrh_-JXoKrc1bUE5Y19l1STGzYnIg2SsrWEXZULVdzB8s2iXUsTCGUS-7A3KjaxU70Ho1JE2CRUZCK3x4n-qgVjxjSIzh3laBrpZUU-zFHc77ew/

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqHCFHS1QZ1jsIoEfJZGmJSaPWbWT3zxJEPOmM3BxSHOM0_j-xFIKuhQwyLLBPmCHd4gfz3lRC8BgzlbHl6tZxx4YYp6L0V3AbtSoDPUqzG6XLEG8go_0xjBWMnCmhLFZnkwO7u6e6bv0/

    2 comments: