Adam Taoufiq
Adam Taoufiq

Reputation: 49

Android studio how to know which textview was clicked?

I'm basically building a small quiz app and for every question, there are 4 answers in 4 different textviews. What I want to know is which textview has been clicked by the user.

Upvotes: 0

Views: 1113

Answers (4)

chandan
chandan

Reputation: 117

Try This :

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dgnc.testproject.R;

public class YourClass extends AppCompatActivity implements View.OnClickListener{
TextView view1,view2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view1 = (TextView)findViewById(R.id.text);
    view2 = (TextView)findViewById(R.id.text2);
    view1.setOnClickListener(this);
    view2.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    Toast.makeText(getApplicationContext(),"clicked textView : 
               "+v.getId(),Toast.LENGTH_LONG).show();
    switch (v.getId()){
        case R.id.text : 
            // do somthing
            break;
        case R.id.text2 : 
            //do somthing
            break;
    }

}

}

Upvotes: 1

Himanshu Sharma
Himanshu Sharma

Reputation: 133

You can give different id's to your TextViews and set onclickListener the following way

 TextView textView=(TextView)findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

and then perform any of the task you want.Here you can use if else statement in order to check the answer afterwards.

Upvotes: 1

Mani Kumar
Mani Kumar

Reputation: 56

You can easily do this by applying onClickListeners to the textViews, Following code may help :

TextView mTvAnswer1, mTvAnswer2, mTvAnswer3, mTvAnswer4;
mTvAnswer1 = findViewById(R.id.text_1);
// similarly find other textviews

mTvAnswer1.setOnClickListener(this);
mTvAnswer2.setOnClickListener(this);
mTvAnswer3.setOnClickListener(this);
mTvAnswer4.setOnClickListener(this);

And after this in you can override onClick() as follows:

@Override
public void onClick(View v){
switch (v.getId()){
     case R.id.text1:
         // First option clicked
         break;
     case R.id.text2:
        // Second option clicked
        break;
     case R.id.text3:
         // Third option clicked
         break;
     case R.id.text4:
         // Fourth option clicked
         break;

} }

Upvotes: 1

Saehun Sean Oh
Saehun Sean Oh

Reputation: 2281

There are many, many ways to do this. The simplest thing would be create a method that takes one parameter, than each TextView's OnClickListener will call that method with different parameter.

private final View.OnClickListener mOnOptionClickListener = 
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onOptionClicked(v.getTag());
        }
    });

private void onOptionClicked(final int optionNumber) {
    // take care of it
}

protected void onCreate(...) {
    // after all your textviews are bound... 
    textView1.setTag(1);
    textView2.setTag(2);
    textView3.setTag(3);
    textView4.setTag(4);

    textView1.setOnClickListener(mOnOptionClickListener);
    textView2.setOnClickListener(mOnOptionClickListener);
    textView3.setOnClickListener(mOnOptionClickListener);
    textView4.setOnClickListener(mOnOptionClickListener);
}

Upvotes: 1

Related Questions