Home Next Tutorial

How To Pass Information From One Activity to Another Activity in Android


Step 1: Open Android Studio
Step 2: Select File->New->New Project
Step 3: In the Create New Project window type Application Name as "StaticLogin" and click on Next button. In the Target Android Devices select Minimum SDK and click on Next Button. In The Add an Activity to Mobile select blank activity and click on Next Button.Click on Finish button.
Step 4: Design an activity as shown below:

Static Login Example

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="User Name:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#020202" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etName"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Password:"
android:id="@+id/textView2"
android:layout_below="@+id/etName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#010101" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etPassword"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btnLogin"

android:layout_below="@+id/etPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

Step 5: Select File->New->Activity->Blank Activity.Specify Name of Activity as "SecondActivity" and click on Finish Button. It will create second activity in your application.

Step 6: Open MainActivity.java file and write following code in the onCreate() method to authentic user and pass information to second activity from MainActivity.

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et1 = (EditText)findViewById(R.id.etName);
final EditText et2 = (EditText)findViewById(R.id.etPassword);
Button b1 = (Button)findViewById(R.id.btnLogin);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(et1.getText().length()==0)
{
et1.setError("Please Enter Name");
}
else if (et2.getText().length()==0)
{
et2.setError("Please Enter Password");
}
else if(et1.getText().toString().equals("admin") && et2.getText().toString().equals("admin") )
{
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("name",et1.getText().toString());
startActivity(i);
}
else
{
Toast.makeText(getApplicationContext(),"Wrong UserName or Password",Toast.LENGTH_LONG).show();
}
}
});
}

Step 6:Open SecondActivity.java file and write following code in the onCreate () method to retrieve the information pass from MainActivity.

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle b;
Intent i = getIntent();
b = i.getExtras();
String Name = b.getString("name");
Toast.makeText(getApplicationContext(),"Welcome "+Name,Toast.LENGTH_LONG).show();
}

Step 7:Run the application.

Download Projects


Download Programs