radio button reselecting not working properly in android,new in development - android

I'm having a set of 9 radio Buttons(a group of 3 each),user has to select 3 (1 of each group)
on right selection a increment in score while on wrong selection decrement in score,but problem is that if all 3 correct ans are selected score is coming right else not ,also it is not responding to re selecting of another radio button of the group.On submit button i have written the code, I am new in development so kindly guide
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.qa:cb.setChecked(false);
cc.setChecked(false);
cd.setChecked(false);
s[0] = false;
break;
case R.id.qb:
ca.setChecked(false);
cc.setChecked(false);
cd.setChecked(false);
s[0] = false;
break;
case R.id.qc:
cb.setChecked(false);
ca.setChecked(false);
cd.setChecked(false);
s[0] = true;
break;
}
public void onClick(View v) {
score = 0;
for (int i = 0; i < 3; i++) {
if (s[i] == true)
score++;
else
score--;
}
where s is boolean s[] = new boolean[];
I am new in development so not able to sort out problem

The code sample below will add the radiobuttons dynamically to a group so that you can arrange the buttons in any way you like using any layouts.
Layout : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RadioButton
android:id="#+id/rbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="A" />
<RadioButton
android:id="#+id/rbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="B" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RadioButton
android:id="#+id/rbtn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="C" />
<RadioButton
android:id="#+id/rbtn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="D" />
</LinearLayout>
</LinearLayout>
Code : MainActivity.xml
public class MainActivity extends Activity {
private static final String GROUP_A = "GroupA";
private static final String GROUP_B = "GroupB";
private List<RadioButton> radioButtons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioButton rbtn1 = (RadioButton) findViewById(R.id.rbtn1);
RadioButton rbtn2 = (RadioButton) findViewById(R.id.rbtn2);
RadioButton rbtn3 = (RadioButton) findViewById(R.id.rbtn3);
RadioButton rbtn4 = (RadioButton) findViewById(R.id.rbtn4);
addRadioButtonToGroup(rbtn1, GROUP_A);
addRadioButtonToGroup(rbtn2, GROUP_A);
addRadioButtonToGroup(rbtn3, GROUP_A);
addRadioButtonToGroup(rbtn4, GROUP_B);
}
private void addRadioButtonToGroup(RadioButton rbtn, String group) {
rbtn.setTag(group);
if (radioButtons == null) {
radioButtons = new ArrayList<RadioButton>();
}
radioButtons.add(rbtn);
rbtn.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
for (int i = 0; i < radioButtons.size(); i++) {
RadioButton radioButton = radioButtons.get(i);
if (radioButton
.getTag()
.toString()
.equalsIgnoreCase(
buttonView.getTag().toString())) {
if (!radioButton.equals(buttonView)) {
radioButton.setChecked(false);
}
}
}
}
}
});
}
}
Here..First 3 radio buttons will belong to the group A and other to Group B, which makes sure one radio button in a group can be checked at a time. We also dont need the RadioGroup and this code gives the control to arrange Radio Buttons in anyway you like.

Use RadioGroup instead of checking and un checking the RadioButtons manually.
If You put 3 radio buttons in a radio group, you will be able to check a radio button at one time, RadioGroup will manage it.
Check the sample below : On Submit button click it will calculate the score.
layout : main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First Question" />
<RadioGroup
android:id="#+id/rg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A" />
<RadioButton
android:id="#+id/rbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B" />
<RadioButton
android:id="#+id/rbtn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Second Question" />
<RadioGroup
android:id="#+id/rg2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="P" />
<RadioButton
android:id="#+id/rbtn5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Q" />
<RadioButton
android:id="#+id/rbtn6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="R" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Third Question" />
<RadioGroup
android:id="#+id/rg3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X" />
<RadioButton
android:id="#+id/rbtn8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y" />
<RadioButton
android:id="#+id/rbtn9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z" />
</RadioGroup>
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</ScrollView>
Activity :
package com.example.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup rg1;
private RadioGroup rg2;
private RadioGroup rg3;
private OnClickListener btnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int answerCount = 0;
if (rg1.getCheckedRadioButtonId() == R.id.rbtn1)
answerCount++;
if (rg2.getCheckedRadioButtonId() == R.id.rbtn5)
answerCount++;
if (rg3.getCheckedRadioButtonId() == R.id.rbtn9)
answerCount++;
Toast.makeText(MainActivity.this, String.valueOf(answerCount),
Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rg1 = (RadioGroup) findViewById(R.id.rg1);
rg2 = (RadioGroup) findViewById(R.id.rg2);
rg3 = (RadioGroup) findViewById(R.id.rg3);
Button btnSumbit = (Button) findViewById(R.id.btnSubmit);
btnSumbit.setOnClickListener(btnClickListener);
}
}

