In the previous post we learned to move between activities through Intents.Now we are going to learn about passing data(String/int/char etc) from one activity to other using Intents. In this tutorial we are going to pass a string on clicking button in one activity(MainActivity.java) and set it as 'text' in a TextView in the second activity
So let us begin..
STEP 1:
activity_main.xml
MainActivity.java
Let us make a small change in our 'onclick method' to make it pass a string say "pass" to the second activity
So MainActivity.java look like this,
STEP 2:
activity_main_activity2.xml
STEP 3:
Add this to OnCreate of second Activity
MainActivity2.java
So let us begin..
STEP 1:
activity_main.xml
<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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="Next Activity"/>
</RelativeLayout>MainActivity.java
Let us make a small change in our 'onclick method' to make it pass a string say "pass" to the second activity
public void onclick(View v)
{
Intent intent = new Intent(this, MainActivity2.class);
inten.putExtra("text", "pass");
this.startActivity(intent);
}So MainActivity.java look like this,
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onclick(View v)
{
Intent intent = new Intent(this, MainActivity2.class);
intent.putExtra("text", "pass");
this.startActivity(intent);
}
}STEP 2:
Now go to our second activity's layout and add a text view with android:id="@+id/your_id"=>used to identify a particular component(Here it is TextView).Actually it gives an identity to the textview
activity_main_activity2.xml
<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="cooltechiz.blogspot.com.helloworld.MainActivity2">
<TextView
android:id="@+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>STEP 3:
Add this to OnCreate of second Activity
String textview_text = getIntent().getExtras().getString("text"); TextView tv1 = (TextView) this.findViewById(R.id.your_id); tv1.setText(textview_text);MainActivity2.java
public class MainActivity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
String textview_text = getIntent().getExtras().getString("text");
//get the text from intent
TextView tv1 = (TextView) this.findViewById(R.id.your_id);
tv1.setText(textview_text);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}Now we finished coding.Click on 'RUN' button and Launch the application.Then click on Button in MainActivity.It will launch the second activity with a TextView with a text "pass" on it.So we passed the string from one activity to other.You are awesome!
No comments:
Post a Comment