Reset results and TextUtil - java

So, I got a feedback from reviewer and fixed most of my problems but there are two problems that I can’t find solution (I will use bold font to highlight them) the first problem comes after I click result button more than one time because the previous result is added. I know that I should add all the methods checkQuestionOne to checkQuestion6 in the button method but this is not possible so far because the method needs (View or view value) which is not working and I don’t know why so I decided not to include the four Radio buttons methods in it. Second when the checkQuestionFive is blank or there is no value the app crashes I was told to use if (!TextUtils.isEmpty(gettingQuestionFive.getText())), then I add Toast message into the if statement but it still crashes. Any help will be appreciated!!
Thank you!
Java code:
public class MainActivity extends AppCompatActivity {
int health = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkQuestionOne(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_one_yes:
if (checked)
health += 1;
break;
}
}
public void checkQuestionTwo(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_two_yes:
if (checked)
health ++;
break;
}
}
public void checkQuestionThree(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_three_no:
if (checked)
health ++;
break;
}
}
public void checkQuestionFour(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_four_no:
if (checked)
health ++;
break;
}
}
**public void checkQuestionFive**() {
EditText gettingQuestionFive = findViewById(R.id.sleep_hours);
if (!TextUtils.isEmpty(gettingQuestionFive.getText())){
Toast displayError = Toast.makeText(this, "You missed question 5, plase don't leave it blank", Toast.LENGTH_LONG);
displayError.show();
}
int answerQuestionFive = Integer.parseInt(gettingQuestionFive.getText().toString());
if (answerQuestionFive >= 7) {
health ++;
}
}
public void checkQuestionSix() {
CheckBox checkBoxOneA = findViewById(R.id.fruits_check_box);
CheckBox checkBoxOneB = findViewById(R.id.chips_check_box);
CheckBox checkBoxOneC = findViewById(R.id.candy_check_box);
CheckBox checkBoxOneD = findViewById(R.id.all_three_check_box);
if (checkBoxOneA.isChecked() && !checkBoxOneB.isChecked() && !checkBoxOneC.isChecked() && !checkBoxOneD.isChecked()) {
health++;
}
}
**public void overView(View view)**{
checkQuestionFive();
checkQuestionSix();
Toast displayToast = Toast.makeText(this, health + " is your score. If is 4 or above, you have a healthy life", Toast.LENGTH_LONG);
displayToast.show();
health = 0;
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context="com.example.martinevtimov.quizapp2.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group1" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1. Do you eat healty?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_one_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionOne" />
<RadioButton
android:id="#+id/question_one_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionOne" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group2" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2. Do you work out?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_two_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionTwo" />
<RadioButton
android:id="#+id/question_two_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionTwo" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group3" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3. Do you drink?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_three_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionThree" />
<RadioButton
android:id="#+id/question_three_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionThree" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group4" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4. Do you smoke?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_four_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionFour" />
<RadioButton
android:id="#+id/question_four_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionFour" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5. How many hours do you sleep?"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="#+id/sleep_hours"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type here"
android:inputType="number" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6. What is a healthy snack?"
android:textSize="30sp"
android:textStyle="bold" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fruits"
android:id="#+id/fruits_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chips"
android:id="#+id/chips_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Candy"
android:id="#+id/candy_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="All three"
android:id="#+id/all_three_check_box" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="overView"
android:text="Check my health"
android:textAllCaps="true" />
</LinearLayout>
</ScrollView>

You can see the Logcat and the message error I have. The purpose of this app is to make a quiz app with 3 different inputs (EditText, Radio Buttons, and CheckBoxes). When the EditText is left blank, a crash appears. It seems there is some problem with the onClick attribute in your code but I can figure it out. The second problem comes from clicking the check button more than one time than more points are added to the previous points, I tried to add all the question methods into one and then set the global health variable to 0 but is not really working. Also, take a look of the two pictures below my code.
Screen Picture 1
Screen Picture 2

Related

Why do i always see the last id in View.OnClickListener?

Why do I always see the last id no matter which text I click?
I would like to see the right fragment displayed in the layout that i created according to the relevant text clicked. I always see the 'alternatives' fragment.
I understood that tags could be helped, but I could not figure out how to use them.
In addition, I tried to use diffrent versions of FragmentManager and FragmentTransaction and even to remove the switch and call each setOnClickListener of textview separately but nothing helped.
This is my Activity file:
IngredientsFragment ingredientsFragment;
FavouritesFragment favouritesFragment;
FeedbacksFragment feedbacksFragment;
Write_Feedback_Fragment writeFeedbackFragment;
AlternativesFragment alternativesFragment;
TextView ingredients;
TextView favourites;
TextView feedbacks;
TextView alternatives;
TextView writeFeedback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
window=this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
ingredients= (TextView) findViewById(R.id.ingredients_option);
favourites= (TextView) findViewById(R.id.favorites_option);
feedbacks= (TextView) findViewById(R.id.watch_feedback_option);
writeFeedback= (TextView) findViewById(R.id.add_feedback_option);
alternatives= (TextView) findViewById(R.id.alternatives_option);
ingredients.setOnClickListener(this);
favourites.setOnClickListener(this);
feedbacks.setOnClickListener(this);
writeFeedback.setOnClickListener(this);
alternatives.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.ingredients_option:
ingredientsFragment= new IngredientsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,ingredientsFragment).commit();
break;
case R.id.favorites_option:
favouritesFragment= new FavouritesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,favouritesFragment).commit();
break;
case R.id.watch_feedback_option:
feedbacksFragment= new FeedbacksFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,feedbacksFragment).commit();
break;
case R.id.add_feedback_option:
writeFeedbackFragment= new Write_Feedback_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,writeFeedbackFragment).commit();
break;
case R.id.alternatives_option:
alternativesFragment= new AlternativesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,alternativesFragment).commit();
break;
}
}
}
This is my XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProductDetails">
<RelativeLayout
android:id="#+id/recommendation_template"
android:layout_width="match_parent"
android:layout_height="142dp"
android:background="#android:color/white">
<ImageView
android:id="#+id/registartion_arrow"
android:layout_width="45dp"
android:layout_height="34dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_arrow_forward_black_24dp" />
<TextView
android:id="#+id/rec_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:text="Recommendation:"
android:textSize="25dp"
android:textStyle="bold"
android:textColor="#android:color/black"/>
<ImageView
android:id="#+id/vi_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rec_tv"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="26dp"
android:paddingLeft="80dp"
android:src="#drawable/ic_check" />
<TextView
android:id="#+id/vi_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vi_image"
android:layout_marginLeft="4dp"
android:layout_marginTop="-29dp"
android:layout_toRightOf="#id/vi_image"
android:text="This is for you"
android:textColor="#android:color/black"
android:textSize="20dp" />
<ImageView
android:id="#+id/not_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vi_image"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="3dp"
android:paddingLeft="80dp"
android:src="#drawable/ic_do_not" />
<TextView
android:id="#+id/not_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/not_image"
android:layout_marginLeft="5dp"
android:layout_marginTop="-27dp"
android:layout_toRightOf="#id/not_image"
android:text="Not for you"
android:textColor="#android:color/black"
android:textSize="20dp" />
</RelativeLayout>
<HorizontalScrollView
android:id="#+id/recommendation_scrollView"
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_below="#id/recommendation_template"
android:layout_marginTop="7dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/recommendation_template"
style="?android:attr/borderlessButtonStyle">
<TextView
android:id="#+id/ingredients_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/ingredients"
android:text="Ingredients" />
<TextView
android:id="#+id/favorites_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_star"
android:paddingLeft="80dp"
android:text="Favorites" />
<TextView
android:id="#+id/watch_feedback_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/customer_review"
android:paddingLeft="145dp"
android:text="Feedbacks" />
<TextView
android:id="#+id/add_feedback_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/write_feedback"
android:paddingLeft="220dp"
android:text="Write feedback" />
<TextView
android:id="#+id/alternatives_option"
android:layout_width="408dp"
android:layout_height="90dp"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_alternatives"
android:paddingLeft="320dp"
android:text="Alternatives" />
</FrameLayout>
</HorizontalScrollView>
<ScrollView
android:id="#+id/scrollView_container"
android:layout_width="match_parent"
android:layout_height="499dp"
android:layout_below="#id/recommendation_scrollView">
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="463dp">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Your code should work like that. The problem is your layout. See the textviews in your FrameLayout. Your defining a paddingLeft and layout_width so, that your textview alternatives_option is overlaying all the other textviews. Thats why in the onclick you always get the id of that view.
I suggest doing a tutorial about XML layout.
<TextView
android:id="#+id/alternatives_option"
android:layout_width="408dp"
android:layout_height="90dp"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_alternatives"
android:paddingLeft="320dp"
android:text="Alternatives" />

