Monday 5 September 2011

Passing Values from activity to another activity using Intent

Passing Values from activity to another activity using Intent: 

User Interface:

We have a login screen with UserName label(TextView) ,Password label(TextView) ,UserName (EditView),Password (EditView) to enter values and a login Button.    
          
main.xml

<?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">
 <LinearLayout android:id="@+id/LinearLayout01"
  android:layout_height="wrap_content" android:layout_width="fill_parent">
  <TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:id="@+id/UserNameText"
   android:text="UserName" android:typeface="sans" android:textStyle="bold">
</TextView>

  <EditText android:layout_height="wrap_content" android:hint="User Name"
   android:layout_width="fill_parent" android:id="@+id/username"></EditText>
 </LinearLayout>
 <LinearLayout android:id="@+id/LinearLayout02"
  android:layout_height="wrap_content" android:layout_width="fill_parent">
  <TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:id="@+id/PasswordText"
   android:text="Password" android:textStyle="bold"></TextView>
  <EditText android:layout_height="wrap_content" android:hint="Password"
   android:singleLine="true" android:layout_width="fill_parent"
   android:id="@+id/password"></EditText>
 </LinearLayout>
 <Button android:layout_height="wrap_content" android:id="@+id/login"
  android:text="Login" android:layout_gravity="center"
  android:layout_width="100px"></Button>
</LinearLayout>

Activity:
               On Clicking on login button, displays an dialog with username

LoginActivity.java
package com.login;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
    /** Called when the activity is first created. */
    // Retrieve username and password using id.
    EditText userName = null;
    EditText password = null;
    // Button
    Button loginButton = null;
    Context context = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        userName = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);
        loginButton = (Button)findViewById(R.id.login);
        loginButton.setOnClickListener(new Button.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {                       
                if(password.getText()!= null && userName.getText()!= null && password.getText().length()==0 && userName.getText().length()==0)
                {
                    showDialog();       
                }
                else
                {
                    String uName = userName.getText().toString();
                    Intent intent = new Intent(LoginActivity.this,DisplayActivity.class);
                    intent.putExtra("UserName", uName);
                    startActivity(intent);
                }
            }
        });
    }
    /**
     * On clicking login button, dialog is displayed with userName
     */
    private void showDialog()
    {
        Builder alert = new AlertDialog.Builder(this);
        alert.setMessage("Enter UserName and Password");
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {

            }
        });
        alert.show();       

    }
}
DisplayActivity.java
package com.login;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
/**
 *
 * @author rathika.r
 *
 */
public class DisplayActivity  extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data);
        TextView uName = (TextView)findViewById(R.id.TextView01);
        Bundle data = getIntent().getExtras();
        if(data != null && data.containsKey("UserName"))
        {
            uName.setText("Welcome "+data.getCharSequence("UserName")+"!!!");
        }
    }
}
Output:

No comments:

Post a Comment