Yonatan Balas
Yonatan Balas

Reputation: 35

Android | Radio buttons wont do anything and keep pressed

Im trying to make a currency converter homework (i am new to programming)

made everything but the radio button is keeped pressed and isnt doing nothing (not puting text into the TextView and the radio buttons is locked on "pressed" mode

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

    mResult = (TextView) findViewById(R.id.result);
    mToConvert = (EditText) findViewById(R.id.toConvert);
    mRadioGroup = (RadioGroup) findViewById(R.id.radioG);
    mDollar = (RadioButton) findViewById(R.id.dollar);
    Meuro = (RadioButton) findViewById(R.id.euro);

    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup rGroup, int checkedId)
        {

            switch (mRadioGroup.getCheckedRadioButtonId())
            {
            case R.id.dollar:
            Double dollarConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
            double price = dollarConvert * 1.28;
            mDollar.setChecked(true);
            Meuro.setChecked(false);
            String result = mToConvert.getText().toString();
            mResult.setText(result + price);
            break;

            case R.id.euro:
                Double euroConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
                double value = euroConvert * 1.28;
                mDollar.setChecked(false);
                Meuro.setChecked(true);
                String result1 = mToConvert.getText().toString();
                mResult.setText(result1 + value);
                break;

                default:;
            }
        }
    });


}

}

Upvotes: 1

Views: 139

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

you will need to set RadioGroup.setoncheckedchangelistener for RadioGroup to do some Action when check changes event fire.

mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    public void onCheckedChanged(RadioGroup rGroup, int checkedId)
    {
              //do your code here
    }
});

Upvotes: 1

Related Questions