Showing text from string through TextView after pressing RadioButton

I'm trying to show automaticly "This is the correct answer" or "Try again" just right after the radio button is pressed.
My question is:
How to add two strings
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
to this textView
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
so I can proceed this
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
And here is setOnClick... (at first it was with checkbutton 'mbuton')
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean check = true;
boolean correct = true;
// To check if all questions have been answered
for (boolean radioChecked : checked)
check = check && radioChecked;
if (check) {
// To check if all questions have been answered correctly
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
}
else
tv.setText("Answer all questions");
}
});
Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And string
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
// i have modified your code, please check it.
// i have display message if user does not select any radio button,
//another wise it display correct answer count on textview.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class Main2Activity2 extends Activity {
TextView tv;
Button mbuton;
RadioGroup rg1,rg2;
ArrayList<Integer> arrayListOfRadioGroupId =new ArrayList<Integer>();
int noAnswerCount= 0;
int correctAnswerRadioButtonCount= 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textViewAnswer);
mbuton = (Button) findViewById(R.id.mbuton);
rg1 = (RadioGroup)findViewById(R.id.rg1);
rg2 = (RadioGroup)findViewById(R.id.rg2);
// Store Radio group id to arraylist
arrayListOfRadioGroupId.add(rg1.getId());
arrayListOfRadioGroupId.add(rg2.getId());
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noAnswerCount= 0;
correctAnswerRadioButtonCount= 0;
for(int radioGroupId: arrayListOfRadioGroupId)
{
RadioGroup objRadioGroup = (RadioGroup) findViewById(radioGroupId);
int checkedId=objRadioGroup.getCheckedRadioButtonId();
// get Selected Radio button id.
if(checkedId>-1) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option"))
{
correctAnswerRadioButtonCount ++;
noAnswerCount --;
}
}
else
{
noAnswerCount++;
}
}
if(noAnswerCount > 0)
{
tv.setText("Answer all questions");
}
else
{
tv.setText("Correct Answer Count is: " +correctAnswerRadioButtonCount);
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton5) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option")) {
tv.setText("Correct Answer");
}
else
{
tv.setText("Wrong Answer");
}
}
}
});
}
}
// my Xml code is.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/mbuton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MButton"
android:layout_below="#id/textViewAnswer"/>
</RelativeLayout>
If your problem is just about setting those string in the TextView then, try this:
//For correct answer.
String CorrectAnswer = getString(R.string.Good_answer);
tv.setText(CorrectAnswer);
//For wrong answer.
String WrongAnswer = getString(R.string.Wrong_answer);
tv.setText(WrongAnswer);
The Code is still unclear (I can't Figure out what mButton is) .
However can you also share your layout xml I would suggest having a Radio Group comprising of X number of Radio Buttons (options) that you like and put the on checkedChangeListener
<RadioGroup
android:id="#+id/rgroup"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
</RadioGroup>
Your Activity should implement the OnCheckedChangeListener and you can then define your Radio Group RadioGroup Options = (RadioGroup) findViewById(R.id.rgroup);
Then you can implement a method in your activity where if a option inside that Radio Button is clicked you can use the below method.
#Override
public void onCheckedChanged(RadioGroup group,
int checkedId)
{
switch (checkedId)
{
case R.id.option1:
// settextview to correct here
break;
case R.id.option2:
//set textview to wrong here
break;
default:
break;
}
}
Assuming you might have answers at dynamic positions you might want to code some logic in the on checked change method . Hope this helps
Layout file; look at those questions and answers. I've hardcoded the string here. You can set it in string resource file and fetch it here using #string or set it dynamically using yourTextView.setText() method.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your name?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Melisa" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alisha" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lolita" />
</RadioGroup>
<RadioGroup
android:id="#+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rg1"
android:layout_marginTop="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What are you learning?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HTML" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rg2"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
And in your activity.
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroupFirst, radioGroupSecond;
private String[] realAnswers = {"Melisa", "Android"};
private String[] userAnswers;
private TextView tv;
private boolean status = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroupFirst = (RadioGroup) findViewById(R.id.rg1);
radioGroupSecond = (RadioGroup) findViewById(R.id.rg2);
tv = (TextView) findViewById(R.id.textViewAnswer);
userAnswers = new String[2];
radioGroupFirst.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
tv.setText("");
RadioButton nameRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + nameRadio.getText().toString());
userAnswers[0] = nameRadio.getText().toString();
}
});
radioGroupSecond.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton learningRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + learningRadio.getText().toString());
userAnswers[1] = learningRadio.getText().toString();
if (checkStatus(userAnswers))
tv.setText(getString(R.string.Good_answer));
else
tv.setText(getString(R.string.Wrong_answer));
}
});
}
private boolean checkStatus(String[] userAnswers) {
status = true;
for (int i = 0; i < userAnswers.length; i++) {
Log.e(userAnswers[i], realAnswers[i]);
if (!userAnswers[i].equals(realAnswers[i]))
status = false;
}
return status;
}
}
I haven't use the string resources here, if you want to set the text view with the string from resources too, look it in my first answer.
So, I think this will help you.

