How to send & receive ListView selected value From one activity to next and set into EditText using Intent.
In this tutorial we would going to create a simple ListView using array adapter. Now we would add asenother activity inside this project named as SecondActivity.java .After that we would set setOnItemClickListener() on listview and pass the listview selected item to another activity using intent. Now on the next activity we would receive that item and set into EditText. So here is the compete step by step tutorial for Android Send ListView Clicked value to Another Activity.
List of activities in this project:-
- MainActivity.java
- SecondActivity.java
List of layout files in this project :-
- activity_main.xml
- activity_second.xml
How to Send ListView Clicked value to Another Activity.
Code for MainActivity.java file.
package com.android_examples.sendlistviewitem_android_examplescom; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends AppCompatActivity { ListView listView; // Define string array. String[] listValue = new String[] {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.listView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listValue); listView.setAdapter(adapter); // ListView on item selected listener. listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub // Getting listview click value into String variable. String TempListViewClickedValue = listValue[position].toString(); Intent intent = new Intent(MainActivity.this, SecondActivity.class); // Sending value to another activity using intent. intent.putExtra("ListViewClickedValue", TempListViewClickedValue); startActivity(intent); } }); } }
Code for activity_main.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.android_examples.sendlistviewitem_android_examplescom.MainActivity" android:padding="10dp"> <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Code for SecondActivity.java file.
package com.android_examples.sendlistviewitem_android_examplescom; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.EditText; public class SecondActivity extends AppCompatActivity { EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); editText = (EditText)findViewById(R.id.editText1); // Receiving value into activity using intent. String TempHolder = getIntent().getStringExtra("ListViewClickedValue"); // Setting up received value into EditText. editText.setText(TempHolder); } }
Code for activity_second.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.android_examples.sendlistviewitem_android_examplescom.SecondActivity" android:padding="10dp"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText1" android:gravity="center" android:hint="ListView Clicked Value Show Here" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
Screenshots :