ListView listView = findViewById(R.id.list_view); int selectedItemPosition = listView.getSelectedItemPosition(); Toast.makeText(this, "Selected item position: " + selectedItemPosition, Toast.LENGTH_SHORT).show();
ListView listView = findViewById(R.id.list_view); if (listView.getSelectedItemPosition() != ListView.INVALID_POSITION) { View selectedItem = listView.getSelectedView(); selectedItem.setBackgroundColor(Color.GRAY); }This example checks if the currently selected item in the ListView is valid, then retrieves the corresponding View object using the getSelectedView() method. The background color of this view is then changed to gray. The package library for the android.widget.ListView class is android.widget.