How to create a state for two buttons android

I have two Buttons, Male and Female, and what I want to do is, if the user select a Male button, change the background color, the way that it works as a RadioGroup, just can select Male or Female.
<LinearLayout
android:paddingTop="20dp"
android:layout_gravity="center"
android:weightSum="2"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_male"
android:textColor="#color/White"
android:background="#color/colorPrimaryDark"
android:layout_gravity="end"
android:text="#string/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="genderPerson"
/>
<Button
android:id="#+id/btn_female"
android:textColor="#color/White"
android:background="#color/colorPrimaryDark"
android:text="#string/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="genderPerson"
/>
</LinearLayout>
Usually for these kind of requirements i suggest you to use RadioButton instead of Buttons
you can achieve easily like below
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
and in Activity
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
for more info refer here
Documentation
this complete example example

How do i ensure that the selection of a radio button from a radio group affects the selection of a radio button in a different radio group.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
Instead of writing text to guide a user to skip question 2 after he
selects "No" in question 1, i want to be able to find a way to disable question two using java or xml codes when the user selects a "No". or automatically take question two away.
Activity:
(Note:i dont know why you want the strings so i separated them from the disabling method but with this it will disable the radio buttons if the user pressed no and they will enable when the user presses yes)
public class main extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainz);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_virus);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radio_avnumber);
Log.w("","click!");
if (checkedId == R.id.radio_virusno) {
Log.w("","asdasda");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(false);
}
}else
{
Log.w("","else");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(true);
}
}
}
});
}
public void assessUserInput(View view){
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus=(RadioGroup)findViewById(R.id.radio_virus);
String radioVirus=((RadioButton)findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber=(RadioGroup)findViewById(R.id.radio_avnumber);
String radioAvNumber=((RadioButton)findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock=(RadioGroup)findViewById(R.id.radio_adblock);
String radioAdBlock=((RadioButton)findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
//EditText nameTextField=(EditText)findViewById(R.id.edit_name);
//String hasName=nameTextField.getText().toString();
}
}
Used xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
</LinearLayout>
You have to handle onClick of your radiobutton :
public void assessUserInput(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_virusyes:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(true);}}
break;
case R.id.radio_virusno:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(false);}
}
break;
}
}
public void assessUserInput(View view) {
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus = (RadioGroup) findViewById(R.id.radio_virus);
String radioVirus = ((RadioButton) findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber = (RadioGroup) findViewById(R.id.radio_avnumber);
String radioAvNumber = ((RadioButton) findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock = (RadioGroup) findViewById(R.id.radio_adblock);
String radioAdBlock = ((RadioButton) findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
EditText nameTextField = (EditText) findViewById(R.id.edit_name);
String hasName = nameTextField.getText().toString();
// Is the button now checked
radioVirus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId = radio_virusno) {
//set the style of the things you want to disable here
}
}
});

