Hello guys, how are you today? Its super Thursday, and hope you will be in the best of your life.
Today we are going to see how to pass value from one activity to other in Android. So lets get started.
1) First of all create an Android Application Project.
2) Next, go to "res/layout/activity_main.xml" and create one editText and one "send" button there. The code looks like.
activity_main.xml
3) Next go to "src/your_package_name/MainActivity.java" and here we are goin to retrieve the value written in the EditText and send the value to the "Second_Activity.java" on clicking the send button. The code looks like the following.
MainActivity.java
4) Now go to "res/layout" and create an Android Xml file there, for our case lets name it "show.xml" .
show.xml
Second_Activity.java
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you
Today we are going to see how to pass value from one activity to other in Android. So lets get started.
1) First of all create an Android Application Project.
2) Next, go to "res/layout/activity_main.xml" and create one editText and one "send" button there. The code looks like.
activity_main.xml
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id = "@+id/et" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id = "@+id/send" android:text="send"/> </LinearLayout>
3) Next go to "src/your_package_name/MainActivity.java" and here we are goin to retrieve the value written in the EditText and send the value to the "Second_Activity.java" on clicking the send button. The code looks like the following.
MainActivity.java
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener { EditText et; Button send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); send = (Button) findViewById(R.id.send); send.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.send: Intent i = new Intent(this, Second_Activity.class); i.putExtra("value", et.getText().toString()); startActivity(i); break; } } }
4) Now go to "res/layout" and create an Android Xml file there, for our case lets name it "show.xml" .
show.xml
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:id = "@+id/tv" android:text="Demo text" android:textColor="#000000" /> </LinearLayout>5) Next go to "src/your_package_name/Second_Activity.java" and here we are going to retrieve the value send in by MainActivity.java and set the same value in the TextView.
Second_Activity.java
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class Second_Activity extends Activity { TextView tv; String word; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show); try{ word = getIntent().getExtras().getString("value"); Toast.makeText(this, word, Toast.LENGTH_SHORT).show(); }catch(Exception e) { Toast.makeText(this, "Exception raised", Toast.LENGTH_SHORT).show(); } tv = (TextView) findViewById(R.id.tv); tv.setText(word); } }Note - Dont forget to declare the activity in AndroidManifest.xml
<activity android:name="com.example.passing_value.Second_Activity" />
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you
hey i have similar doubt regarding this.How to share php variables in 2 different php files?
ReplyDeleteI have declared variable $v1 in first.php,The value of $v1 is obtained through user's input via EditText widget in android.
Now how to use the value of variable $v1 ,declared in first.php, in second.php?
In php we use $_SESSION variable for transferring value from one php to another..
Delete