I am making an android application that contains a test for depression. The test consists of 9 questions and each question has 4 possible answers. The answers are in the form of radio buttons. I need to assign numeric values to the radio buttons as follows:
Answer 1 = 0 Answer 2 = 1 Answer 3 = 2 Answer 4 = 3
At the end of the test the user will receive a result, 1-10 being likely not depressed and 10-27 being likely depressed.
Each question is asked on a separate page and the result page will contain the result which is the total of each answer added.
Can someone please make a suggestion as to how I should tackle this?
res/values/strings.xml code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="int1">0</integer>
<integer name="int2">1</integer>
<integer name="int3">2</integer>
<integer name="int4">3</integer>
</resources>
"Test1" xml code:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/white"
tools:context=".Test1">
<!-- title text -->
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="#string/D1title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- subtitle text -->
<TextView
android:id="#+id/txtSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"
android:text="#string/Q1Subtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- question 1 text -->
<TextView
android:id="#+id/txtQ1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtSubTitle"
android:layout_marginTop="38dp"
android:text="#string/Q1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- radio group to allow for only one radio button selection -->
<RadioGroup
android:id="#+id/radioAnswers1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtQ1" >
<!-- 1st answer radio button-->
<RadioButton
android:id="#+id/radioA1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:checked="false"
android:text="#string/zero"
android:tag="#integer/int1" />
<!-- 2nd answer radio button-->
<RadioButton
android:id="#+id/radioA2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one"
android:tag="#integer/int2" />
<!-- 3rd answer radio button-->
<RadioButton
android:id="#+id/radioA3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/two"
android:tag="#integer/int3" />
<!-- 4th answer radio button-->
<RadioButton
android:id="#+id/radioA4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/three"
android:tag="#integer/int4" />
</RadioGroup>
<!-- next button -->
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/txtTitle"
android:text="#string/next" />
</RelativeLayout>
"Test1" java code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class Test1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
final RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()));
static int result;
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged (RadioGroup group, int checkedId){
final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioA1);
final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioA2);
final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioA3);
if (radioButton1.isChecked()) {
displayToast("No, not at all");
} else if (radioButton2.isChecked()) {
displayToast("On some days");
}else if (radioButton3.isChecked()) {
displayToast("On more than half the days");
}else {
displayToast("Nearly everyday");
}
}
});
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
checkedButton.getTag();
Intent intent = new Intent(getBaseContext(), Test2.class);
intent.putExtra("EXTRA_RESULT", result);
startActivity(intent);
openTest2();
}
});
}
//Open test page
public void openTest2() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test2.class);
startActivity(i);
}
public void displayToast(String text){
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
"Results1" XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<!-- Creating Results Title -->
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="#string/resultsTitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- Creating Results Subtitle -->
<TextView
android:id="#+id/txtSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="#string/subtitleR1" />
<!-- Creating Score Label -->
<TextView
android:id="#+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/txtResults"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:text="#string/score1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- Creating Results Text -->
<TextView
android:id="#+id/txtResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnNext"
android:layout_alignParentLeft="true"
android:text="#string/results1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- Creating Next Button -->
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="#string/next" />
</RelativeLayout>
"Results1" java code:
package com.lifematters;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class Results1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results1);
final Button nextBtn = (Button) findViewById(R.id.btnNext);
final TextView score = (TextView) findViewById(R.id.txtScore);
int result;
result = getText(Test1.result1);
//Set up listener for Next
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
open101();
}
});
}
//Open test page
public void open101() {
//create new textview
Intent i = new Intent(getApplicationContext(), Depression101.class);
startActivity(i);
}
}
Rather than checking every single radio button to see which one is checked, you can get the only checked radio button from the checked ID.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId() ))
Then you don't need that checked change listener, you can get the value on nextBtn click. Of course, you may still want the checked change listener to see if one was selected or display the toast... but that is besides the point.
Part 2, how to convert that radio button to a numeric value. Really, there are many ways to do this. I think my preferred way would be an enum, but you could do any number of methods for string to int mapping, radio button id to int mapping, etc. You could even use tags. Looks like you have some commented code in there around tags. I would just set the tags in the xml, then use checkedButton.getTag() to get the value when you are ready to move on to the next page. Simple enough.
I would definitely recommend passing the results on to the next Activity through intent extras. Each new Test page can add the result from that page to the previous page and pass the new value on, then when you get to the result page, the total result score is already calculated. It scales nicely if the user starts clicking the back button to change answers.
Related
I'm currently working on an Activity in my game that provides two important functionalities:
determining the amount of Players for a game session
determining what Roles from the predetermined ArrayList allRoles will be present in this game session (all Roles selected here will be randomly assigned to a list of players later)
Every Role has different abilities and values, so each Role is its own object that inherits from the Role.java class.
For example, the allRoles-ArrayList contains objects like Mage, Mountainclimber, Bear, Wise_Man and more, all inheriting from the Role class.
Role.java
public class Role {
protected String imageName;
protected int count;
protected int maxCount;
public Role(int maxCount){
this.imageName = null;
this.count = 0;
this.maxCount = maxCount;
}
}
Now, in my Activity there's a GridLayout (will be built and then added to a LinearLayout) containing one role_entry.xml for each element in allRoles.
Some Roles can be in the game multiple times, while there can be only one of others.
The TextView with the id "roleCount" shows how many instances of a Role the user wants in his game.
This number can be adjusted with the "+" and "-" Buttons, within the Role's bounds (0-maxCount).
NOTE: I want the Buttons to adjust the count value of its corresponding Role as well, but I don't know how.
I also don't know which Role to get 'maxCount' from when a Button is pressed.
My question aims to solve these problems.
Possible user selection:
3x Mountainclimber, 2x Mage, 1x Bear, 1x Wise_Man = 7 Players in total (user can choose 7 Player profiles later)
role_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/roleImage"
android:layout_width="120dp"
android:layout_height="120dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/roleCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_marginBottom="6dp"
android:text="0"
android:textSize="#dimen/roleSelection_roleCount_ts"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="#id/roleImage"
app:layout_constraintEnd_toEndOf="#id/roleImage"/>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#id/roleImage"
app:layout_constraintStart_toStartOf="#id/roleImage"
app:layout_constraintEnd_toEndOf="#id/roleImage"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/roleSubtract"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="-"
android:textSize="#dimen/button_ts"
android:enabled="false"
android:textColor="#color/black"
android:background="#drawable/button_left_custom"/>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/roleAdd"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="+"
android:textSize="#dimen/button_ts"
android:background="#drawable/button_right_custom"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
RoleSelection.java (my Activity)
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class RoleSelection extends AppCompatActivity {
//Variables
ArrayList<Role> allRoles = null;
//Views
private TextView totalRoleCount = null;
private LinearLayout scrollList = null;
private GridLayout basegameList = null;
private Button next = null;
private Button back = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_role_selection);
//initializing views
totalRoleCount = findViewById(R.id.total_role_count);
scrollList = findViewById(R.id.scrollList);
//initializing 'allRoles' in the order it will be shown in in the GridLayout
allRoles = new ArrayList<>();
allRoles.add(new Mountainclimber());
allRoles.add(new Mage());
allRoles.add(new Bear());
allRoles.add(new Wise_Man());
//adding Roles to GridLayout
//
//building GridLayout to contain Role-entries
GridLayout basegameList = new GridLayout(this);
basegameList.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
basegameList.setColumnCount(3);
basegameList.setUseDefaultMargins(true);
for(Role r: allRoles){
//initializing 'roleEntry'
View roleEntry = getLayoutInflater().inflate(R.layout.role_selection_entry, null);
ImageView roleImage = roleEntry.findViewById(R.id.roleImage);
TextView roleCount = roleEntry.findViewById(R.id.roleCount);
Button roleSubtract = roleEntry.findViewById(R.id.roleSubtract);
Button roleAdd = roleEntry.findViewById(R.id.roleAdd);
//setting Image
roleImage.setImageResource(this.getResources().getIdentifier("drawable/" + r.imageName, null, this.getPackageName()));
//just to be safe
roleCount.bringToFront();
//'Subtract'-Button of roleEntry
roleSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO: pseudocode:
// check if decrement would hit 0:
// if yes, decrement and set 'enabled' of roleSubtract to "false" --> this role can no longer be decremented
// else:
// decrement
}
});
//'Add'-Button of roleEntry
roleAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO: pseudocode:
// check if increment would hit 'maxCount':
// if yes, increment and set 'enabled' of roleAdd to "false" --> this role can no longer be incremented
// else:
// increment
}
});
//adding 'roleEntry' to GridLayout
basegameList.addView(roleEntry);
}
//adding Gridlayout to scrollList
//NOTE: scrollList exists because more Gridlayouts and Seperators may be added later - when I decide to make extensions of the game
scrollList.addView(basegameList);
next = findViewById(R.id.navigation_bar_next);
//TODO: set onClickListener for next --> PlayerSelection
back = findViewById(R.id.navigation_bar_back);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { gotoPlayerCreation(); }
});
}
//TODO: create gotoPlayerSelection() function
public void gotoPlayerCreation(){
Intent intent = new Intent(this, PlayerCreation.class);
startActivity(intent);
}
}
role_selection.xml (my Activity)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RoleSelection"
tools:ignore="HardcodedText">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/info_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/explanation"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginStart="#dimen/marginStart_default"
android:layout_marginTop="#dimen/marginTop_default"
android:layout_marginEnd="60dp"
android:orientation="horizontal"
app:layout_constraintEnd_toStartOf="#+id/total_selected"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Wähle die Karten aus, die mitspielen:"
android:textSize="#dimen/standard_ts" />
</LinearLayout>
<TextView
android:id="#+id/total_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gesamt: "
android:textSize="#dimen/standard_ts"
app:layout_constraintBottom_toBottomOf="#+id/explanation"
app:layout_constraintEnd_toStartOf="#+id/total_role_count"
app:layout_constraintTop_toTopOf="#+id/explanation" />
<TextView
android:id="#+id/total_role_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/marginEnd_default"
android:text="0"
android:textSize="#dimen/standard_ts"
app:layout_constraintBottom_toBottomOf="#id/total_selected"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/total_selected" />
<TextView
android:id="#+id/recommendations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/marginStart_default"
android:layout_marginEnd="#dimen/marginEnd_default"
android:layout_marginBottom="#dimen/marginBottom_default"
android:text="Empfehlung: "
android:textSize="#dimen/standard_ts"
app:layout_constraintTop_toBottomOf="#id/explanation"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<ScrollView
android:id="#+id/roleSelection_scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/info_container"
app:layout_constraintBottom_toTopOf="#id/navigation_container">
<LinearLayout
android:id="#+id/scrollList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/navigation_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<include layout="#layout/navigation_bar" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
How do I associate a role_entry with its corresponding object in ArrayList allRoles?
Please keep in mind that I'm new to android development, and thank you all in advance :D
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I am trying to do an easy check wether the right answer is given to a multiple choice question by using radio buttons and an if-statement.
I wanna use this as a part of an teaching app, however I am probably misunderstanding the usage of
int radioId = radioGroup.getCheckedRadioButtonId()
and I hope you guys can help me. I am using four radio buttons in an radio group and posted xml and java below.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IntroQuiz1">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="8dp"
android:text="Quiz1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="checkButton"
android:text="RadioButton1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="checkButton"
android:text="RadioButton2" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="checkButton"
android:text="RadioButton3" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="checkButton"
android:text="RadioButton4" />
</RadioGroup>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="156dp"
android:text=""
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.906"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioGroup" />
<Button
android:id="#+id/button_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="52dp"
android:text="Check"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioGroup" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
public class IntroQuiz1 extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro_quiz1);
radioGroup = findViewById(R.id.radioGroup);
textView = findViewById(R.id.textView2);
Button buttonCheck = findViewById(R.id.button_check);
buttonCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int radioId = radioGroup.getCheckedRadioButtonId();
int test = 1;
radioButton = findViewById(radioId);
String checkString = (String) radioButton.getText();
if (checkString == "RadioButton1") {
textView.setText("Korrekt");
} else {
textView.setText(checkString);
}
}
});
}
public void checkButton(View v) {
int radioId = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(radioId);
Toast.makeText(this, "Selected Radio Button: " + radioButton.getText(),
Toast.LENGTH_SHORT).show();
}
}
However, I don't get "Korrekt" printed with any radio button checked. This is already a workaround. I do not want to compare strings. I would rather use the Id, but as I tried to use radioId to check for the correct answer, radioId seems not to be an integer as I would expect from what I learned so far, is that correct?
So, I am a bit confused, could someone help me how to correct this code and tell me what I did wrong?
You should be compare your id selected with others, not string;
Like:
int radioId = radioGroup.getCheckedRadioButtonId();
switch(radioId ){
case R.id.radioButton1:
//your code;
break;
case R.id.radioButton2:
//your code;
break;
case R.id.radioButton3:
//your code;
break;
}
I am making a Quiz app where I have created 2 buttons ,one for submit and other to move to next Question
I Want to code it in such a way that if I hit Submit button the response gets recorded and doesn't change after it:
My XML Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vidit.project3.question1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/q1"
android:textAlignment="center"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:id="#+id/tv"
/>
<RadioGroup
android:layout_marginTop="20dp"
android:layout_below="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:orientation="vertical"
android:id="#+id/rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a2" />
<RadioButton
android:id="#+id/correct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a4"
android:id="#+id/last" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="#+id/rg"
android:id="#+id/submit"
android:text="SUBMIT"
android:layout_marginTop="20dp"
android:onClick="submit1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="NEXT"
android:onClick="toQuestion2"/>
</RelativeLayout>
</ScrollView>
Java Code:
package com.example.vidit.project3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
public class question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
public void submit1(View v)
{
RadioButton rb= (RadioButton) findViewById(R.id.correct);
if(rb.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(),"Whopiee! Correct Answer",Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(),"Arghh! Incorrect Answer",Toast.LENGTH_LONG);
toast.show();
}
}
public void toQuestion2(View v)
{
Intent intent = new Intent(this,question2.class);
startActivity(intent);
}
}
You can achieve this by storing the response of answer in Map and need to be check in the map if the Question exists , then disable the options otherwise open the options.
Map<Question,Response> quizResponse = new HashMap();
Now add your all questions one by one in this Map and check
if(quizResponse.contains(question))
{ radioGroup.getChildAt(i).setEnabled(false); // Disable the button
}
else
{
// show the question
quizResponse.put(question, response); // this response can be blank and update once you got user response
}
This will disable all the radio buttons. Is that what you want?
for (int i = 0; i < radioGroup.getChildCount(); i++)
{
radioGroup.getChildAt(i).setEnabled(false);
}
Hello Stack Overflow !
I'm working on an android application for children : Each activity is a question.
For each question, there is two buttons for clue and the answer.
I encounter two issues :
1/ I have to click on a button before change activity, do I need an override or something so I can change of activity without any click required ?
2/ On this activity (code is below), if I click on the button with the id "essai", the activity stop and I came to the previous activity.. Anybody know how I could fix this ?
Thank you in advance !
Here is the java code
package com.beryl.timewaster;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class Activity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
public void indice1(View v)
{
Toast.makeText(this, "expression", Toast.LENGTH_LONG).show();
}
public void indice2(View v)
{
Toast.makeText(this, "aguéri", Toast.LENGTH_LONG).show();
}
public void essai(View v)
{
Toast.makeText(this, "LOLILO", Toast.LENGTH_LONG).show();
final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1);
ib2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity1.this, Accueil.class);
startActivity(intent);
}
});
}}
And here the xml
<AbsoluteLayout
android:id="#+id/absoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxx"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:layout_x="-1dp"
android:layout_y="61dp"
android:text="#string/dev2"
android:textColor="#color/blanc"
android:textSize="25sp"
android:color="#color/blanc" />
<Button
android:id="#+id/Solution2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="259dp"
android:onClick="essai"
android:text="#string/solution"
android:textColor="#color/vertpomme"
android:textSize="30sp" />
<Button
android:id="#+id/indice1de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="1dp"
android:layout_y="121dp"
android:onClick="indice1"
android:text="#string/indice1"
android:textColor="#color/orange"
android:textSize="30sp" />
<Button
android:id="#+id/indice2de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="190dp"
android:onClick="indice2"
android:text="#string/indice2"
android:textColor="#color/rouge"
android:textSize="30sp" />>
<ImageButton
android:id="#+id/flechedroite2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="196dp"
android:layout_y="338dp"
android:src="#drawable/flechedroite" />
<ImageButton
android:id="#+id/flechegauche2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="339dp"
android:src="#drawable/flechegauche" />
</AbsoluteLayout>
</RelativeLayout>
Change final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1); to final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche2);. You have not declared android:id="#+id/flechegauche1" in your xml
I have a dialog that I launch with:
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
The problem is that when I click the button it does not dismiss the dialog.
My dialog xml is:
<?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:orientation="vertical" >
<TextView
android:id="#+id/tastePickTitle"
android:text="Select a Taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/taste_array"
/>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="#+id/ammountPickTitle"
android:text="How much taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/ammount_array"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="1"
/>
<Button
android:id="#+id/addTasteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_weight="1"
android:onClick="addTasteNow" />
</LinearLayout>
</LinearLayout>
Here is the full taste tag java code to give the snipet above some more context:
public class TasteTags extends Activity {
BeerData e;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tastetag_page);
//get beer data
Intent intent = getIntent();
Bundle b = intent.getExtras();
e = b.getParcelable("myBeerObject");
TextView beerTitle = (TextView) findViewById(R.id.beerTitleTaste);
beerTitle.setText(e.beerName + " Taste Profile");
String url = "myURL";
url = url + "b=" +e.beerId;
//async task to get beer taste tag percents
new GetTasteJSON(this).execute(url);
}
public void addTaste(View v){
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("dialog", "cancel pressed");
dialog.dismiss();
}
});
dialog.show();
}
}
make final Dialog dialog = null; a global variable (activity level), remove the final and initialize at it where you have it right now, that is fine.
Looking at your code nothing jumps out, if you post your full code or a simple example to replicate i'm sure someone could fully decipher the problem. I have included some sample snippets that show a dialog that work, take a look at those to see if they shed any insight as to why yours does not work.
One thing to check before looking at the working sample, make sure you don't have a button in your dialog view with id 'cancelButton' defined twice(common from copy/paste errors). This will cause the behavior you are seeing. If you take the working example below and use the following view it will reproduce your behavior:
Broken Dialog View
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
See below for working example.
Dialog View:
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
Application class
package com.example.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Add Taste");
Button dialogButton = (Button) dialog.findViewById(R.id.button1);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}