i am trying to add the add text changed listener into my code so when
i edit the text it is automaticly converting the currency to the SELECTED RADIO BUTTON (dollar,euro) without the need to push one of the radio buttons to make it show the answer in the textview.
i have no idea how to do this
i am new to programming searched in google didnt find the answer
package com.gardana.superh;
public class ConvertActivity extends Activity {
TextView mResult;
EditText mToConvert;
RadioGroup mRadioGroup;
RadioButton mDollar, Meuro;
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 mradRadioGroup, int checkedId)
{
switch (checkedId)
{
case R.id.dollar:
Double dollarConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
double price = dollarConvert * 3.76;
mDollar.setChecked(true);
Meuro.setChecked(false);
mResult.setText("$"+price);
break;
case R.id.euro:
Double euroConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
double value = euroConvert * 5;
mDollar.setChecked(false);
Meuro.setChecked(true);
mResult.setText("€" + value);
Meuro.setChecked(false);
break;
default:;
}
}
});
}
}
I would suggest you break down your code to make it easier to do. First you need to add your two listeners so the conversions are preformed when needed.
Because you’re basically going to do the same work in two places, I suggest you create a method which performs the calculation for you. This then can be called whenever you liked to update the conversion.
I’ve also included a suggestion above about how to rework your switch/case to use less code duplication, because theres no point writing the same thing twice.