20

I'm building an Android App that has many many TextViews that I want to be clickable. I've tried to assign the properties android:clickable="true" and android:onClick="clickHandler" to the single TextView and when the app fires the clickHandler(View v) I get the clicked item correctly through v.getId(). What I don't like is to define, for every TextView, the properties android:clickable and android:onClick ... is there something that i can do by code to say: "all the textviews are clickable and click is handled in clickHandler ?"

Thanks.

jrudolph
  • 8,307
  • 4
  • 32
  • 50
Cris
  • 12,124
  • 27
  • 92
  • 159

6 Answers6

22

You could do something like this below - this way they all have the same handler:

public class sticks extends Activity implements View.OnTouchListener { 
  private TextView tv1; 
  private TextView tv2;
  private TextView tv3;

  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tv1 = (TextView) findViewById(R.id.tv1); 
    tv2 = (TextView) findViewById(R.id.tv2); 
    tv3 = (TextView) findViewById(R.id.tv3); 

    // bind listeners
    tv1.setOnTouchListener(this); 
    tv2.setOnTouchListener(this); 
    tv3.setOnTouchListener(this); 

  } 

  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
    // check which textview it is and do what you need to do

    // return true if you don't want it handled by any other touch/click events after this
    return true; 
  } 
}
Brilliand
  • 13,404
  • 6
  • 46
  • 58
xil3
  • 16,305
  • 8
  • 63
  • 97
15

I used this:

        <TextView
            android:id="@+id/txtTermsConditions"
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:onClick="onTermConditions"
            android:clickable="true"
            android:focusable="true"
            android:text="Terms and Conditions"
            android:textColor="@color/blue" />
Roozbeh Zabihollahi
  • 7,207
  • 45
  • 39
7

Hi You Must use Both of below Code :

  <TextView
        android:id="@+id/reg_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_btn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:clickable="true"
        android:text="Are you now need to create an account?"
        android:textColor="#ff0000ff"
        android:textSize="15sp"
        />

And

private TextView tv1;
        tv1= (TextView)findViewById(R.id.reg_text);
    tv1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            ProgressDialog progressBar = ProgressDialog.show(MainActivity.this, "Title",
                    "شکیبا باشید!");
            progressBar.setCancelable(true);
            Intent i = new Intent(getApplicationContext(), Register.class);
            startActivity(i);
            return false;
        }
    });
Iman Marashi
  • 5,593
  • 38
  • 51
4

I use an OnClickListener, try it instead of OnTouchListener.Here is the a description how to use them.

Java_Waldi
  • 924
  • 2
  • 12
  • 29
0

Below code worked for me:

add to strings.xml:

 <string name="about_us"><font fgcolor="#039BE5">\"THIS IS SPARTA! more info at" <font fgcolor="#000000"><a href="https://google.com/">google.com</a></font>
</string>

add to TextView:

android:linksClickable="true"

add to activity:

about_us.movementMethod = LinkMovementMethod.getInstance()
Android01
  • 24
  • 5
  • This still has the setting act for each textview, which @Cris wanted to avoid... You should elaborate a bit about movementMethod (an interesting solution) – yakobom Sep 26 '17 at 04:18
0

Hello You Must use Both of below Code :

  1. Use is XML file code

    <com.google.android.material.textview.MaterialTextView
     android:id="@+id/tx_demo"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Demo World!" />
    
  2. Use is JAVA file code

     txDemo = findViewById(R.id.tx_demo);
     txDemo.setOnClickListener(v -> {
         Toast.makeText(this, "TextView Click", Toast.LENGTH_SHORT).show();
     });
    

OR 2) Use is Kotlin file code

 txDemo = findViewById(R.id.tx_demo)
    txDemo!!.setOnClickListener {
        Toast.makeText(
            this,
            "TextView Click",
            Toast.LENGTH_SHORT
        ).show()
    }