0

I am not sure if it is even possible but if it is I would appreciate your help! I need to change the textHint depending which radioButton is checked Radio Buttons. For example, I want the editText Hint to display "Miles" if the US radioButon is checked; and when the radioButton Metric is checked I want the textHint to display "Kilometers."

I attempted to solve this multiple time but constantly fail.

Tanks ahead of time!

Eugene H
  • 3
  • 4

2 Answers2

2

You can use setHint(hint); on your EditText to change the Hint. You can use a listener on your radioButton to detect when one is selected.

Rogue
  • 751
  • 1
  • 17
  • 36
  • I see your answer, Good one. – zIronManBox May 18 '14 at 15:32
  • THank for getting back so fast. I tried to setHint in the .java of the activity and I couldnt get it to work. WHat should I put in the .XML of the radioButton. And should I do a radioroup listener or radioButton? – Eugene H May 18 '14 at 15:38
  • Your XML file only needs to specify IDs on your EditText and RadioGoup. The listener you need is an OnCheckedChangeListener, and you have to apply it on your RadioGroup. Just like @Lal said. :) – Rogue May 18 '14 at 15:42
2
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1); 
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
         // checkedId is the RadioButton selected 
        switch (checkedId) {
            case R.id.radioButtonUS:
                 Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show(); 
                  Distance.setHint("Miles");
                  break; 
            case R.id.radioButtonMetric: 
                  Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show(); 
                 Distance.setHint("Kilometers"); 
                  break; 
        } 
      } 
 });
Lal
  • 14,726
  • 4
  • 45
  • 70
  • RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // checkedId is the RadioButton selected switch (checkedId) { case R.id.radioButtonUS: if (US.isChecked()) US.setHint("Miles"); break; case R.id.radioButtonMetric: if (Metric.isChecked()) Metric.setHint("Kilometers"); break; } } }); This is what I have inside the .Java. It is not doing anything when I run it. – Eugene H May 18 '14 at 15:47
  • RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // checkedId is the RadioButton selected switch (checkedId) { case R.id.radioButtonUS: if (US.isChecked()) Distance.setHint("Miles"); break; case R.id.radioButtonMetric: if (Metric.isChecked()) Distance.setHint("Kilometers"); break; } } }); This is what I have inside the .Java. It is not doing anything when I run it. – Eugene H May 18 '14 at 15:57
  • No it didnt work. I forgot to add the propper editText under if (checked) but it still didnt work. – Eugene H May 18 '14 at 15:58
  • Set a `Toast` inside the each cases and check if the toasts are displayed when radio button is selected,.. – Lal May 18 '14 at 15:58
  • Sorry! It didnt work. but I am going to try if(checked) – Eugene H May 18 '14 at 16:23
  • case R.id.radioButtonMetric: if (US.isChecked()) Distance.setHint("Kilometers"); Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show(); break; – Eugene H May 18 '14 at 16:24
  • case R.id.radioButtonMetric: if (Metric.isChecked()) Distance.setHint("Kilometers"); Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show(); break; } – Eugene H May 18 '14 at 16:26
  • Distance = (EditText) findViewById(R.id.editTextDistance); US = (RadioButton) findViewById(R.id.radioButtonUS); Metric = (RadioButton) findViewById(R.id.radioButtonMetric); rg1 = (RadioGroup) findViewById(R.id.radioGroup1); These are my id's – Eugene H May 18 '14 at 16:29
  • Would you rewrite the code with my Id's so I can see if it works and what I am doing wrong? – Eugene H May 18 '14 at 16:36
  • The only issue I have now is that I have to click the radioButtonUS then radioButtonMetric Then Back to radioButtonUS for the hint to show – Eugene H May 18 '14 at 16:52
  • The only issue I have now is that I have to click the radioButtonUS then radioButtonMetric Then Back to radioButtonUS for the hint to show – Eugene H May 18 '14 at 16:54
  • for that i think you set radioButtonUS checked and set hint for that in onCreate().. – Lal May 18 '14 at 16:54
  • I got it to work with what you had posted. Again thank you very much, Lal. – Eugene H May 18 '14 at 17:01
  • Glad that it worked @EugeneH..If it helped you kindly mark my answer as accepted..Please... – Lal May 18 '14 at 17:02
  • Pretty sure I accepted your answer. This is my first question on OverFlow – Eugene H May 18 '14 at 17:06