I want to do an Android App Quiz. I did a radiogroup with 4 radiobuttons that represent the 4 answers.
I want that when these radiobuttons aren't clicked the app shows an error, but my doesn't do it.
This my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
RadioGroup champ=(RadioGroup)findViewById(R.id.answer1);
champ.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#SuppressWarnings("null")
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId) {
case R.id.answer1A:
ans1 =0;
break;
case R.id.answer1B:
ans1 =1;
break;
case R.id.answer1C:
ans1 =2;
break;
case R.id.answer1D:
ans1 =3;
break;
case R.id.answer1E:
ans1 =4;
break;
default:
ans1=(Integer) null;
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
break;
}
}
});
Why?Can you help me, please?
First assign:
ans1=-1; //in onCreate or at the time of declaration
You might be having a button or something clicking on which you would be accessing which answer was checked, there you can check :
if(ans1==-1)
{
//show error message
}
else
{
//Success
}
This check is not executing because this is a checkedchangelistener which will trigger when you click on some radiobutton (when status is changed).
All you have to do is Check the status of the each and every radiobutton outside this listener.
Related
HELP ME PLEASE! I want to create 2 buttons ("Next" and "Previous") that will change the text in TextView. I made a switch and "systemcounter" to change the cases, which then will set another text in TextView. When I test my program in this window buttons do not change the pages. I think this is because the system cannot see the "systemcounter"
private Button next_button;
private Button previous_button;
private TextView Text_set1;
int systemcounter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learningpage);
next_button = (Button) findViewById(R.id.next_button);
previous_button = (Button) findViewById(R.id.previous_button);
Text_set1 = (TextView) findViewById(R.id.Text_set);
next_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
systemcounter = systemcounter + 1;
}
});
previous_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
systemcounter = systemcounter - 1;
}
});
switch (systemcounter) {
case (0):
Text_set1.setText("Thermodynamics is the branch of physics that deals with the relationships between heat and other forms of energy.");
previous_button.setVisibility(View.INVISIBLE);
break;
case (1):
Text_set1.setText("Hi there");
previous_button.setVisibility(View.VISIBLE);
break;
case (2):
Text_set1.setText("How are you");
previous_button.setVisibility(View.VISIBLE);
break;
case (3):
Text_set1.setText("How old are you?");
break;
default:
Text_set1.setText("OPS");
break;
}```
you have switch statement in oncreate method and so it executes it only once, make a seperate method like
setEditText(int systemcount) and create your switch tehre and call this methods from button onclick methods
private void setText() {
switch (systemcounter) {
case (0):
Text_set1.setText("Thermodynamics is the branch of physics that deals with the relationships between heat and other forms of energy.");
previous_button.setVisibility(View.INVISIBLE);
break;
case (1):
Text_set1.setText("Hi there");
previous_button.setVisibility(View.VISIBLE);
break;
case (2):
Text_set1.setText("How are you");
previous_button.setVisibility(View.VISIBLE);
break;
case (3):
Text_set1.setText("How old are you?");
break;
default:
Text_set1.setText("OPS");
break;
}
}
and call this method in your listeners (like this)
next_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
systemcounter = systemcounter + 1;
setText();
}
});
As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .
In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.
If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.
Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .
Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"
I tried to get source from SO but I can't find a clear solution for this .
If anyone helps me to come out from this ,that's much helpful for me .
coding
public class CreateAlarm extends Activity implements View.OnClickListener {
private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mRepeat = (TextView) findViewById(R.id.mRepeat);
mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);
mbtn_Sun.setOnClickListener((View.OnClickListener) this);
mbtn_Mon.setOnClickListener((View.OnClickListener) this);
mbtn_Tue.setOnClickListener((View.OnClickListener) this);
mbtn_Wed.setOnClickListener((View.OnClickListener) this);
mbtn_Thu.setOnClickListener((View.OnClickListener) this);
mbtn_Fri.setOnClickListener((View.OnClickListener) this);
mbtn_Sat.setOnClickListener((View.OnClickListener) this);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mRepeat.setText("");
} else
mRepeat.setText("Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mRepeat.setText("");
} else
mRepeat.setText("Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mRepeat.setText("");
} else
mRepeat.setText("Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mRepeat.setText("");
} else
mRepeat.setText("Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mRepeat.setText("");
} else
mRepeat.setText("Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mRepeat.setText("");
} else
mRepeat.setText("Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mRepeat.setText("");
} else
mRepeat.setText("Sat");
break;
default:
mRepeat.setText("Never");
}
}
}
Image :
By default the TextView text is "Never".
You can define a TreeMap as:
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...
My suggestion is: use a single TextView can make your logic quite complex
Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.
I am currently developing a questionnaire based application where by there are options. I need to assign numerical values to each radio button so when one of it is selected it will give a certain amount of points which will then be used to produce a cumulative amount. I need help in coding the java to do this. any help will be greatly appreciated. Thank you!
int score = 0;
public TextView tv;
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getID()) {
case R.id.radioButton1:
if(checked)
score +=1;
break;
case R.id.radioButton2:
if(checked)
score +=1;
break;
case R.id.radioButton3:
if(checked)
score +=3;
break;
}
}
public void updateScore(int score) {
tv = (TextView)findViewById(R.id.textview1);
tv.setText(" " + score);
}
in the XML I have coded a textview with "0" and a text id of #+id/textview1
the issue is that the score remains at 0 even if any radio buttons are selected
Make the following changes to your code
public TextView tv;
int score = 0;
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
score += 1;
}
break;
case R.id.radioButton2:
if(checked)
score +=1;
break;
case R.id.radioButton3:
if(checked)
score +=3;
break;
}
updateScore(score); // <- Added this line here
}
public void updateScore(int score) {
// Don't initialise your view here. Take it to onCreate()
tv.setText(" " + score);
}
EDIT
Is your XML for the radio button similar to what I have here?
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="asas"
android:onClick="onRadioButtonClicked"/>
Make sure you have added the android:onClick="onRadioButtonClicked" property to each of the radio buttons
I am creating this activity that I request the user to find the correct answer using check boxes. My layout has multiple checkboxes but one of them is correct one. The main idea is to give a user 3 tries to guess the correct answer( the one checkbox). I manage to implement a code that when the correct checkbox is clicked to do something, but I can't get the code to work when user is clicking on any other checkbox.
My code is below (currently implemented the correct checkbox, but code for any other checkbox does not work)
UPDATE
CheckBox centerBack, checkBox1;
int counts = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tennis_facts);
CheckBox centerBack = (CheckBox) findViewById(R.id.centerBack);
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
centerBack.setOnClickListener(this);
checkBox1.setOnClickListener((this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.centerBack:
Toast.makeText(getApplicationContext(), "correct", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox1:
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_SHORT).show();
break;
}
}
You mean these code below do not work, right ?
else{
counts ++;
DisplayToast("Incorrect Answer" + counts);
}
because you set listener for only this checkbox by code :
checkBox.setOnClickListener(new View.OnClickListener());
Others checkbox not set setOnClickListener yet. it a reason why when others checkbox checked, Android did not get this event
Should do:
checkBox.setOnClickListener(this);
checkBox2.setOnClickListener(this);
checkBox3.setOnClickListener(this);
checkBox4.setOnClickListener(this);
And You also should implement OnClickListener interface and override onClick method like this below :
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checkBox:
//your code:
break;
case R.id.checkBox2:
//your code:
break;
case R.id.checkBox3:
//your code:
break;
case R.id.checkBox4:
//your code:
break;
}
}
Try to imlement View.OnClickListener interface in your Activity. then add to every chechkBox:
checkBox.setOnClickListener(this);
Inside the onClick:
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checkBox1:
//your code:
break;
case R.id.checkBox2:
//your code:
break;
//so on...
}
}
I have 10 buttons set up which are the answers to ten questions. When a certain button is clicked, I have a switch statement set up in my onClick method shown below. My question is what is the best way to set up the OnClickListeners for all the buttons seeing that I need to pass 2 arrays to the onClick method in order to tell if it is correct or not? Also, I need to return and integer value. Thanks
public void onClick(View v, int[] qaarray, int questionorder) {
int x=0;
switch(v.getId())
{
case R.id.imageButton0:
if(qaarray[0] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton1:
if(qaarray[1] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton2:
if(qaarray[2] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton3:
if(qaarray[3] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton4:
if(qaarray[4] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton5:
if(qaarray[5] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton6:
if(qaarray[6] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton7:
if(qaarray[7] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton8:
if(qaarray[8] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton9:
if(qaarray[9] == questionorder){
//correct
}else{
//incorrect
}
break;
default:
throw new RuntimeException("Unknown button ID");
}
}
The OnClickListener only gives you one parameter, which is the View:
void onClick(View v);
But you don't have to pass the questions and 'order' to the method to have what you want. One of the technique you can use is the setTag() method of View:
int[] button = new int[] { R.id.imageButton1, R.id.imageButton2.... };
private class AnswerPair{
public int questionOrder;
public int answer;
}
public void onCreate(Bundle bundle){
for(int i=0; i<NO_OF_BUTTON; i++){
AnswerPair ans = new AnswerPair();
ans.questionOrder = i;
ans.answer = 0; // SET this
getViewById(button[i]).setTag(ans);
getViewById(button[i]).setOnClickListener(this);
}
}
public void onClick(View v){
if (v.getTag() == null) return;
try{
AnswerPair answer = (ans)v.getTag();
// Check answer == question order? index?
}catch(exception e) return;
}
You can implement as many OnClickListeners as you want and assign different listeners for each button.
public void onCreate(Bundle bundle){
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.myButton)
b.setOnClickListener(new MyListener());
}
private class MyListener implements OnClickListener {
#Override
public void onClick(View v) {
// Your code here
}
}
i think a lot of people know this already, but there's a shortcut you can use instead of having different instances of onClickListeners and assigning them in code using setOnClickListener(x).
In your button XML, give it the android:onClick property, and assign it a string you like, for example,
android:onClick="clickOne"
In the activity the sets this xml as its content view, create a method named clickOne with a View parameter.
public void clickOne(View view)
Whatever you place on this method will be executed when you click the button.