In our example, we have a LearningActivity (which is the first screen of my application) and a HelloActivity (this would be the activity that I would like to navigate to). The LearningActivity class uses a simple layout that contains an TextEdit and a Button.
User can enter his name in the text box and on click of the button.
When user clicks the button, we want to pass the name that user entered and start HelloActivity which will simply print Hello with the name.
Let us get into action. This is what we are going to do
- Attach an onclick listener to the button.
- On click of the button, read the entry in the text box
- Add the data to a Bundle
- Create an Intent with the target activity
- Set the bundle as extras to the Intent
- Start the next activity
I have added the full source code below. My eclipse project structure is as below
Below is the layout, main.xml, that LearningActivity uses
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your name:" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>
Here is the LearningActivity class
package com.test;
import android.app.Activity;
import android.content.Context;
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 LearningActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context ctx = this;
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.editText1);
String userName = et.getText().toString();
Bundle b = new Bundle();
b.putString("NAME", userName);
Intent t = new Intent(ctx, HelloActivity.class);
t.putExtras(b);
startActivity(t);
}
});
}
}
Here is hello.xml, which is the layout that HelloActivity uses
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
and here is the code for HelloActivity
package com.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello);
Bundle b = getIntent().getExtras();
String userName = (String)b.get("NAME");
TextView t = (TextView)findViewById(R.id.textView1);
t.setText("Hello "+userName+"!");
}
}