Related

How to select Multiple options from RadioGroup Buttons?

it is possible to select multiple options from radiogroup buttons in android plz give some solution.
this my code in this code i can only select one radio button from the group plz give solution for select more then one selection.
<RelativeLayout 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: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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="#+id/text"
android:text="#string/ChoiceText" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:id="#+id/myRadioGroup"
android:background="#abf234"
android:checkedButton="#+id/sound" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sound"
android:text="#string/Sound" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vibration"
android:text="#string/Vibration" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/silent"
android:text="#string/Silent" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/myRadioGroup"
android:layout_marginTop="10dp"
android:id="#+id/chooseBtn"
android:text="#string/Choose" />
</RelativeLayout>
Activity
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton sound, vibration, silent;
private Button button;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.silent) {
Toast.makeText(getApplicationContext(), "choice: Silent",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.sound) {
Toast.makeText(getApplicationContext(), "choice: Sound",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "choice: Vibration",
Toast.LENGTH_SHORT).show();
}
}
});
sound = (RadioButton) findViewById(R.id.sound);
vibration = (RadioButton) findViewById(R.id.vibration);
silent = (RadioButton) findViewById(R.id.silent);
textView = (TextView) findViewById(R.id.text);
button = (Button)findViewById(R.id.chooseBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
// find which radioButton is checked by id
if(selectedId == sound.getId()) {
textView.setText("You chose 'Sound' option");
} else if(selectedId == vibration.getId()) {
textView.setText("You chose 'Vibration' option");
} else {
textView.setText("You chose 'Silent' option");
}
}
});
}
}
Instead of using a radio group, just use some collection of radio buttons. RadioGroup is meant for selecting one from the list of radio buttons. So your xml will look like this after the changes
<RelativeLayout 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: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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="#+id/text"
android:text="#string/ChoiceText" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sound"
android:text="#string/Sound" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vibration"
android:text="#string/Vibration" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/silent"
android:text="#string/Silent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/myRadioGroup"
android:layout_marginTop="10dp"
android:id="#+id/chooseBtn"
android:text="#string/Choose" />
See that the radio group is removed.
Please use checkbox if you want to have multiple entries. :)
http://developer.android.com/reference/android/widget/CheckBox.html

How to get selected radio button value from question1 activity page to next page

i am developing a quiz using mobile application. currently, i am having problems on how to retrieve selected radio button from previous page. i want the selected radio button value to be displayed on the next page. below is my code in xml and java.
question1. java
public class question1 extends Activity {
private Button Button2;
public RadioButton r1,r2,r3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question1);
r1 = (RadioButton)findViewById(R.id.rb1);
r2 = (RadioButton)findViewById(R.id.rb2);
r3 = (RadioButton)findViewById(R.id.rb3);
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),question2.class);
intent.putExtra("r1",r1.isSelected());
intent.putExtra("r2",r2.isSelected());
intent.putExtra("r3",r3.isSelected());
startActivityForResult(intent,0);
}
}));
}
}
this is my code in question1.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="452dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:src="#drawable/clay" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="YANG MANA SATUKAH DI ANTARA AYAT-AYAT BERIKUT YANG BENAR ?"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton2"
android:text="A.Tanah liat yang leper lebih berat" />
<RadioButton
android:id="#+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton3"
android:text=" B. Kedua-dua tanah liat adalah sama berat" />
<RadioButton
android:id="#+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/radioButton2"
android:layout_marginBottom="14dp"
android:text="C.Bola tanah liat lebih berat" />
</RadioGroup>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#drawable/nextnext" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
this is my code for the second page that i want the selected radio button value to be displayed.
question2. java
public class question2 extends Activity
{
private Button Button2;
public RadioButton r1,r2,r3,r4,r5;
public RadioGroup rg1,rg2;
public String a2,b2,total;
public TextView output ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question2);
output = (TextView)findViewById(R.id.textView5);
r1 = (RadioButton)findViewById(R.id.rb1);
r2 = (RadioButton)findViewById(R.id.rb2);
r3 = (RadioButton)findViewById(R.id.rb3);
r4 = (RadioButton)findViewById(R.id.rb4);
r5 = (RadioButton)findViewById(R.id.rb5);
rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
rg2 = (RadioGroup)findViewById(R.id.radioGroup2);
Object RadioButton;
r1= (RadioButton).getValue();
r2 =(RadioButon).getValue();
r3 =(RadioButon).getValue();
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent(view.getContext(),question3.class);
startActivityForResult(intent,0);
}
}
));
{
switch(rg2.getCheckedRadioButtonId()){
case R.id.rb4:
if (r4.isChecked()){
//a2 =0;
}
break;
case R.id.rb5:
if (r5.isChecked()){
//b2 = 1;
}
break;
//total = rg1.getCheckedRadioButtonId()+rg2.getCheckedRadioButtonId() ;
}}
//receive the arguments from the previous Activity
Bundle extras = getIntent().getExtras();
if (extras==null) {
return;
}
output = (TextView) findViewById(R.id.textView5);
Bundle data = this.getIntent().getExtras();
r1=data.getValue(r1);
r2=data.getValue(r2);
r3=data.getValue(r3);
double result = 0;
if (r1 == "")
{
result+ = 1;
}
else if (r2 == "true"){
result+ = 0;}
else if (r3=="true")
{
result+ = 0;
}
else {
result + =0;
}
}
}
}
question2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="452dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_weight="0.13"
android:src="#drawable/soklan2" />
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4" >
<RadioButton
android:id="#+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A. Ya" />
<RadioButton
android:id="#+id/rb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioGroup1"
android:layout_below="#+id/radioGroup1"
android:text="B. Tidak" />
</RadioGroup>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup2"
android:layout_marginTop="20dp"
android:text="Result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#drawable/nextnext" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
I would like to know, how to retrieve the value from question 1 and displayed it on question2 page.
you could use:
r1.setChecked(getIntent().getExtras().getBoolean("r1"));
r2.setChecked(getIntent().getExtras().getBoolean("r2"));
r3.setChecked(getIntent().getExtras().getBoolean("r3"));
You may also need to change the code where you put the values:
intent.putExtra("r1",r1.isChecked());
intent.putExtra("r2",r2.isChecked());
intent.putExtra("r3",r3.isChecked());
You should also probably look at this code:
Object RadioButton;
r1 = (RadioButton).getValue();
r2 = (RadioButon).getValue();
r3 = (RadioButon).getValue();
I have no idea what you are trying to accomplish here. And it should probably be removed to keep the refferences to your RadioButton Objects
In you second activity , get the values from intent extras:
Intent intent = getIntent();
boolean r1Value = intent.getBooleanExtra("r1",false);
r1.setChecked(r1Value );

