Android list view setOnItemClickListener not working

29,607

Solution 1

Add the following attributes to your button

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

ListView setOnItemClickListener not working by adding button

Is your edittext taking focus on click of list item? If so remove the focus on edittext also.

Solution 2

Here your edittext inside the listview is having all the focus, so this listview onClick is not working. Remove the focus from the editext box and it will start working

Share:
29,607
Grant
Author by

Grant

Student. Like to programming and just started Android programming. Still learning by my own and with your help. :)

Updated on August 30, 2020

Comments

  • Grant
    Grant over 3 years

    I want to hide the edit text and button field initially in list view and show that edit text and button for a particular raw in list view when that raw is clicked.So I tried to set the height to 0 in layout xml and then set it to some other value when user clicks on a raw, but it is not working I think my list view click event is not working. In the Android layout that I have list view there are Image view as a button, edit text field and list view also. Like follows

    <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=".MainPortal" >
    
    <ListView
        android:id="@+id/employeeListView"
        android:layout_width="650dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="25dp"
        android:clickable="true">
    </ListView>
    
    <EditText
        android:id="@+id/empPin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/iv_start_journey_btn"
        android:layout_marginBottom="56dp"
        android:layout_marginRight="17dp"
        android:layout_toLeftOf="@+id/employeeListView"
        android:ems="10"
        android:inputType="number"
        android:hint="@string/add_emp_pin_hint" >
    
        <requestFocus />
    </EditText>
    
    <ImageView
        android:id="@+id/iv_start_journey_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/employeeListView"
        android:layout_alignRight="@+id/empPin"
        android:layout_marginBottom="84dp"
        android:onClick="startJourney"
        android:src="@drawable/start" />
    
    </RelativeLayout>
    

    I have used following custom lay out for the list view

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView
        android:id="@+id/emp_avatar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:adjustViewBounds="true"
        android:maxHeight="80dp"
        android:maxWidth="80dp"
        android:src="@drawable/person" />
    
    <TextView
        android:id="@+id/emp_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/emp_avatar"
        android:layout_toRightOf="@+id/emp_avatar"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    
    <EditText
        android:id="@+id/empPin"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignBottom="@+id/emp_avatar"
        android:layout_alignRight="@+id/emp_number"
        android:layout_toRightOf="@+id/emp_avatar"
        android:ems="10"
        android:inputType="number" >
    
        <requestFocus />
    </EditText>
    
    <Button
        android:id="@+id/empAdd"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignBaseline="@+id/empPin"
        android:layout_alignBottom="@+id/empPin"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/empPin"
        android:text="@string/emp_add_btn" />
    

    Here I post the code of Activity.java.

    public class MainPortal extends Activity {
    private List<Employee> employees = new ArrayList<Employee>();
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_portal);
        populateEmployeeList();
        //populsteListView();
    
        ListView list = (ListView) findViewById(R.id.employeeListView);
        ArrayAdapter<Employee> adapter = new MylistAdapter();
        list = (ListView) findViewById(R.id.employeeListView);
        list.setAdapter(adapter);
    
         list.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
    //I ADDED ON CLICK IMPLEMENTATION HERE, BUT THIS IS NOT WORKING
                  Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();   
                  }
                });
    
    
    private void populateEmployeeList() {
          ...
    }
    
    
    
    private class MylistAdapter extends ArrayAdapter<Employee>{
    
    
        public MylistAdapter(){
            super(MainPortal.this,R.layout.item_view,employees);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if(itemView==null){
                itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
            }
            Employee currentEmployee = employees.get(position);
            //Avatar
            ImageView imageView = (ImageView) itemView.findViewById(R.id.emp_avatar);
            imageView.setImageResource(currentEmployee.getAvatarId());
            //Employee number
            TextView emp_id = (TextView) itemView.findViewById(R.id.emp_number);
            emp_id.setText(currentEmployee.getId());
    
            et = (EditText) itemView.findViewById(R.id.empPin);
            return itemView;
        }
    }
    }
    

    Above I have posted some codes that I think important. Can any one please help me to do this task. Thanks in advance.

  • Raghunandan
    Raghunandan over 10 years
    @Grant the button takes focus when you click on list item. CHeck the same for edittext. if so remove the focus fro both button and edittext
  • Raghunandan
    Raghunandan over 10 years
    @Grant you can add this to the root element in xml android:descendantFocusability="blocksDescendants"
  • Grant
    Grant over 10 years
    I set EditText.setHeight(SOME VALUE) on list view on click listener. but it is not working. What can be the wrong
  • Raghunandan
    Raghunandan over 10 years
    do you initialize editext and then do the same also you need to refersh listview by calling adapter.notifyDataSetChanged()
  • Grant
    Grant over 10 years
    I initialized edit text, but I don't know about this adapter.notifyDataSetChanged() , please tell me how to use this. Thanks
  • Raghunandan
    Raghunandan over 10 years
    it is used to refresh listview. suppose you change the underlying data that populated listview you call notifyDataSetChanged() on your adapter to refresh lsitview. Also why do you want to change the height of edittext dynamically. just set it to wrap_content
  • Grant
    Grant over 10 years
    I want to initially hide the edit text and when user clicks on particular item only I want show that edit text, but it is not working.
  • Raghunandan
    Raghunandan over 10 years
    they why do you need height settign here. you can set the view visibility to inivisible or gone depending on your need
  • Grant
    Grant over 10 years
    Thank you verymuch for the information I didn't know that. Now I used editText.setVisibility(View.GONE); adapter.notifyDataSetChanged(); but this is also not working, do you have any idea in xml lay out I set android:visibility="invisible" for the edit text
  • Whome
    Whome almost 9 years
    My problem was this in my item layout: android:inputType="textMultiLine". Once removed items were click-able.
  • GFPF
    GFPF over 8 years
    this works, but for that the property android: clickable must be false for the "view" in question. Attention to the parents of the "View" to be not using "android: clickable" on. this will block the pattern selector of your list or derivative. You can match the pattern selector with a custom selector using the property android: foreground like this: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:fitsSystemWindows="true"> <FrameLayout "android: foreground="CUSTOM SELECTOR""> <RelativeLayout><RelativeLayout/> <.../> <.../>