Pages

Saturday, August 4, 2012

Manage Landscape Portrait View

Manage Landscape Portrait View

 

Now-a-days mostly android base mobile are come with touch screen so we have to manage both Landscape and Portrait view for application.

 

To Manage Landscape Portrait View in your Application Following step.

 

 1) Create the Folder(Resources) in "res"  folder as following...

2) Create main xml Following Path -res/layout/main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvName"
            style="@style/htextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/etName"
            style="@style/heditText"
            android:layout_width="212dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvClass"
            style="@style/htextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Class:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/etClass"
            style="@style/heditText"
            android:layout_width="220dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvGender"
            style="@style/htextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gender:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <RadioGroup
            android:id="@+id/rgGender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RadioButton
                android:id="@+id/rbMale"
                style="@style/htextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

            <RadioButton
                android:id="@+id/rbFemale"
                style="@style/htextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3" />

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button5" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button6" />
    </LinearLayout>

</LinearLayout>

3) Create main xml Following Path -res/layout-land/main.xml

 

    Copy the res/layout/main.xml in this main.xml

 

4) Create string xml Following Path -res/values/string.xml

 

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

    <string name="hello">Hello World, LandscapPotraitActivity!</string>
    <string name="app_name">LandscapPotrait</string>

    <style name="htextView">
        <item name="android:textSize">30dp</item>
        <item name="android:textColor">#41FCF9</item>
    </style>

    <style name="heditText">
        <item name="android:textSize">30dp</item>
        <item name="android:textColor">#FCFCC9</item>
    </style>

</resources>

5) Create  xml Following Path -res/values-land/string.xml

 

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

    <string name="hello">Hello World, LandscapPotrentActivity!</string>
    <string name="app_name">LandscapPotrent</string>

    <style name="htextView">
        <item name="android:textSize">30dp</item>
        <item name="android:textColor">#E4101B</item>
    </style>

    <style name="heditText">
        <item name="android:textSize">30dp</item>
        <item name="android:textColor">#FCFCC9</item>
    </style>

</resources>

6) LandscapPotraitActivity.java

 

package com.example.LandscapPotrait;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;

public class LandscapPotraitActivity extends Activity {
    /** Called when the activity is first created. */
    EditText etName, etClass;
    RadioButton rbMale, rbFemale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        etName = (EditText) findViewById(R.id.etName);
        etClass = (EditText) findViewById(R.id.etClass);
        rbMale = (RadioButton) findViewById(R.id.rbMale);
        rbFemale = (RadioButton) findViewById(R.id.rbFemale);
        if (savedInstanceState != null) {
            etName.setText(savedInstanceState.getString("name"));
            etClass.setText(savedInstanceState.getString("class"));
            if (savedInstanceState.getBoolean("gender")) {
                rbMale.setChecked(true);
            } else
                rbFemale.setChecked(true);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (etName.getText().equals("")) {
            outState.putString("name", etName.getText().toString());
        }
        if (etClass.getText().equals("")) {
            outState.putString("class", etClass.getText().toString());
        }
        if (rbMale.isChecked()) {
            outState.putBoolean("gender", true);
        } else {
            outState.putBoolean("gender", false);
        }

        super.onSaveInstanceState(outState);

    }
}

Output:-

 

No comments:

Post a Comment

Must Comment