Android Send ListView Clicked value to Another Activity

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:-

  1. MainActivity.java
  2. SecondActivity.java

List of layout files in this project :-

  1. activity_main.xml
  2. activity_second.xml

android-project-download-code-button

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 :

Android Send ListView Clicked value to Another Activity

Click here to download Android Send ListView Clicked value to Another Activity project with source code.

2 Comments

  1. i have project like this , i can’t send a data to SecondActivity. i think , i have been do like this tutorial but is not going to success. i guest, it’s because my listview source from mysql database. Any body help me to solve my problem. i will thankful for u who helping me.

Leave a Reply

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