lmouhib
lmouhib

Reputation: 47

Checking which textview is clicked android

I want to put two textviews and then decide which one of them is clicked to start the appropriate activity? I don't want to use buttons. Any idea??

Upvotes: 0

Views: 3475

Answers (3)

Vinay Sagar Savithru
Vinay Sagar Savithru

Reputation: 19

You can try this:

TextView name= (TextView) findViewById(R.id.box_Name);
TextView name2= (TextView) findViewById(R.id.box_Name2);

name.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public void onTouch(View v) {

     //code here ...
     Toast.makeText(getApplicationContext(), "Clicked first...", 
     Toast.LENGTH_LONG).show();
    }
});

  name2.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public void onTouch(View v) {

     //code here ...
     Toast.makeText(getApplicationContext(), "Clicked second..", 
     Toast.LENGTH_LONG).show();
    }
});

Upvotes: 0

Arshid KV
Arshid KV

Reputation: 10037

Add following code in onCreate

TextView texVar= (TextView) findViewById(R.id.box_Name);
TextView texVar2= (TextView) findViewById(R.id.box_Name2);

texVar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

     //code here ...
     Toast.makeText(getApplicationContext(), "Clicked first!!", 
     Toast.LENGTH_LONG).show();
    }
});

  texVar2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

     //code here ...
     Toast.makeText(getApplicationContext(), "Clicked second!!", 
     Toast.LENGTH_LONG).show();
    }
});

Upvotes: 2

yshahak
yshahak

Reputation: 5096

Just imlements the View.onClickListener interface in your activity, add:

textView1.setOnClickListener(this);

To each textView in your activity. Inside the onClick(View v) implementation add:

v.getId();

To determine whice TextView pressed.

Upvotes: 0

Related Questions