Wednesday 21 November 2012

Intent - values passing One Activity to Another Activity


 

1.IntentDemo1Activity:

package com.venkool;

import android.app.Activity

public class IntentDemo1Activity extends Activity {
      /** Called when the activity is first created. */
      EditText edtUsername ,edtPassword;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button btnLogin = (Button) findViewById(R.id.btnLogin);

            edtUsername = (EditText) findViewById(R.id.edtUserName);
            edtPassword = (EditText) findViewById(R.id.edtPassword);

            btnLogin.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String Username, Password = null;
                       
                        Username = edtUsername.getText().toString();
                        Password = edtPassword.getText().toString();

                        Intent intent = new Intent(IntentDemo1Activity.this,
                                    SecondScreen.class);
                        intent.putExtra("username", Username);
                        intent.putExtra("password", Password);
                        startActivity(intent);
                  }
            });
      }
}


2.SecondScreenActivity:


package com.venkool;

import android.app.Activity

public class SecondScreen extends Activity {
      /** Called when the activity is first created. */
      EditText edtUsername ,edtPassword;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.results);

            Bundle b = getIntent().getExtras();
           
            String UserName =b.getString("username");
            String Password =b.getString("password");
           
            TextView tvUserName = (TextView) findViewById(R.id.tvForUserName);
            TextView tvPassword = (TextView) findViewById(R.id.tvForPassword);
           
            tvUserName.setText(UserName);
            tvPassword.setText(Password);
      }
}


3.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:layout_gravity="center|center_vertical"
    android:orientation="vertical"
    android:background="@color/myColor">

    <TextView
        android:id="@+id/tvUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="UserName" />

    <EditText
        android:id="@+id/edtUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Password" />

    <EditText
        android:id="@+id/edtPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Login" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cancel" />

</LinearLayout>


4.results.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:layout_gravity="center|center_vertical"
    android:background="@color/myColor1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Your UserName and Password"
        android:textSize="15dp" />

    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvForUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/tvForPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

</LinearLayout>

5: Register your All Activities in Android manifest File
Ex:
  <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       
        <activity
            android:label="@string/app_name"
            android:name=".SecondScreen" />
           
        <activity
            android:label="@string/app_name"
            android:name=".IntentDemo1Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

No comments:

Post a Comment