Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable TextView inside a customized ListView

I made my own customized ListView and in each row there are 2 TextViews.

I intend to make those textviews inside each row of the listview clickable. So whenever the user clicks on any of them, it will redirect him to some other place.

Actually, when the user clicks on a ListView item, it clicks all of it, and the textviews are being ignored.

In conclusion, when I click on a ListView item, I don't want the Listview item to respond, I want the Textview inside the Listview to respond.

EDIT:

Here is how i customized my view:

String[] from = {"value1", "value2"};
int[] to = {R.id.label, R.id.label2};
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.rowlayout, from, to);
like image 910
Alex Avatar asked Aug 09 '26 10:08

Alex


2 Answers

In your Adapter class, inside getview() method you can perform onclick action like this

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    Holder holder = null;
    if (row  == null) {
        LayoutInflater vi = 
                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = vi.inflate(R.layout.listitem, null);
        holder.txtTitle1 = (TextView) row.findViewById(R.id.heading1);
        holder.txtTitle2 = (TextView) row.findViewById(R.id.heading2);

        holder.txtTitle1 .setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
            }
        });

        holder.txtTitle2 .setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
            }
        });
    }
    return row;
}
/////
///////////

static class Holder {
    TextView txtTitle1;
    TextView txtTitle2;
}
like image 50
Amit Gupta Avatar answered Aug 11 '26 22:08

Amit Gupta


set your TextView in your layot.xml

 <TextView
     android:id="@+id/YourID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:clickable="true"

/>

and then on your Custom Adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    Holder holder = null;
    if (row  == null) {
        LayoutInflater vi = 
                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = vi.inflate(R.layout.youLayout, null);
        holder.txt1 = (TextView) row.findViewById(R.id.tx1);
        holder.txt2 = (TextView) row.findViewById(R.id.tx2);

       holder.txt1 .setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
//your statements            
}
        });

        holder.txt2 .setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
//your statements
            }
        });
    }
    return row;
}
like image 34
Spry Techies Avatar answered Aug 12 '26 00:08

Spry Techies