Sunday, 6 January 2013

Switching between Activities and passing extras in Android


First, create  user interface layouts that are needed as follows and your project structure should like this :

Project Structure :




main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="22dp"
            android:layout_marginTop="31dp"
            android:text="UserName"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentRight="true"
            android:ems="10"
            android:inputType="textPersonName" />


        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="38dp"
            android:text="Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView2"
            android:layout_alignLeft="@+id/editText1"
            android:ems="10"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/textView1"
            android:text="Done" />

    </RelativeLayout>


target_layout.xml

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

        <TextView
            android:id="@+id/uname_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentRight="true"
            android:layout_marginRight="56dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="18dp"
            android:text="UserName"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="22dp"
            android:text="EmailId"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/pass_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_toRightOf="@+id/textView1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

After creating and defining layouts, then you define the following activities

HelloAndroidActivity.java


package com.example.helloandroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class HelloAndroidActivity extends Activity implements OnClickListener {

    private EditText mUsername;
    private EditText mPassword;
    private Button mDoneButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mUsername = (EditText) findViewById(R.id.editText1);
        mPassword = (EditText) findViewById(R.id.editText2);
        mDoneButton = (Button) findViewById(R.id.button1);
        mDoneButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.button1) {

            String username = mUsername.getText().toString();
            String pass = mPassword.getText().toString();
           
            Intent intent = new Intent(HelloAndroidActivity.this, TargetActivity.class);      
            intent.putExtra("uname", username);
            intent.putExtra("pass", pass);
            startActivity(intent);

        }

    }

}

TargetActivity.java


package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TargetActivity extends Activity {

    private TextView mNameView;
    private TextView mPassView;
   
    private String uname;
    private String pass;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.target_layout);
   
        uname = getIntent().getExtras().getString("uname");
        pass = getIntent().getExtras().getString("pass");
   
        mNameView = (TextView) findViewById(R.id.uname_view);
        mPassView = (TextView) findViewById(R.id.pass_view);
       
        mNameView.setText(uname);
        mPassView.setText(pass);
    }   
}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="7"  android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
       
        <activity
            android:name=".HelloAndroidActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TargetActivity" />
       
    </application>

</manifest>

Output :







No comments:

Post a Comment