0

Currently, I have three textviews (H M S) for hours, minutes, and seconds, respectively. They are selectable; however, let's say textView H is selected. when the textview M is touched then textview H automatically gets deselected and textview M is the only one selected. Same case for S. So they switch.

Right now I have the following:

enter image description here

   View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView previousText = (TextView) previousView;
                TextView curText = (TextView) v;
                // If the clicked view is selected, deselect it
                if (curText.isSelected()) {
                    curText.setSelected(false);
                    curText.setTextColor(getResources().getColor(R.color.red_highlight));
                }
                // If this isn't selected, deselect  the previous one (if any)
                else {
                    if (previousText != null && previousText.isSelected()) {
                        previousText.setSelected(false);
                        previousText.setTextColor(getResources().getColor(R.color.red_highlight));
                    }
                    curText.setSelected(true);
                    curText.setTextColor(getResources().getColor(R.color.white));
                    previousView = v;
                }

            }
        };

        findViewById(R.id.hourtext).setOnClickListener(clickListener);
        findViewById(R.id.minutetext).setOnClickListener(clickListener);
        findViewById(R.id.secondtext).setOnClickListener(clickListener);

I want the timer to record for hours when H is selected, minutes when M is selected, and seconds when S is selected.

I am trying to follow this:

Android OnClickListener - identify a button

But how do I check which TextView is selected currently, so a different method/function can be performed when that TextView is selected.

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81
  • Did you try `setOnClickListener` for all the textviews? Then write the logic what each tv should do inside `onClick`. – Aniruddha Jul 18 '14 at 04:20
  • "mBtn.isSelected()" will help you to get the selected state of your button, same as your text case above. – sai Jul 18 '14 at 06:20
  • Also in your currently selected button's onClick listener put flags or create method to check the states of all the Buttons is "isSelected()" or not, onclick of that Button execute the code you need, for example, if(btnH.isSelected()){==Do SomeThing==} elseif(btnM.isSelected()) {==Do Something M==}elseif(btnS.isSelected()) {== Do Something S==} – sai Jul 18 '14 at 06:23

2 Answers2

1

You can check with your text_view id like below ...

View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getID() == R.id.hourtext){
                   //corresponding button logic should below here
                } else if (v.getID() == R.id.minutetext) {
                   //corresponding button logic should below here
                } else if (v.getID() == R.id.secondtext) {
                   //corresponding button logic should below here
                }
                }

            }
        };

        findViewById(R.id.hourtext).setOnClickListener(clickListener);
        findViewById(R.id.minutetext).setOnClickListener(clickListener);
        findViewById(R.id.secondtext).setOnClickListener(clickListener);
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12
0

I am assuming that by 'selected' , you mean focused as well. Use this:

Call getCurrentFocus() on the parent view: http://developer.android.com/reference/android/app/Activity.html#getCurrentFocus%28%29

harveyslash
  • 5,906
  • 12
  • 58
  • 111