1

SetHint updates TextEdit only after OnClickListener (after clicking on button) In my case, when user type some text in TextEdit, and activates toggle, Hint will change only after he/she submit button was pressed. If I for example will press on Toggle, hint will be the same as original.

I want to make SetHint work just after toggle set on or off. Want to change Hint in TextEdit to "Custom Text" just after I pressed toggle. Otherwise I want TextEdit stay with original hint text. Not sure if I need change it in else, since I use same hint text in layout.

public class MainActivity extends Activity {
    private Button button;
    private TextEdit text;
    private ToggleButton toggle;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

text = (TextEdit) findViewById(R.id.myid);
toggle = (ToggleButton) findViewById(R.id.myid2);
        if (toggle.isChecked()) {
            text.setHint("Custom Text");
        } else {
            text.setHint("Text");
        }
            button.setOnClickListener(new View.OnClickListener() {
...
Anonymous
  • 11
  • 1

3 Answers3

0

You can do it like

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // change the hint text when click in button
        if (toggle.isChecked()) {
            text.setHint("Custom Text");
        } else {
            text.setHint("Text");
        }
    }
});
Linh
  • 57,942
  • 23
  • 262
  • 279
  • It works that way now. (I don't know why, since I wrote it outside OnClickListener). I want hint to be changed just after I pressed toggle. – Anonymous Jun 22 '16 at 17:43
  • @Anonymous if you think this solve your problem, please mark it as correct answer. thank you – Linh Jun 22 '16 at 17:53
0

You are currently checking for the initial state of the toggle only. Once you click it, edit text won't be changing the hint.

You're likely looking for a usage of "setOnCheckedChangeListener" on the button. Its implementation is similar to the onClickListener.

See this question for more details.

Edit: You said you wanted the hint to change only after the "submit" button is pressed, but you also said you wanted it to change when the "toggle" is changed. Whichever method you use (onCheck or onClick) will depend on what you actually want.

Community
  • 1
  • 1
Allan W
  • 2,791
  • 4
  • 23
  • 41
0
toggle = (ToggleButton) findViewById(R.id.myid2);
        final CompoundButton.OnCheckedChangeListener toggleButtonChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (toggle.isChecked()) {
                    text.setHint("Custom Text");
                } else {
                    text.setHint("Text");
                }

            }
        };
        toggle.setOnCheckedChangeListener(toggleButtonChangeListener);
Anonymous
  • 11
  • 1