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.
Related
I want to make a quiz app that can calculate the sum of the number in radio button that user click. How to caluclate the sum and how can I display the result in another layout after user click the button to generate the result?
Question page Result page
Here is the code for question layout (activity_main.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:id="#+id/instruction_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/instruction" />
// Question 1
<TextView
android:id="#+id/question1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question1" />
<RadioGroup
android:id="#+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question1_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question1_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<RadioButton
android:id="#+id/question1_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question1_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question1_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question1_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question1_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 2
<TextView
android:id="#+id/question2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question2" />
<RadioGroup
android:id="#+id/question2_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question2_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question2_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question2_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question2_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question2_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question2_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question2_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 3
<TextView
android:id="#+id/question3_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question3" />
<RadioGroup
android:id="#+id/question3_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question3_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question3_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question3_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question3_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question3_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question3_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question3_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 4
<TextView
android:id="#+id/question4_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question4" />
<RadioGroup
android:id="#+id/question4_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question4_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question4_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question4_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question4_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question4_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question4_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question4_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 5
<TextView
android:id="#+id/question5_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question5" />
<RadioGroup
android:id="#+id/question5_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question5_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question5_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question5_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question5_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question5_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question5_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question5_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/jana_keputusan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jana Keputusan" />
</LinearLayout>
Here is the code for the question activity (MainActivity.java):
package com.android.resilien.resilienminver;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
startActivity(generate_result_intent);
}
});
}
// Question 1 Radio Button clicked
public void onRadioButtonQuestion1Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question1_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question1_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question1_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question1_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question1_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question1_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question1_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 2 Radio Button clicked
public void onRadioButtonQuestion2Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question2_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question2_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question2_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question2_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question2_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question2_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question2_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 3 Radio Button clicked
public void onRadioButtonQuestion3Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question3_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question3_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question3_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question3_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question3_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question3_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question3_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 4 Radio Button clicked
public void onRadioButtonQuestion4Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question4_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question4_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question4_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question4_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question4_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question4_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question4_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 5 Radio Button clicked
public void onRadioButtonQuestion5Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question5_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question5_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question5_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question5_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question5_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question5_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question5_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
}
First i would strongly recommend to sum up the click method for all RadioButtons to one single method, e.g. onRadioAction:
The layout:
<?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:id="#+id/instruction_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/instruction" />
// Question 1
<TextView
android:id="#+id/question1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question1" />
<RadioGroup
android:id="#+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question1_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question1_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<RadioButton
android:id="#+id/question1_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question1_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question1_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question1_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question1_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 2
<TextView
android:id="#+id/question2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question2" />
<RadioGroup
android:id="#+id/question2_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question2_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question2_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question2_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question2_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question2_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question2_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question2_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 3
<TextView
android:id="#+id/question3_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question3" />
<RadioGroup
android:id="#+id/question3_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question3_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question3_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question3_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question3_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question3_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question3_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question3_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 4
<TextView
android:id="#+id/question4_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question4" />
<RadioGroup
android:id="#+id/question4_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question4_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question4_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question4_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question4_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question4_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question4_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question4_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 5
<TextView
android:id="#+id/question5_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question5" />
<RadioGroup
android:id="#+id/question5_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question5_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question5_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question5_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question5_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question5_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question5_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question5_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/jana_keputusan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jana Keputusan" />
</LinearLayout>
The activity:
package com.android.resilien.resilienminver;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
startActivity(generate_result_intent);
}
});
}
public void radioButtonAction(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question5_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question5_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question5_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question5_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question5_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question5_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question5_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
To extend your app i would also recommend, that you generate the layout for questions dynamically in your source code, not as hardcoded layout xml.
Finally to display the result e.g. add a Button to the bottom and set the following code to display a dialog with the achieved score. As alternative you can also start a new Activity and show it there via Intent and Bundle.
private onClickedButtonResult(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Your score")
//set message
.setMessage("You achieved a score of " + String.valueOf(score) + "!")
//set positive button
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Call finish to close your app
finish();
}
})
.show();
}
Alternative:
Show score in another Activity
Create an Intent for the other Activity and save the value of score in the intent:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
Bundle extras = generate_result_intent.getExtras();
extras.putExtra("SCORE", score);
startActivity(generate_result_intent);
}
});
}
And in your other Activity you can read the passed value of score and show it to the user:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
Intent mIntent = getIntent();
if (i.hasExtra("SCORE")) {
int score = mIntent.getIntExtra("SCORE", 0);
// Do something to display the score
}
}
Finally, be aware that the user can increase his own score by clicking multiple times on a RadioButton. That should be addressed in your application logic, because the scoring would be invalid.
You can go to another activity and display result by INTENT. You can pass your score to another activity by using putExtra.
MainActivity:
okAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("Score", score);
startActivity(intent);
}
});
Another Activity:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
int score = bundle.getInt("Score");
restltText.setText("Your score is: "+ score);
}
To get text from RadioButton I offer you to use:
int indexOfButton = radioGroup.indexOfChild(findViewById(radioGroup.getCheckedRadioButtonId()));
String scoreS = (indexOfButton > -1)? ((RadioButton) radioGroup.getChildAt(indexOfButton)).getText().toString() : "";
score += Integer.parseInt(scoreS);
This methods is getting checked RadioButton id and by it finds index in that group. By the index you can get RadioButton text.
To show the score in other activity you can send data throw Intent extras. Like this:
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("result", score);
startActivity(intent);
On ResultActivity.class you can get score from extras like this:
Integer result = 0;
Intent i = getIntent();
if (i.hasExtra("result")) {
result = i.getIntExtra("result");
}
Then you can display it in TextView or in other possible ways.
If you don't understand something, feel free to ask.
I hope it helps you...
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.
So I would like to have a calculator in my app. User will first click on RadioButton which would be for Imperial and Metric units, and depending on which one does user click, one of the layouts would appear.
As you know imperial units has feets and inches, for which I need two EditTexts, but metric unit needs only one.
So that's what I want, switching between layouts in same activity to avoid using fragments.
Any ideas?
EDIT
xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
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"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.KalkulatoriBFMan"
tools:showIn="#layout/activity_kalkulatori_bfman"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff"
android:layout_marginBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout28"
android:layout_marginBottom="20dp"
android:layout_marginTop="55dp"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_uvodno"
android:id="#+id/textView140"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tekst_upute"
android:id="#+id/textView141"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Drugo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_marginBottom="10dp"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout27"
android:layout_below="#+id/linearLayout28"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_radio_naslov"
android:id="#+id/textView3"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneCmKg"
android:text="#string/bf_radio2"
android:onClick="radioZeneCmKgClicked"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneFtLb"
android:text="#string/bf_radio1"
android:onClick="radioZeneBFFtLbClicked"
android:checked="false" />
</RadioGroup>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_tezina"
android:id="#+id/textView133"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTezinaBF"
android:hint="#string/bf_edit_tezina_hint_z"
android:layout_marginBottom="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaMetric">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_visina"
android:id="#+id/textViewVisinaMetric"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVisinaBF"
android:hint="#string/bf_edit_visina_hint_z"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaImperial">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_feetinch_naslov"
android:id="#+id/textViewVisinaImperial"
android:textStyle="bold" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaFT"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaINC"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint2" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_struk"
android:id="#+id/textView135"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editStrukBF"
android:hint="#string/bf_edit_struk_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_bokovi"
android:id="#+id/textView136"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editBokoviBF"
android:hint="#string/bf_edit_bokovi_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_vrat"
android:id="#+id/textView137"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVratBF"
android:hint="#string/bf_edit_vrat_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_godine"
android:id="#+id/textView138"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editGodineBF"
android:hint="#string/bf_edit_godine_hint_z"
android:layout_marginBottom="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout27"
android:layout_marginBottom="20dp"
android:id="#+id/linearLayout29">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bf_btn_izracunaj"
android:id="#+id/btnIzracunajBF"
android:layout_gravity="center_horizontal"
android:background="#424242"
android:textColor="#ffffff"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_rezultat"
android:id="#+id/textView139"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textRezultatBF"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_rezultat"
android:id="#+id/textView152" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_below="#+id/linearLayout27"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_tumacenje"
android:id="#+id/textView142"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tumacenje_rezultat_z"
android:id="#+id/textView143"
android:gravity="center_horizontal"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
java
public class KalkulatoriBFMan extends AppCompatActivity {
EditText editTezina, editVisina, editStruk, editBokovi, editVrat, editGodine, editVisinaFT, editVisinaINC;
Button btnIzracunajBF;
TextView textRezultatBF, textVisinaMetric, textVisinaImperial;
LinearLayout imperial, metric;
public void radioZeneCmKgClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.radioButtonBFZeneCmKg:
if (checked)
editVisina.setVisibility(View.VISIBLE);
imperial.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (women) = 495 / (1.29579 - .35004 * log10(Waist + Hip - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisina.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.0324 - 0.19077 * Math.log10(trecePolje - petoPolje) + 0.15456 * Math.log10(drugoPolje)) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
public void radioZeneBFFtLbClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButtonBFZeneFtLb:
if (checked)
editVisinaFT.setVisibility(View.VISIBLE);
metric.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (men) = 495 / (1.29579 - .35004 * log10(Waist - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisinaFT.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double sedmoPolje = Double.parseDouble(editVisinaINC.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.29579 - 0.35004 * Math.log10(trecePolje - petoPolje) + 0.22100 * Math.log10(((drugoPolje * 12) + sedmoPolje))) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kalkulatori_bfwoman);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
editTezina = (EditText) findViewById(R.id.editTezinaBF);
editVisina = (EditText) findViewById(R.id.editVisinaBF);
editStruk = (EditText) findViewById(R.id.editStrukBF);
editBokovi = (EditText) findViewById(R.id.editBokoviBF);
editVrat = (EditText) findViewById(R.id.editVratBF);
editGodine = (EditText) findViewById(R.id.editGodineBF);
editVisinaFT = (EditText) findViewById(R.id.editTextVisinaFT);
editVisinaINC = (EditText) findViewById(R.id.editTextVisinaINC);
textVisinaImperial = (TextView) findViewById(R.id.textViewVisinaImperial);
textVisinaMetric = (TextView) findViewById(R.id.textViewVisinaMetric);
imperial = (LinearLayout) findViewById(R.id.LayoutVisinaImperial);
metric = (LinearLayout) findViewById(R.id.LayoutVisinaMetric);
btnIzracunajBF = (Button) findViewById(R.id.btnIzracunajBF);
textRezultatBF = (TextView) findViewById(R.id.textRezultatBF);
}
}
Note that I solved blank space problem with setting visibility to GONE, but when I open activity both EditTexts appear, imperial and metric. I want them both to be invisible until user selects one RadioButton
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
For example, here are a couple RadioButton objects:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_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>
Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
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.imperial:
if (checked)
EditBox1.setVisibility(Visible)
break;
case R.id.metrics:
if (checked){
EditBox1.setVisibility(Visible)
EditBox2.setVisibility(Visible)
}
break;
}
}
Source: http://developer.android.com/guide/topics/ui/controls/radiobutton.html
<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
}
}
});
I'm having a problem with my code. Whenever I run it, it shows the error error application stopped unexpectedly. I think the issue is caused by some error in the Java class. What exactly is causing this issue?
Here is my XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:overScrollMode="always"
android:layout_height="match_parent"
android:background="#A52A2A"
tools:context="com.safeermalik.gpa.Start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Name"
android:id="#+id/subject"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Marks"
android:id="#+id/marks"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Credits"
android:id="#+id/credits"/>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Criteria :"/>
<RadioButton android:id="#+id/radio_old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Old Criteria"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Criteria"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add course"
android:background="#696969"
android:id="#+id/add"
android:onClick="sendMessage"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/add_table"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Percentage "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="GPA "/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TableRow
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:textStyle="bold"
android:textSize="18sp"
android:text="Total Marks : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/total_marks"
android:hint="Total Marks "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="S-GPA : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/s_gpa"
android:hint="GPA "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="PERCENTAGE : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/final_percentage"
android:hint="Percentage "/>
</TableRow>
</TableLayout>
</LinearLayout>
Here is the Java class:
public class Start extends ActionBarActivity {
EditText sub;
EditText mark;
EditText credit;
Button Adds;
int a=0,b=0,hour=0,x=0,counter=0;
String grade="";
String subject="";
EditText SUM,percent,GPA;
double gpa,gpa_multiple;
TableLayout table;
CalcGPA cal=new CalcGPA();
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_new:
if (checked)
x=1;
break;
case R.id.radio_old:
if (checked)
x=0;
break;
} }
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
sub=(EditText)findViewById(R.id.subject);
subject=sub.getText().toString();
mark=(EditText)findViewById(R.id.marks);
a=Integer.parseInt(String.valueOf(mark));
credit=(EditText)findViewById(R.id.credits);
b=Integer.parseInt(String.valueOf(credit));
Adds=(Button)findViewById(R.id.add);
table=(TableLayout)findViewById(R.id.add_table);
SUM=(EditText)findViewById(R.id.total_marks);
GPA=(EditText)findViewById(R.id.s_gpa);
percent=(EditText)findViewById(R.id.final_percentage);
Adds.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sub.setText("");
mark.setText("");
credit.setText("");
counter++;
hour +=b;
if(x==0){
gpa=cal.subgpa(a);
grade=cal.Grade(a);
}
else if(x==1){
gpa=cal.ChangedGpa(a);
grade=cal.ChangedGrade(a);
}
add_row(subject,a,b,gpa,grade);
gpa_multiple+=(gpa*b);
SUM.setText(""+(counter*a));
GPA.setText(""+(gpa_multiple/hour));
percent.setText(""+((counter*a)/(counter*100)*100));
}
} );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void add_row(String sub, int crdt, int marks, double sgpa, String subgrade ){
TableLayout tb = (TableLayout) findViewById(R.id.add_table);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView subject_name=new TextView(this);
subject_name.setText(counter+""+sub);
subject_name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_name.setBackgroundResource(R.drawable.border);
TextView subject_marks=new TextView(this);
subject_marks.setText(""+marks);
subject_marks.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_marks.setBackgroundResource(R.drawable.border);
TextView subject_credits=new TextView(this);
subject_credits.setText(""+crdt);
subject_credits.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_credits.setBackgroundResource(R.drawable.border);
TextView subject_gpa=new TextView(this);
subject_gpa.setText(""+sgpa);
subject_gpa.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
subject_gpa.setBackgroundResource(R.drawable.border);
TextView subject_grade=new TextView(this);
subject_grade.setText(""+subgrade);
subject_grade.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_grade.setBackgroundResource(R.drawable.border);
}
}
You have a logical error and a runtime error here.
Why are you parsing your content of your EditText in the onCreate() method?
just remove those two lines :
//a = Integer.parseInt(String.valueOf(mark));
//b = Integer.parseInt(String.valueOf(credit));
First you don't need to parse them in the onCreate method. You parse the content in the onClick() method of your Button.
that was the logical error.
Second:
Your runtime error is being cause by the fact that you are trying to get the valueOf an EditText and not of a String. so mark should be replace by mark.getText().toString() and credit by credit.getText().toString()