Android ListView item and button clickable

Typically an item of a ListView is clickable after you use the setOnItemClickListener() method to register OnItemClickListener to receive the item-click event. However, the item is not clickable if it contains a button. This is because the focus is on the button. It is not on the item. A workable solution is to use the focusable attribute of the button. You need to assign false value to this attribute to avoid focusing on the button when the list is displayed. By doing this, you will be able to click the item of the list and the button.



Now to have an example application on making both ListView item and button clickable, you create a new Android project. Then in the activity_main.xml file, you add a ListView.

<LinearLayout 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:orientation="vertical"
   tools:context="com.example.myapp.MainActivity" >  
  
   <ListView
     android:id="@+id/listv"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
  
     >
    
   </ListView>
  
</LinearLayout>


In the res/layout folder, you create a layout file (list_item.xml) of the ListView. It has one TextView and one Button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="5dp"
   android:orientation="horizontal"
   >
  
<TextView
     android:id="@+id/txt"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_marginRight="10dp"
       />
<Button
     android:id="@+id/bt"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:focusable="false"
     android:padding="10sp"
     android:onClick="clickMe"

      >
</Button>
  
</LinearLayout>


In the src folder, you create class called LstViewAdapter that extends ArrayAdaper class. An instance of the LstViewAdapter is used to supply data to the ListView.


public class LstViewAdapter extends ArrayAdapter<String> {
int groupid;
String[] item_list;
ArrayList<String> desc;
Context context;
public LstViewAdapter(Context context, int vg, int id, String[] item_list){
super(context,vg, id, item_list);
this.context=context;
groupid=vg;
this.item_list=item_list;

}
// Hold views of the ListView to improve its scrolling performance
static class ViewHolder {
  public TextView textview;
  public Button button;
  
}

public View getView(int position, View convertView, ViewGroup parent) {

View rowView = convertView;
// Inflate the list_item.xml file if convertView is null
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView= inflater.inflate(groupid, parent, false);
ViewHolder viewHolder = new ViewHolder();  
viewHolder.textview= (TextView) rowView.findViewById(R.id.txt);
viewHolder.button= (Button) rowView.findViewById(R.id.bt);
rowView.setTag(viewHolder);  
  
}
// Set text to each TextView of ListView item
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.textview.setText(item_list[position]);
holder.button.setText(item_list[position]);
return rowView;
}

}



In the MainActivity class, you write code to populate the ListView with sample data, and do actions when the item and button are clicked.

public class MainActivity extends Activity{
Context context;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     context=this;
     ListView lstview=(ListView)findViewById(R.id.listv);
     lstview.setOnItemClickListener(new OnItemClickListener(){
     public void onItemClick(AdapterView<?> parent, View view, int position, long id){
     Toast.makeText(context, "An item of the ListView is clicked.", Toast.LENGTH_LONG).show();
     }
     });
    
     String[] items={"1","2","3","4","5"};    
     LstViewAdapter adapter=new LstViewAdapter(this,R.layout.list_item,R.id.txt,items);
     // Bind data to the ListView
     lstview.setAdapter(adapter);
    
    
              
    
   }
  
   public void clickMe(View view){
Button bt=(Button)view;
Toast.makeText(this, "Button "+bt.getText().toString(),Toast.LENGTH_LONG).show();
}
  
  
}


Android-ListView-item-and-button-clickable

Posted by: Dara | post date: 01-07-2015 | Subject: C Programming Language

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.