I am trying to make a radio button toggle using setChecked().
setChecked(false) works fine but setChecked(true) doesn't work.
Also tried toggle(), even this doesn't uncheck the radio button.
I need a solution without using radio groups. Just a single radio button and on tapping should toggle its state.
MainActivity.java
public void radioCheck(View v) {
System.out.println("radioCheck");
RadioButton rb = (RadioButton) findViewById(R.id.radioButton1);
//rb.toggle();
if (rb.isChecked() == true) {
rb.setChecked(false);
}
else {
rb.setChecked(true);
}
}
activity_main.xml
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:onClick="radioCheck"
android:text="Set me" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="39dp"
android:onClick="radioCheck"
android:text="On/ Off" />
Surprisingly, setChecked works when a button is used to uncheck the radio button but the same doesn't work when set on radio button.
You're causing a infinite loop and stack overflow probably. The setChecked is all unnecessary, you don't need to toggle it yourself. The onClick is for reacting to a new selection, not checking/unchecking it yourself.
From http://developer.android.com/guide/topics/ui/controls/radiobutton.html:
<?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>
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;
}
}
Try this way,hope this will help you to solve your problem.
if condition work true or false case so when you get isChecked() is already return true or false so no required to check with (== true) this things you have done wrong in your code.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set me" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioCheck"
android:layout_marginTop="10dp"
android:text="On/ Off" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private RadioButton radioButton1;
private Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
button2 = (RadioButton) findViewById(R.id.button2);
}
public void radioCheck(View v) {
if (radioButton1.isChecked()) {
radioButton1.setChecked(false);
}
else {
radioButton1.setChecked(true);
}
}
}
Related
I am trying to disable my RadioGroup so that the user cannot re-select and therefore my score count rises. I have seen an implementation here:
How to disable a RadioGroup until checkbox is checked
Albeit tried to go about it this way
My xml:
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/sm_margin"
android:orientation="horizontal">
<RadioButton
android:id="#+id/pates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/pates"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/chips"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/frites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/frites"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/crepes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/def_margin"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/crepes"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/question2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/def_margin"
android:background="#drawable/layout_bg"
android:fontFamily="#font/cambria"
android:padding="#dimen/tv_padding"
android:text="#string/question2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="70dp"
android:adjustViewBounds="true"
android:src="#drawable/jc_fav" />
<RadioGroup
android:id="#+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/sm_margin"
android:orientation="vertical">
<RadioButton
android:id="#+id/mille_feuille"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/milleFeuille"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/creme_chantilly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cremeChantilly"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/opera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/opera"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/creme_brulee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/def_margin"
android:text="#string/cremeBrulee"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
Java:
int score = 0;
RadioGroup rgQ1;
RadioGroup rgQ2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgQ1 = findViewById(R.id.rg1);
rgQ2 = findViewById(R.id.rg2);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked? then assign into a boolean named 'checked'
boolean checked = ((RadioButton) view).isChecked();
// Question 1 logic
// Check correct answer is checked and update score
switch (view.getId()) {
case R.id.frites:
if (checked) score += 1;
Log.v("MainActivity", "score" + score);
break;
}
// Disable RadioButtons of rg1
for (int i = 0; i < rgQ1.getChildCount(); i++) {
(rgQ1.getChildAt(i)).setEnabled(false);
}
// Question 2 logic
// Check correct answer is checked and update score
switch (view.getId()) {
case R.id.mille_feuille:
if (checked) score += 1;
Log.v("MainActivity", "score" + score);
break;
}
// Disable RadioButtons of rg2
for (int i = 0; i < rgQ2.getChildCount(); i++) {
(rgQ2.getChildAt(i)).setEnabled(false);
}
}
}
It disables the first radiogroup when an item is selected and also the 2nd (before a user has selected for the 2nd radiogroup.
Any ideas?
You are disabling your second radio group in the same click event. Check which radio button group is selected.
Like this, Similarly check group two and disable radio button group two!
int selectedId = radioGroup.getCheckedRadioButtonId();
if(selectedId != null){
//Then disable radio button group one
}
i got this nw. Before clicking on the button, my EditText is just viewable:
After button press, it should convert to an editable field:
I want the EditText field line to disappear and become read only and on button click make it editable. But this just fades the line and also the text inside it loses it visual appearance.
Here's my code:
et1.setEnabled(false);
et1.setFocusable(true);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
et1.setEnabled(true);
et1.requestFocus();
}
});
Your XML should look like this,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Now, add the following code to your activity class.
//init the views.
et1.setVisiblity(GONE);
tv1.setVisiblity(VISIBLE);
//if the textView is shown, the btn click will hide it and show editText
//instead, and same happens for editText
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(){
if(et1.getVisiblity() != VISIBLE) {
et1.setVisbility(VISIBLE);
tv1.setVisibility(GONE);
} else {
et1.setVisbility(GONE);
tv1.setVisibility(VISIBLE);
}
} });
P.S: This isn't executable code, although, hopefully it will get you the idea.
I am trying to make a popup window in an activity. The popup window shows up when we click a button in the activity. The layout for popup window is to show a radio group containing 4 radio buttons, a button and a seek bar and has to return a value to the main activity based on the selected radio button when the button in the pop up window is pressed.
When i run the app and open the pop up window it is giving we this error:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference "
The code for my main activity is:
public class CustomMenuActivity extends Activity
{
String ingredientName;
String ingredientQuantity;
private PopupWindow pw;
Button setQuantity;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_menu);
setQuantity = (Button) findViewById(R.id.btnSetQty);
setQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
initiatePopupWindow();
}
});
}
private void initiatePopupWindow()
{
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) CustomMenuActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.set_quantity_popup,
(ViewGroup) findViewById(R.id.popupElementid));
// create a 600px width and 570px height PopupWindow
pw = new PopupWindow(layout, 600, 570, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroupid);
final String[] tempQtyVar = new String[1];
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rbqty30id:
tempQtyVar[0] = "30";
break;
case R.id.rbqty60id:
tempQtyVar[0] = "60";
break;
case R.id.rbqty90id:
tempQtyVar[0] = "90";
break;
case R.id.rbqtycutomid:
tempQtyVar[0] = "120";
break;
}
}
});
Button setQtyBtn = (Button) layout.findViewById(R.id.buttonOkSetQtyid);
setQtyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredientQuantity = tempQtyVar[0];
Toast.makeText(getApplicationContext(),ingredientQuantity,Toast.LENGTH_LONG).show();
pw.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
The layout.xml for my pop up window is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#fd050505"
android:padding="30dp"
android:id="#+id/popupElementid">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET QUANTITY"
android:textColor="#84e9e6"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/popupTitleid"/>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popupTitleid"
android:orientation="horizontal"
android:background="#drawable/btn_rounded_boarder"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroupid"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" 30ml "
android:textColor="#84e9e6"
android:id="#+id/rbqty30id"
android:checked="true" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 60ml "
android:id="#+id/rbqty60id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 90ml "
android:id="#+id/rbqty90id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" Custom Value "
android:id="#+id/rbqtycutomid"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true">
<SeekBar
android:id="#+id/customqtySBid"
android:layout_height="fill_parent"
android:layout_width="600dp"
android:textColor="#84e9e6"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
android:paddingBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonOkSetQtyid"
android:textColor="#84e9e6"
android:text="OK"
android:background="#drawable/btn_rounded_boarder"
android:layout_marginTop="275dp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I tried looking for answer online but didn't really find solution specific to my problem. Please suggest any solutions.
Thanks in advance.
You seem to be inflating the wrong layout. Change the correct layout.xml in
setContentView(R.layout.activity_custom_menu);
I am trying to make a checkbox in android studio/java that if checked will result in a subset of other checkboxes checkable and checked.
The developer site says:
abstract void setChecked(boolean checked)
Change the checked state of the view
But this hasn't worked for me.
Current code:
Java:
package crc.cybereye;
public class CreateNew extends Activity {
LinearLayout background;
Button btnBack;
CheckBox checkbox_structural_info;
CheckBox checkbox_years_of;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
background = (LinearLayout) findViewById(R.id.background);
setContentView(R.layout.activity_create_new);
btnBack = (Button) findViewById(R.id.btnBack);
checkbox_years_of = (CheckBox) findViewById(R.id.checkbox_years_of);
checkbox_floors_above = (CheckBox) findViewById(R.id.checkbox_floors_above);
checkbox_structural_info = (CheckBox) findViewById(R.id.checkbox_structural_info);
// setUpIntroCheckBoxes(); // Add change listeners to check boxes
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), NewFormActivity.class);
startActivityForResult(intent, 0);
}
});
}
More Java:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_structural_info:
if (checked){
checkbox_years_of checked.setChecked(true) //ERROR HERE
}
break;
case R.id.checkbox_years_of:
if (checked){
}
break;
XML:
<TableRow android:layout_marginTop="10dp">
<TextView
android:text="#string/structural_info"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="#+id/checkbox_structural_info"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
</TableRow>
<TableRow>
<TextView
android:text="#string/years_of"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="#+id/checkbox_years_of"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
<TableRow>
There is an error saying that it cannot resolve method setChecked(boolean).
My code currently is just trying to get the structural_info checkbox to check the years_of checkbox if it is checked, but I also want to make years_or only checkable if structural_info is checked, I haven't got there yet because I was still stuck on this issue.
Thanks so much for any help in advance.
As requested by the creator of the post, i'm adding the comment as an answer:
You're calling setChecked to checked wich is a boolean variable.
You shoud write:
checkbox_years_of.setChecked(true)
Instead of
checkbox_years_of checked.setChecked(true)
I'm Developing an android app in which the Questionnaire activity contains Questions which as radio Buttons and also a Button.So when the button is pressed I've to check whether all the Questions are answered.if the Question is not answered then a alert message should pop up stating that the particular Question no is not answered.Can anyone please help me with the java code.
Thanks in Advance.
<RadioGroup
android:id="#+id/Mquestion1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_1_rb1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_1_rb2" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_1_rb3" />
</RadioGroup>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="#+id/Mquestion2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_rb1" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_rb2" />
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_rb3" />
</RadioGroup>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="#+id/Mquestion3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_rb1" />
<RadioButton
android:id="#+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_rb2" />
<RadioButton
android:id="#+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="sendMessage"
android:text="#string/MQ1_next" />
</RelativeLayout>
</LinearLayout>
and here is the java code
public class ManagerQuestionnaire1 extends Activity
{
Button next;
RadioGroup rg1;
RadioGroup rg2;
RadioGroup rg3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_questionnaire1);
final RadioGroup rg1=(RadioGroup)findViewById(R.id.Mquestion1);
final RadioGroup rg2=(RadioGroup)findViewById(R.id.Mquestion2);
final RadioGroup rg3=(RadioGroup)findViewById(R.id.Mquestion3);
Button next=(Button)findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener()
{
public void OnClick(View v) //Here in this line I'm getting a caution symbol which says REMOVE METHOD 'OnClick'
{
if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || rg1.getCheckedRadioButtonId()!=R.id.radioButton2 || rg1.getCheckedRadioButtonId()!=R.id.radioButton3)||(rg2.getCheckedRadioButtonId()!=R.id.radioButton4 || rg2.getCheckedRadioButtonId()!=R.id.radioButton5 || rg2.getCheckedRadioButtonId()!=R.id.radioButton6)||(rg3.getCheckedRadioButtonId()!=R.id.radioButton7 || rg3.getCheckedRadioButtonId()!=R.id.radioButton8 || rg3.getCheckedRadioButtonId()!=R.id.radioButton9))
{
AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
alert.setTitle("Exception:Complete the Questions");
alert.setMessage("Please ensure all Questions are answered");
}
else
{
Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
startActivity(intent);
}
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
}
You should check on the SelectedID of the radioGroup and check for the return ID and their corresponding ids as following:
0)Declare all the used UI component in the global scoop of your code as following:
Class x extends Activity{
RadioButton radioButton1;
...
1)Initialize all the XML buttons to get each button ID in OnCreate as following
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
//The rest of other buttons
....
....
2)In the clickListener
next.setOnClickListener(new View.OnClickListener()
{
public void OnClick(View v)
{
if(Mquestion1.value!=radioButton1 || Mquestion1.value!=radioButton2 || Mquestion1.value!=radioButton3 || Mquestion1.value!=radioButton4) ||(Mquestion2.valu!= radioButton5 || ..... )||(Mquestion3.value....)
{
//Show the allert
}
}
});
Replace the dots with your rest checks.
Use the following code instead of yours
Button next;
RadioGroup rg1;
RadioGroup rg2;
RadioGroup rg3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_questionnaire1);
rg1=(RadioGroup)findViewById(R.id.Mquestion1);
rg2=(RadioGroup)findViewById(R.id.Mquestion2);
rg3=(RadioGroup)findViewById(R.id.Mquestion3);
next=(Button)findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener()
{
public void OnClick(View v)
{
if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton2 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton3)||(Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton4 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton5 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton6)||(Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton7 || Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton8 || Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton9))
{
AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
alert.setTitle("Exception:Complete the Questions");
alert.setMessage("Please ensure all Questions are answered");
}
else
{
Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
startActivity(intent);
}
}
});
If you generate your questions, so you don't have any IDs for your views, you can cycle through all RadioGroups and through all their children in RadioGroup. So if you store RadioGroups for example in an ListArray radioGroups, then you can do this:
private boolean isAllAnswered() {
for (RadioGroup rg : radioGroups) {
boolean questionAnswered = false;
for (int i = 0; i < rg.getChildCount(); i++) {
RadioButton rb = rg1.getChildAt(i);
if (rb.isChecked()) {
questionAnswered = true;
break; // litle optimalization
}
}
if (questionAnswered == false) {
return false;
}
}
return true;
}
If you need to know, which questions are not answered, you can expand it to returning an Array.
If you use xml, then make an common method isAnswered(RadioGroup rg) and skip the first cycle. It's up to you.