vignesh
vignesh

Reputation: 1599

Writing onClick event for textviews inside linear layout

I have two linear layouts present inside relativelayout. Each linear layout contains three text view. I want to write onclick event for all text views present in both linear layouts in general. Please advice.

Upvotes: 0

Views: 1609

Answers (2)

khubaib
khubaib

Reputation: 535

Have a common click listener for all the text views. From the common click listener handle the click events of all the text views by the ID of the text view.

Sample FYR.

findViewById(R.id.textview1_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview2_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview3_id).setOnClickListener(commonClickListener);

private OnClickListener commonClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        int selectedItemId = v.getId();
        switch (selectedItemId) {
        case R.id.textview1_id:
            // implement your code here.
            break;
        case R.id.textview2_id:
            // implement your code here.
            break;
        case textview3_id:
            // implement your code here.
            break;

        }
    }`

Upvotes: 0

stinepike
stinepike

Reputation: 54682

in all TextView add the following attribute

android:onClick="onClick"

dont forget to set id to all TextView too

Then from your code

public void onClick(View v){
      switch (v.getId()) {
          case R.id.tv1:
             // do somethong
                   break;
                 default:
                   break
        }

Upvotes: 1

Related Questions