Radio Button is selected automatically in android

quiz_list.xml : My xml file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="20sp"
android:textStyle="bold"
android:paddingTop="4dip"
android:paddingBottom="1dip" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Radio Group"
>
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="bottom|center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/loadPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Previous"
android:visibility="invisible" />
<Button
android:id="#+id/loadNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Next" />
</LinearLayout>
I have a list of questions and radio buttons have the options for it.
The problems that I face are
1. When i load the next list a button is already selected.
2. When i load the previous list a different button is selected instead of the one i selected.
This is my code containing the questions as a json.
String json = "[{\"question\": \"What?\", \"option1\": \"alpha\" , \"option2\": \"beta\" , \"option3\": \"gamma\" , \"option4\": \"alpha\"},{\"question\": \"What is your name ?\", \"option1\": \"Dinesh\" , \"option2\": \"Boopesh\" , \"option3\": \"Srinath\" , \"option4\": \"JK\"},{\"question\": \"What is the capital of India?\", \"option1\": \"Delhi\" , \"option2\": \"Bombay\" , \"option3\": \"Calcutta\" , \"option4\": \"Chennai\"}]";
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(json).getAsJsonArray();
for(JsonElement obj : array) {
Quiz quiz = gson.fromJson(obj,Quiz.class);
String question = quiz.getQuestion();
System.out.println("Question: "+question);
quizList.add(quiz);
}
I'm adding the questions using a customadapter.
How can i change it to make it work ??
Any Ideas ??? Pls let me know...
Thanks Nishit.
try this
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
boolean hack = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.r1);
radioButton2 = (RadioButton) findViewById(R.id.r2);
radioButton3 = (RadioButton) findViewById(R.id.r3);
OnClickListener radioClickListener = new OnClickListener()
{
public void onClick(View v)
{
if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack)
{
radioGroup.clearCheck();
}
else
{
hack = true;
}
}
};
OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
hack = false;
}
};
radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton1.setOnClickListener(radioClickListener);
radioButton2.setOnClickListener(radioClickListener);
radioButton3.setOnClickListener(radioClickListener);
}

Android : Managing two buttons

