Get Selected Item In ListView Android

Photo by Henrik Dønnestad on Unsplash

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create a Blank Activity and click on Finish button.
Step 2 : Open res -> layout ->activity_main.xml and add following code :

< 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.saud.listview_listactivity.MainActivity” >

< ListView

android : id = “@+id/list”

android : layout_width = “match_parent”

android : layout_height = “wrap_content”

android : layout_below = “@+id/textview1” >

< / ListView >

< / RelativeLayout >

Step 3 : Open src -> package ->MainActivity.java and add following code :

package com . saud. android .listview_getselecteditem ;

import android . app . Activity ;

import android . os . Bundle ;

import android . view . View ;

import android . widget . AdapterView ;

import android . widget . AdapterView .OnItemClickListener ;

import android . widget . ArrayAdapter ;

import android . widget . ListView ;

import android . widget . TextView ;

import android . widget . Toast ;

public class MainActivity extends Activity {

static final String [ ] FRUITS = new String [ ] {“Apple” , “Banana” , “Coconut” ,

“Durian” , “Guava” , “Kiwifruit” , “Jackfruit” ,”Mango” , “Orange” ,

“Pear” } ;

@Override

public void onCreate ( Bundle savedInstanceState ){

super . onCreate ( savedInstanceState ) ;

setContentView ( R . layout . activity_main ) ;

ListView listView = ( ListView ) findViewById( R . id . list ) ;

ArrayAdapter <String> adapter = newArrayAdapter <String> ( this ,

android . R . layout . simple_list_item_1 ,FRUITS ) ;

listView . setAdapter ( adapter ) ;

listView . setOnItemClickListener ( newOnItemClickListener ( ) {

public void onItemClick ( AdapterView < ? >parent , View view ,

int position , long id ) {

// When clicked, show a toast with the TextView text

Toast . makeText ( getApplicationContext ( ) ,

( ( TextView ) view ) . getText ( ) , Toast .LENGTH_SHORT ) . show ( ) ;

//String selectedItem = parent.getItemAtPosition(position).toString(); or

//String selectedItem = ((TextView) view).getText(); //for simple_list_item_1 layout or

//String selectedItem = (TextView) view.findViewById(android.R.id.text1); //for simple_list_item_1 layout with textview id or

//String selectedItem = FRUITS[position]; // using selected position

}

} ) ;

}

}

Step 4 : Open AndroidManifest.xml and add following code :

<? xml version = “1.0” encoding = “utf-8” ?>

< manifest xmlns : android = “http://schemas.android.com/apk/res/android"

package =”com.saud.android.listview_getselecteditem”

android : versionCode = “1”

android : versionName = “1.0” >

< uses — sdk

android : minSdkVersion = “16”

android : targetSdkVersion = “19” / >

< application

android : allowBackup = “true”

android : icon = “@drawable/ic_launcher”

android : label = “@string/app_name”

android : theme = “@style/AppTheme” >

< activity

android : name =”com.skholingua.android.listview_getselecteditem.MainActivity”

android : label = “@string/app_name” >

< intent — filter >

< action android : name = “android.intent.action.MAIN”/ >

< category android : name =”android.intent.category.LAUNCHER” / >

< / intent — filter >

< / activity >

< / application >

< / manifest >

--

--

--

I’m a good husband, father and Senior Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The impact of lockdown on mobile gamers behaviors

Testing Realm Migrations.

Android Core interview questions part 1

Espresso Testing with Hilt and MockWebServer

Typical ANR in Android

Android : Vulnerability is inevitable | Part 2

Introduction to RxJava

Implement In app update For Your android app

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingwithSaud

CodingwithSaud

I’m a good husband, father and Senior Developer.

More from Medium

Hello World! With Android Studio

Setting up Mapbox SDK 10.1.0 in Android Studio (2021)

How to build in-app chat using Kotlin — Part 1

How to get started with Android Development