Question

In my android code, I have implemented onclick event on 3 textviews using following code. But nothing happens when they are clicked. What is wrong ?

 <TextView 
 android:id="@+id/tv1"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:background="@drawable/menucircle"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:onClick="selectit"
 android:textColor="#ffffff"
/>


public void selectit(View v)
{
    Log.d("tv0","ok");
    if(v.getId()==tv1.getId())
    {Log.d("tv1","ok");
        selectoption(1);
        Log.d("tv1","ok");
    }
    if(v.getId()==tv2.getId())
    {Log.d("tv2","ok");
        selectoption(2);
    }
    if(v.getId()==tv3.getId())
    {Log.d("tv3","ok");
        selectoption(3);
    }
}
Was it helpful?

Solution

Add this into your xml

android:clickable="true"

OTHER TIPS

You probably need to have this code in your xml

android:clickable="true"

Or Set the Clicklistenner to TextView via code by

TextView btn=(TextView) findViewById(R.id.tv1);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        selectit(v);
    }
});

It will automatically make it clickable so no need of android:clickable="true"

You can set the click handler in xml with these attribute:

android:clickable="true"

Don't forget the clickable attribute, without it, the click handler isn't called.

main.xml

<TextView 
 android:id="@+id/tv1"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:background="@drawable/menucircle"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:onClick="selectit"            
 android:clickable="true"
 android:textColor="#ffffff"
/>

i hope it will help u all the best

Try like below:

android:onClick="onClick"
android:clickable="true"

My textview:

<TextView 
       android:id="@+id/click"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"               
       android:text="Click Me"
       android:textSize="55sp"
       android:onClick="onClick"                
       android:clickable="true"/>

Main Activity:

public class MyActivity extends Activity {

      public void onClick(View v) {
        ...
      }  
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top