I have a problem concerning setting a listener that would execute different action based on the clicked button.
The compiler doesn't detect any error on the syntax, but the problem is that when I simulate my app on the emulator, it pops up the window asking for "Force Close".
Here is my Java file :
package tp.imc.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class IMCCalculatorActivity extends Activity {
EditText poids,taille;
Button boutton1,boutton2;
TextView text1;
Boolean k=true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
poids=(EditText) findViewById(R.id.poids);
taille=(EditText) findViewById(R.id.taille);
boutton1 =(Button) findViewById(R.id.boutton1);
text1=(TextView) findViewById(R.id.text1);
boutton1.setOnClickListener(clicker);
boutton2.setOnClickListener(clicker);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener clicker = new View.OnClickListener(){
public void onClick (View v){
if( v == boutton1 )
{
String a,b;
Double r;
a=poids.getText().toString();
b=taille.getText().toString();
Double zero=Double.parseDouble(b);
a=poids.getText().toString();
b=taille.getText().toString();
if (zero==0)
{
text1.setText("Erreur ! Division par 0.");
}
else {
r=(Double.parseDouble(a))/(Double.parseDouble(b)*Double.parseDouble(b));
if (r<16.5) text1.setText("Famine.");
else if (r>16.5 && r<18.5) text1.setText("Maigreur.");
else if (r>=18.5 && r<25) text1.setText("Coplulence nomrale.");
else if (r>=25 && r<30) text1.setText("Surpoids.");
else if (r>=30 && r<35) text1.setText("Obésité modérée.");
else if (r>=35 && r<40) text1.setText("Obésité sévère.");
else if (r>=40) text1.setText("Obésité morbide ou massive.");
}
}
else if( v == boutton2 )
{
poids.setText("");
taille.setText("");
text1.setText("");
}
}
};
}
And here is my XML file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/poids"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/poids"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/poids"
android:lines="1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/taille"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/taille"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/taille"
android:lines="1"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/metre"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/centimetre"
/>
</RadioGroup>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mega"
android:checked="false"
/>
<Button
android:id="#+id/boutton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/compute" />
<Button
android:id="#+id/boutton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/raz" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/resultat"
/>
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text"
/>
</LinearLayout>
You don't define boutton2, it is null. Add this:
boutton2 = (Button) findViewById(R.id.boutton2);
boutton2.setOnClickListener(clicker);
Since boutton1 and boutton2 don't have common code between them you could simply create two different listeners.
Lastly, this line doesn't look right.
if( v == boutton1 )
You are trying to compare a View to a Button which may not work. Try this:
if( v.getId() == boutton1.getId() )

multiply with zero, double no number

I am trying to build a calculator for android, i am trying to multiply two numbers.
If i give number 1 and leave number 2 open, i want to get zero as an answer.
I first thought it could be the problem of not having a value for the double.
So i tried: if (Double.isNaN(number1)) { number1=0; }
But it did not work.
How do i make this happen?
R.layout.main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 1 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="#+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<!-- 2 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="#+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<!-- multiply -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="multiply:" />
<TextView
android:id="#+id/multiplydisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="20dp" />
</LinearLayout>
<Button
android:id="#+id/btncalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"/>
<Button
android:id="#+id/btnreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset" />
</LinearLayout>
TipcalcActivity.java
package com.tip.calc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class TipcalcActivity extends Activity {
private EditText number1;
private EditText number2;
private TextView multiplydisplay;
private Button btncalculate;
private Button btnreset;
private double number1calc = 0;
private double number2calc = 0;
private double multiply = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls() {
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
multiplydisplay = (TextView)findViewById(R.id.multiplydisplay);
btncalculate = (Button)findViewById(R.id.btncalculate);
btnreset = (Button)findViewById(R.id.btnreset);
btncalculate.setOnClickListener(new Button.OnClickListener() { public void
onClick (View v){ calculate(); }});
btnreset.setOnClickListener(new Button.OnClickListener() { public void
onClick (View v){ reset(); }});
}
private void calculate() {
number1calc=Double.parseDouble(number1.getText().toString());
number2calc=Double.parseDouble(number2.getText().toString());
multiply=(number1calc*number2calc);
multiplydisplay.setText(Double.toString(multiply));
}
private void reset() {
multiplydisplay.setText("");
number1.setText("");
number2.setText("");
}
}
you should just check that the content of the textfield of the value is different than "". If not, just assign the value 0.
In calculate :
private void calculate() {
number1calc=(number1.getText().toString() != "" ) ? Double.parseDouble(number1.getText().toString()) : 0;
number2calc=(number2.getText().toString() != "" ) ? Double.parseDouble(number2.getText().toString()) : 0;
multiply=(number1calc*number2calc);
multiplydisplay.setText(Double.toString(multiply));
}
if(numberOne.getText().toString().trim().length() < 1 || numberTwo.getText().toString().length() < 1){
multiplyDisplay.setText("0");
}
else{
//default handling
}
If the edittext is left blank, this is obviously not a number(NaN). Doing any operation with NaN in Java will cause the result of this operation to be NaN.

Categories

Resources