15
<uses-sdk android:minSdkVersion="8" />

onClick method defined in xml

 <TextView
    android:id="@+id/titlemainpage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"       
    android:text="Social To Dos"   
    android:onClick="testing" />

testing method used in java class is

public void testing(View v){

    Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show();

    textview.setTextColor(Color.CYAN);
}
Shahzad Imam
  • 2,030
  • 2
  • 26
  • 45

5 Answers5

37

Add one more attribute to the textview in xml:

android:clickable="true"

Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • 1
    Thanks a lot!! It was working out of the box since the day I started making my app... But suddenly today: The TextView's onClick="methodName" was not at all being called on pre-lollipop devices. Why it worked so far is a mystery now :P – Vinay Vissh Feb 28 '17 at 21:24
  • Don't forget to add setOnClickListener on onCreateView too e.g. mBinding.llSpeedHome.setOnClickListener(this) – Ashraf Amin Jul 14 '22 at 03:16
2

Buttons are by default clickable but TextViews are not. Unless you explicitly setup the onClick listener at runtime textViews won't be clickable on pre-Lollipop devices.

So if you want to make a TextView clickable which is hooked with a listener in XML layout file you should use

android:clickable="true"
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
1

Dont forget to remove the on click listener (if you have set that programmatically inside the containing activity).

0

You have to set setOnClickListener on onCreateView e.g. mBinding.llSpeedHome.setOnClickListener(this)

Ashraf Amin
  • 343
  • 3
  • 7
-3

I might be wrong but I think you have to implement a listener rather than using a simple function.

Check this website.

M Rajoy
  • 4,028
  • 14
  • 54
  • 111