In this article you will learn how to send data from one Activity to another Activity,I created two activity AndroidLoginActivity and LoginSuccess, in the LoinSuccess Activity will display logged user name.
Right the project and create Java class and name it as LoginSuccess , copy and past bellow code.
Create new layout for LoginSuccess
Right click on res/layout , New-> Android XML give the file name as success, copy and past bellow xml
Add AndroidLoginActivity and LoginSuccess to AndroidManifest.xml
AndroidLoginActivity is the main activity
Download application source code from here Simple Android Login
Read more...
- Create Android project visit my previous example First Android Application in Eclipse Hello world Android Example, Un check the
Add OnClickListener to Button and Send Data to next Activity
// Set OnClickListner to the login button Button login = (Button) findViewById(R.id.btn_login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Get user Name EditText loginName = (EditText) findViewById(R.id.txt_userName); String name = loginName.getText().toString(); // create Intent and set LoginSuccess Activity Intent intent = new Intent(getApplicationContext(), LoginSuccess.class); // put values to intent which will get in the LoginSuccess Activity intent.putExtra("name", name); // Start LoginSuccess Activity startActivity(intent); } });
Create AndroidLoginActivity
Right the project and create Java class and name it as AndroidLoginActivity, copy and past bellow code.
package jsupport.com; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class AndroidLoginActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); /* Set OnClickListner to the login button */ Button login = (Button) findViewById(R.id.btn_login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText loginName = (EditText) findViewById(R.id.txt_userName); String name = loginName.getText().toString(); Intent intent = new Intent(getApplicationContext(),LoginSuccess.class); intent.putExtra("name", name); startActivity(intent); } }); } }
Create new layout for AndroidLoginActivity
Right click on res/layout , New-> Android XML give the file name as login , copy and past bellow xml
Get Value from Intent
Create LoginSuccess Activity
Right click on res/layout , New-> Android XML give the file name as login , copy and past bellow 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" > <TextView android:text="Login here" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="180dp" ></TextView> <TextView android:text="User Name " android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView2" android:layout_height="wrap_content" android:layout_width="126dp"></TextView> <EditText android:layout_height="wrap_content" android:id="@+id/txt_userName" android:layout_width="272dp"> <requestFocus></requestFocus> </EditText> <TextView android:text="Password" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/password" android:layout_width="272dp"></EditText> <Button android:text="Login" android:layout_height="wrap_content" android:layout_width="126dp" android:id="@+id/btn_login" ></Button> </LinearLayout>
Get Value from Intent
/* Get values from Intent */ Intent intent = getIntent(); String name = intent.getStringExtra("name");
Create LoginSuccess Activity
Right the project and create Java class and name it as LoginSuccess , copy and past bellow code.
package jsupport.com; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class LoginSuccess extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.success); TextView txt_loggedName = (TextView) findViewById(R.id.txt_loggedName); /* Get values from Intent */ Intent intent = getIntent(); String name = intent.getStringExtra("name"); txt_loggedName.setText(name); } }
Right click on res/layout , New-> Android XML give the file name as success, copy and past bellow xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:weightSum="1"> <TextView android:text="Hello" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:layout_width="158dp" android:layout_height="wrap_content" android:layout_weight="0.06"></TextView> <TextView android:text="Java Srilankan Support" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/txt_loggedName"></TextView> </LinearLayout>
Add AndroidLoginActivity and LoginSuccess to AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jsupport.com" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AndroidLoginActivity" 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="LoginSuccess"></activity> </application> </manifest>
AndroidLoginActivity is the main activity
Download application source code from here Simple Android Login