19

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.

6 Answers 6

21

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; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

is this working code or pseudocode? couldnt get it to work as is. but tks anyway! UPVOTED! :)
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" /> 

2 Comments

In this example, android:textColorLink does nothing, unless the text contains an actual hyperlink.
also needs android:focusable="true" according to the linter
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; } }); 

Comments

4

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

Comments

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() 

1 Comment

This still has the setting act for each textview, which @Cris wanted to avoid... You should elaborate a bit about movementMethod (an interesting solution)
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() } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.