Home » Android » unable to select row in listview in a popup in 4.1 but working in android 5.0

unable to select row in listview in a popup in 4.1 but working in android 5.0

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a popup in my page which has a listview. I have created design of popup in a seperate xml and loading it on some button click in my main page. The popup has a listview with each row having a image and a textview. I am not able to get row selection in listview in android 4.1 but it is working in 5.0. Can anyone please suggest me the solution?
Listview:

<ListView android:id="@+id/lstSites"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fastScrollEnabled="true"
        android:layout_margin="5dp"
        android:descendantFocusability="blocksDescendants"></ListView>
</LinearLayout>

Listview Item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
    android:id="@+id/thumbImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:focusable="false"
    android:focusableInTouchMode="false"/>
<TextView
    android:id="@+id/tvSite"
    android:textSize="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="Name"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

adding row click listner:

lstSiteMenu = (ListView) popupView.findViewById(R.id.lstSites);

            adapter = new MenuItemsAdapter(SiteActivity.this, arrMenu);
            // Attach the adapter to a ListView
            lstSiteMenu.setAdapter(adapter);
            lstSiteMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        final int position, long id) {
            }
How to&Answers:

Try all of solutions:


popupwindow.setFocusale(true);
listView xml : "android:focusable="true"

do not use lstSiteMenu.setOnItemClickListener
instead go to your Adapters getView and add

convertView.setOnClickListener

Hope it will help you!

Answer:

remove this property from listview , android:descendantFocusability="blocksDescendants"
and

android:focusable="false"
android:focusableInTouchMode="false"

from text and imageview.

And try This is sample example,

String names[] ={"A","B","C","D"};
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = (View) inflater.inflate(R.layout.row_file, null);
        alertDialog.setView(convertView);
        alertDialog.setTitle("List");
        ListView lv = (ListView) convertView.findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
        lv.setAdapter(adapter);
        alertDialog.show();

otherwise your code is fine. it works.

go through this link for more details.

Another official link for the same.

Answer:

You need to remove android:descendantFocusability="blocksDescendants" from your ListView.

Also remove android:focusable="false" and android:focusableInTouchMode="false" from your row layout in the ImageView and the TextView.

The list item is supposed to be clickable, there is disable it obtaining focus

Answer:

Set row onclick listener in adapter class

public class MenuItemsAdapter extends Base Adapter{
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
             /*
                          Paste your adapter code
             */
              customView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("log", "You clicked at pos "+position);
                }
            });
        }
}

This code worked in all sdk versions.

Also remove these properties from xml files

android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"