Pass EditText value to another activity in android

Send EditText entered selected value to another android activity on button click and receive+set into textview.

Some of android application developer want to get edittext value then set that selected value inside textview placed on second next activity. So here is the complete step by step tutorial for Pass EditText value to another activity in android.

android-project-download-code-button

How to Pass EditText value to another activity in android.

Code for MainActivity.java file.

 package com.passedittextvalue_android_examples.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 MainActivity extends Activity {

 EditText SendValue;
 Button SendEditTextValue;
 Intent intent;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SendEditTextValue = (Button)findViewById(R.id.button1);
 SendValue = (EditText)findViewById(R.id.editText1);
 
 SendEditTextValue.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 intent = new Intent(getApplicationContext(),SecondActivity.class);
 intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
 startActivity(intent);
 
 }
 });
 }
}

Code for activity_main.xml layout file.

<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: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="com.passedittextvalue_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:ems="10" >
 </EditText>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="26dp"
 android:text="Click Here to Pass edittext value to another activity in android" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Main Activity"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Code for SecondActivity.java file.

 package com.passedittextvalue_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

 TextView receive;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 
 receive = (TextView)findViewById(R.id.textView1);
 
 receive.setText(getIntent().getStringExtra("EdiTtEXTvALUE"));
 
 }
}

Code for activity_second.xml layout file.

 <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: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="com.passedittextvalue_android_examples.com.SecondActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Large Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="22dp"
 android:text="Second Activity"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Pass EditText value to another activity in android

Click Here to download Pass EditText value to another activity in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *