1

i after a way of being able to validate a textview based on the state of a toggle button. if the toggle is in the on state i would like the max figure to be 9 and if in the off state i would like the max to be 14.

i currently have the following for my toggle

public void addListenerOnButton() {

unitToggle = (ToggleButton) findViewById(R.id.unitToggle);
        final TextView tw1 = (TextView)findViewById(R.id.tw1);
        final TextView tw2 = (TextView)findViewById(R.id.tw2);
        final TextView cw1 = (TextView)findViewById(R.id.cw1);
        final TextView cw2 = (TextView)findViewById(R.id.cw2);
        final TextView rightUnit = (TextView)findViewById(R.id.rightUnit);

        unitToggle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                StringBuffer result = new StringBuffer();
                if(tw1.getHint().toString().equals("kg")){
                    tw1.setHint("st");
                    tw2.setHint("lb");
                    cw1.setHint("st");
                    cw2.setHint("lb");
                }
                else{
                    tw1.setHint("kg");
                    tw2.setHint("g");
                    cw1.setHint("kg");
                    cw2.setHint("g");
                }
            }
        });
    }

tw2 and cw2 are the textviews i would like the validation to be added to. (the above just sets the hint for the textviews when the toggle is clicked. I also have a button that stores the values (tw1, tw2, cw1 and cw2) into shared preferences. once this store button is clicked a few conditions are run. my issue is I want to add a condition where by if the toggle state is on and tw2 or cw2 is >9 bring up alert dialog. (and same with off stae but with 14 instead of 9 )

any help would be appreciated.

If this doesn't sound clear please let me know and i'll rewrite it for you.


EDIT. I know how to validate by

        if( Integer.parseInt(tw2.getText().toString())>14){

but i don't know how to compare the 2 states of the toggle

Tuffy G
  • 1,521
  • 8
  • 31
  • 42
  • lol. defo not a copy. that is a totally different issue – Tuffy G Apr 12 '12 at 13:23
  • and btw, your answer on that one wasn't allowing me to have setOnCheckedChangeListener – Tuffy G Apr 12 '12 at 13:26
  • and also the other question is about setting the hint on the textview whilst clicking on the toggle. this one requires validation from a totally different button dependent on the state. – Tuffy G Apr 12 '12 at 13:30

1 Answers1

1

You can just set the maximum length of the exitText. See this for info. length input filter

you simply set an input filter on the edit text or any textView component (editText extends TextView). dont use int parsing for that this way is much better. or at least count the string length, no need to convert to Int.

for checking the toggle button state, set a checkedChangeListener described here:

ToggleButton

and to check if the button is toggled or not you simply use isChecked(), also described in the above documentation.

Community
  • 1
  • 1
DArkO
  • 15,880
  • 12
  • 60
  • 88