Two RadioGroups with only one selected RadioButton in each of them

I have two RadioGroups in my layout. I want that only one RadioButton should be checked at a time in RadioGroup. I've tried many methods, but none of them work properly. Now I can check every RadioButton in my RadioGroups.
public class MainActivity extends ActionBarActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
String x;
char y;
int c=1;
String num,n,type;
int d,b,o,h;
String dec,bin,hex,oct;
EditText number;
EditText result;
Button convert, clear;
RadioGroup rgFrom, rgTO;
int convertedResult;
int from=R.id.r10From;
int to=R.id.r2TO;
String stNumber; //Liczba przed przeliczeniem - string
int numbResult; //Już int
String iterResult; //Liczba po przeliczeniu - string
int convResult; //Już int
//int id = ((RadioGroup).findViewById( R.id.rgFrom )).getCheckedRadioButtonId();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
number = (EditText) findViewById(R.id.etNumber);
result = (EditText) findViewById(R.id.etResult);
convert = (Button) findViewById(R.id.bCalculate);
clear = (Button) findViewById(R.id.bReset);
convert.setOnClickListener(this);
clear.setOnClickListener(this);
rgFrom = (RadioGroup) findViewById(R.id.rgFrom);
rgTO = (RadioGroup) findViewById(R.id.rgTO);
rgFrom.setOnCheckedChangeListener(this);
rgTO.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View view) {
// from = (String)findViewById(R.id.rgFrom).getSelectedItem();
// int id = ((RadioGroup)findViewById( R.id.rgFrom )).getCheckedRadioButtonId();
//from = getFrom(id);
switch (view.getId()){
case R.id.r2From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r8From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r10From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r16From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r2TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r8TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r10TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r16TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.bCalculate:
stNumber = number.getText().toString();
//iterResult = result.getText().toString();
//calculate();
break;
case R.id.bReset:
//reset();
break;
}
}
#Override
public void onCheckedChanged(RadioGroup rgFrom, int i) {
switch (view.getId()){
case R.id.r2From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r8From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r10From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r16From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r2TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r8TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r10TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r16TO:
to = rgTO.getCheckedRadioButtonId();
break;
}
}
}
My layout:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.converter_numeralsystem.app.MainActivity">
<TextView
android:layout_marginTop="20dp"
android:text="#string/number"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvNumber" />
<TextView
android:layout_marginTop="20dp"
android:layout_below="#id/tvNumber"
android:text="#string/result"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResult" />
<EditText
android:id="#+id/etNumber"
android:layout_marginLeft="5dp"
android:hint="#string/enter_numb"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/tvNumber"
android:layout_alignBottom="#id/tvNumber"/>
<EditText
android:id="#+id/etResult"
android:layout_marginLeft="5dp"
android:hint="#string/et_result"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/tvNumber"
android:layout_alignBottom="#id/tvResult"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="#string/from"
android:id="#+id/tvFrom"
android:layout_below="#id/tvResult"
android:layout_marginTop="20dp" />
<RadioGroup
android:weightSum="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tvFrom"
android:id="#+id/rgFrom"
android:layout_marginTop="10dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_weight="50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2From"
android:text="#string/dwa"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r8From"
android:text="#string/osiem"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="75dp" />
<RadioButton
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r10From"
android:text="#string/dziesiec"
android:layout_below="#id/r2From"
android:layout_alignStart="#id/r2From" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r16From"
android:text="#string/szesnascie"
android:layout_alignParentRight="true"
android:layout_below="#id/r8From"
android:layout_alignStart="#id/r8From"/>
</RelativeLayout>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="#string/to"
android:id="#+id/tvTo"
android:layout_below="#id/rgFrom"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvTo"
android:layout_marginTop="10dp"
android:id="#+id/rgTO">
<RelativeLayout
android:id="#+id/rel2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:checked="true"
android:layout_weight="50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2TO"
android:text="#string/dwa"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r8TO"
android:text="#string/osiem"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="75dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r10TO"
android:text="#string/dziesiec"
android:layout_below="#id/r2TO"
android:layout_alignStart="#id/r2TO" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r16TO"
android:text="#string/szesnascie"
android:layout_alignParentRight="true"
android:layout_below="#id/r8TO"
android:layout_alignStart="#id/r8TO"/>
</RelativeLayout>
</RadioGroup>
<LinearLayout
android:layout_marginTop="10dp"
android:weightSum="100"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rgTO">
<Button
android:layout_weight="30"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:id="#+id/bCalculate"
android:layout_toLeftOf="#+id/bReset" />
<Button
android:layout_weight="70"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/reset"
android:id="#+id/bReset"
android:layout_alignBottom="#+id/bCalculate" />
</LinearLayout>
</RelativeLayout>
Remove the RelativeLayout from the RadiGroup. So the code would look something like:
<RadioGroup
android:id="#+id/rgFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:weightSum="100" >
<RadioButton
android:id="#+id/r2From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="dwa" />
<RadioButton
android:id="#+id/r8From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="75dp"
android:text="osiem" />
<RadioButton
android:id="#+id/r10From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="dziesiec" />
<RadioButton
android:id="#+id/r16From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="szesnascie" />
</RadioGroup>
This will resolve the issue you are facing about multiple buttons being checked in same group. However, this will create a small issue in UI. I think you want 2 buttons in one line, and other two buttons in different line. As far as I know, you would have to implement your own custom layout for that. Check this post for details. Check here, too. Hope it helps a bit.

Categories