In this program we will pass value of a intent to another activity class.This is pretty simple.We have to use putExtra() method to pass the value.
For this we will take two XML layout.One is the main and another is newintent.The XML code for the main is
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Here we take a button.when we press it the value is passed to the NewIntent Activity.We take another XML (newintent)with a TextView.The passed value will be shown in this TextView.The XML code for newintent:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Now we code the MyValue class.(Notice,by convention we should start a class name with capital letter and a XML name with a small letter.)In this class the value(the string "hello") is given that will be passed.
For this we will take two XML layout.One is the main and another is newintent.The XML code for the main is
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Here we take a button.when we press it the value is passed to the NewIntent Activity.We take another XML (newintent)with a TextView.The passed value will be shown in this TextView.The XML code for newintent:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Now we code the MyValue class.(Notice,by convention we should start a class name with capital letter and a XML name with a small letter.)In this class the value(the string "hello") is given that will be passed.
package com.value.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyValue extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String a = "hello";
Intent intent = new Intent(MyValue.this,NewIntent.class);
intent.putExtra("extra_value", a);
startActivity(intent);
}
});
}
}
Here in this code we create a object named intent of Intent class.The value of "a" is passed to NewIntent class via putExtra.Now we code the NewIntent class.
package com.value.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class NewIntent extends Activity{
public void onCreate(Bundle savedInsatanceState){
super.onCreate(savedInsatanceState);
setContentView(R.layout.newintent);
public class NewIntent extends Activity{
public void onCreate(Bundle savedInsatanceState){
super.onCreate(savedInsatanceState);
setContentView(R.layout.newintent);
TextView tv=(TextView)findViewById(R.id.TextView01);
Intent intent = this.getIntent();
String value = intent.getStringExtra("extra_value");
tv.setText("Carried value is:::"+value);
}
}
Intent intent = this.getIntent();
String value = intent.getStringExtra("extra_value");
tv.setText("Carried value is:::"+value);
}
}
Here the most important things to remember is,we have to register the new Activity class NewIntent in the Android manifest.For this we have to open the manifest,open the Activity tab and there we have to add the NewIntent Activity.Otherwise it will give a runtime error,when the button will be pressed.
No comments:
Post a Comment