0

I'm developing a simple app that flips the word in a sentence. I need it to have three ways of doing that depending on mode the user chooses by enabling RadioButtons. So I use RadioGroup as parent layout to be able to enable one RadioButton at a time. I can achieve this simply by switching their IDs.

modeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){

                case R.id.rb_mode_1:
                    String[] nowTyping = input.getText().toString().split(" ");
                    ArrayList<String> wordArray = new ArrayList<>();
                    for (String word : nowTyping){
                        wordArray.add(0, word);
                    }
                    String invertedSentence = TextUtils.join(" ", wordArray);
                    output.setText(invertedSentence);

                    break;

               //And so...

            }
        }
    });

Now as the output text is printed as the user is typing, I use TextWatcher to get what user is typing directly be shown into a textView. Now I'm unable to change mode because the code that flips the words is actually called from onTextChanged() method that the class implements.

 @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    String[] nowTyping = input.getText().toString().split(" ");
    ArrayList<String> wordArray = new ArrayList<>();
    for (String word : nowTyping){
        wordArray.add(0, word);
    }
    String invertedSentence = TextUtils.join(" ", wordArray);
    output.setText(invertedSentence);

}

My question is, how can I achieve my need when also using TextWatcher? Actually I can either change mode without being able to get texts by output in real time or have texts be output without being able to change mode.

esQmo_
  • 1,464
  • 3
  • 18
  • 43

1 Answers1

0

I solved this using getCheckedRadioButtonId() method of RadioGroup directly inside onTextChanged() method of TextWatcher like below:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    if (modeGroup.getCheckedRadioButtonId() == R.id.rb_mode_1) {

        String[] nowTyping = input.getText().toString().split(" ");
        ArrayList<String> wordArray = new ArrayList<>();
        for (String word : nowTyping) {
            wordArray.add(0, word);
        }
        String invertedSentence = TextUtils.join(" ", wordArray);
        output.setText(invertedSentence);
    }
    else if (modeGroup.getCheckedRadioButtonId() == R.id.rb_mode_2){

           //Do mode 2 stuffs...
    }
     ....
}

Sometimes we just don't dig enough before asking other to do for us.

esQmo_
  • 1,464
  • 3
  • 18
  • 43