Ranjan Srinivas Ranjan Srinivas - 6 months ago 18
Android Question

Both Radio Buttons are active

I'm kind of new to android and am trying to implement a basic app for conversions to Celsius or Fahrenheit.

Here is the basic code

public class MainActivity extends AppCompatActivity {

EditText temp ;
RadioButton celsius;
RadioButton fahrenheit;
Button button;

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

temp = (EditText) findViewById(R.id.question_text);
final RadioGroup options = (RadioGroup) findViewById(R.id.options);
celsius = (RadioButton) findViewById(R.id.celsius);
fahrenheit = (RadioButton) findViewById(R.id.fahrenheit);
button = (Button) findViewById(R.id.convert);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view){
int button1 = 1;
int button2 = 2;
celsius.setId(button1);
fahrenheit.setId(button2);
int number = Integer.parseInt(temp.getText().toString());
TextView text = (TextView) findViewById(R.id.answer_text);
double answer;
int id = options.getCheckedRadioButtonId();
if(id == -1){
text.setText("Please select an option");
}
else if(id == 2){
answer = number*1.8 + 32; //Convert to Fahrenheit
text.setText("" + answer);
}
else{
answer = (number-32)/1.8;
text.setText("" + answer); //Convert to Celsius
}
}
});
}


}

I implemented a RadioGroup so that only one of the buttons can be selected

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.temperatureconverter.MainActivity">

<EditText
android:id="@+id/question_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the temperature."
android:inputType="number"
android:paddingLeft="18dp"
android:paddingTop="18dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="18dp"
android:paddingTop="18dp"
android:text="Convert to?"
android:textAllCaps="true"
android:textSize="24sp"
android:typeface="sans" />


<RadioGroup
android:id="@+id/options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="18dp"
android:orientation="horizontal">

<RadioButton
android:id="@+id/celsius"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Celsius"
android:textSize="20sp" />

<RadioButton
android:id="@+id/fahrenheit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Fahrenheit"
android:textSize="20sp" />
</RadioGroup>

<Button
android:id="@+id/convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONVERT"
android:textSize="20sp"
android:typeface="monospace" />

<TextView
android:id="@+id/answer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


The program works all right the first time. But if i want to convert it again both the radio buttons are being selected, which is puzzling. I'm really new to this, so i'd like to know what's going on...

Answer

Comment out the following lines.

celsius.setId(button1);
fahrenheit.setId(button2);

This was basically the issue.

Moreover, this will solve the issue.

button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view){
                    int number = Integer.parseInt(temp.getText().toString());
                    TextView text = (TextView) findViewById(R.id.answer_text);
                    double answer;
                    int id=-1;
                    if(fahrenheit.isChecked())
                    {
                        id=1;
                    }
                    else if(celsius.isChecked())
                    {
                        id=2;
                    }
                    System.out.println("Selected option is "+id);
                    if(id == 1){
                        answer = number*1.8 + 32;     //Convert to Fahrenheit
                        text.setText("" + answer);
                    }
                    else if (id ==2){
                        answer = (number-32)/1.8;
                        text.setText("" + answer);    //Convert to Celsius
                    }
                    if(id == -1){
                        text.setText("Please select an option");
                    }

                }
            });
Comments