I am creating a small quiz app. As the question suggests, how do I prevent a second click without disabling the button? When I click twice on a radio button it adds 2 points instead of 1. Very much appreciated!
Here's a picture of my app:
public void question1 (View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question1_9:
if (checked) {
scoreForRadioButtons += 1;
}
case R.id.question1_8:
if (checked) {
break;
}
case R.id.question1_7:
if (checked) {
break;
}
}
}
SOLVED
This is what I did: just add 'break;' on the correct answer.
public void question1 (View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question1_9:
if (checked) {
pointForQ1 = 1;
break;
}
case R.id.question1_8:
if (checked) {
pointForQ1 = 0;
}
case R.id.question1_7:
if (checked) {
pointForQ1 = 0;
}
}
}
Short Answer:
You can set the clickable attribute to false for the RadioButton after it is clicked if you really want to using the following:
myRadioButton.setClickable(false);
Long Answer:
Don't use radio buttons for this. Users should be able to click a radio button as many times as they want without anything happening like incrementing the score in your case. It's a standard convention of a radio button that once selected, no new code is executed if it is selected again and again. Note this is only if the state of the radio button hasn't changed during selections (state meaning whether it's selected or not).
The way your app is using (or wants to use) RadioButtons is not correct. I'd recommend using buttons for this, where you could do the following:
myButton.setEnabled(false);
Edit:
If you want to make it visible to the user that they clicked a button, you can do a few things (change it's text, change the background colour). For this example, you can change the background colour with this:
myButton.setBackgroundColor(Color.GREEN); // or whatever colour you choose
If you want to do this add the following to your imports:
import android.graphics.Color
If you just don't want to click that radio button two times, After radio button is clicked,
add view.setEnabled(false);or
view.setClickable(false); to prevent it click again
To prevent the second click on a checked radio button, make that radio disabled
radioButton.setEnabled(false);
Disable a clicked button and enable it when another is checked
public void question1 (View view) {
//will enable all buttons
Integer[] buttons = {R.id.question1_9, R.id.question1_8, R.id.question1_7};
for (int i = 0; i < buttons.length; i++) {
findViewById(buttons[i]).setEnabled(true);
}
RadioButton button = (RadioButton) view;
if (button.isChecked()){
switch (view.getId()) {
case R.id.question1_9:
scoreForRadioButtons ++;
//will disable clicked button
button.setEnabled(false);
break;
case R.id.question1_8:
break;
case R.id.question1_7:
break;
}
}
Related
I use Android Studio.
I have two editText named: E1, E2 and three buttons named: btn1, btn2, btn3
When I press button, it would insert some word in editText.
For example: When I press btn1, it would insert "cat" in edittext.
But now, I don't know which edittext does student want to insert. How I can detect cursor?
I hope when I detect cursor, I know student which edittext will be insert
this is my code:
private Button.OnClickListener btn=new Button.OnClickListener(){
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
s=s+"cat";
E1.setText(s);
E2.setText(s);
//I dont know whether the student want to insert E1 or E2
// how can I do,thank;
break;
case R.id.btn2:
s=s+"apple";
E1.setText(s);
E2.setText(s);
//same problem .....
break;
case R.id.btn3:
s=s+"dog";
E1.setText(s);
break;
}
}
};
Thank.
check the focus of EditText:
if(EditText1.isFocused()){
//EditText1 is focused
}else if(EditText2.isFocused()){
//EditText2 is focused
}
i've created a function and set it to onClick, so every time i hit the button this function is called. It is called, but the value of 4 other functions i can't get them in setMessage() function(when we create a dialog box). So the logic is like, after hitting the button first we check which radioId1 is equal to which id, and call the appropriate fucntion(video,cons,news,pict).These 4 funct. return some string. Then i want to add these strings to some text in my dialog box, like ....SetMessage(checkMyId() + "Some text").SetPositiveButton(...)...
but the problem is my function checkMyId is set to onClick, so it recieves an argument View v, and when i call it "mannualy" in SetMessage() it shows error, because i don't know what to put inside that parenthesis. So the question is, what type of data(or whatever) is that View ? i saw some documentation and didn't understood much or at least how to implement that in my problem.
public void checkMyId(View v) {
int radioId1 = radioGroup.getCheckedRadioButtonId();
if (radioId1 == R.id.radio_one) {
video();
} else if (radioId1 == R.id.radio_two) {
pict();
} else if (radioId1 == R.id.radio_three) {
cons();
} else news();
}
EDITED
Picture of the error
sir you have 2 way
first try this ( HINT : Don't forget to implement ONCLICK in activity )
public void checkMyId(View v) {
switch (v.getId()) {
case R.id.radio_one:
video();
break;
case R.id.radio_two:
pict();
break;
case R.id.radio_three:
cons();
break;
default:
news();
break;
}
}
sec you can use it in OnCreate ( HINT : this on radio group ) HERE
(findViewById(R.id.radio_one)).setOnCheckedChangeListener(new
OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
____ READ This also for single RB
Whenever I answer a question and hit the submit button, a score of 1 should show but on hitting submit button again, it keeps adding 1 to the score and more hits on the submit button keeps adding more 1 to the score. I actually don't want 1 to be added to the score every time the submit button is clicked. I do not want the score to be updated based on the number of times I hit the submit button.
package com.example.android.generalknowledge;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int baseScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitResult(View view) {
RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
boolean answerIsLargestRailwayStation = largestRailwayStation.isChecked();
RadioButton insects = (RadioButton) findViewById(R.id.insects);
boolean answerIsInsects = insects.isChecked();
CheckBox physicsChemistry = (CheckBox) findViewById(R.id.physics_chemistry);
boolean physicsChemistryIsAnswer = physicsChemistry.isChecked();
CheckBox physiologyMedicine = (CheckBox) findViewById(R.id.physiology_medicine);
boolean physiologyMedicineIsAnswer = physiologyMedicine.isChecked();
CheckBox literature = (CheckBox) findViewById(R.id.literature);
boolean literatureIsAnswer = literature.isChecked();
CheckBox peaceEconomics = (CheckBox) findViewById(R.id.peace_economics);
boolean peaceEconomicsIsAnswer = peaceEconomics.isChecked();
RadioButton naziParty = (RadioButton) findViewById(R.id.nazi_party);
boolean answerIsNaziParty = naziParty.isChecked();
RadioButton allOfTheAbove = (RadioButton) findViewById(R.id.all_of_the_above);
boolean answerIsAll = allOfTheAbove.isChecked();
RadioButton filmFinance = (RadioButton) findViewById(R.id.film_finance);
boolean answerIsFilmFinance = filmFinance.isChecked();
EditText enterNameHere = (EditText) findViewById(R.id.name_view);
String name = enterNameHere.getText().toString();
EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
String answer = enterAnswerHere.getText().toString();
if (enterAnswerHere.getText().toString().equals("Africa")) {
baseScore += 1 ;
}
int finalScore = calculateTotalScore(answerIsLargestRailwayStation, answerIsInsects, physicsChemistryIsAnswer,
physiologyMedicineIsAnswer, literatureIsAnswer, peaceEconomicsIsAnswer, answerIsNaziParty, answerIsAll, answerIsFilmFinance);
Toast.makeText(MainActivity.this,
name + " " + "\n" + "You have a Total Score of " + " " + finalScore + " " + "/10", Toast.LENGTH_LONG).show();
}
/**
* This method is called when any of the radio buttons in question one is selected
*/
public void onRadioButtonClickedOne(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.largest_railway_station:
if (checked)
// This is the correct answer
break;
case R.id.highest_railway_station:
if (checked)
// Wrong answer
break;
case R.id.longest_railway_station:
if (checked)
// Wrong answer
break;
case R.id.none_of_the_above:
if (checked)
//Wrong answer
break;
}
}
/**
* This method is called when any of the radio buttons in question two is selected
*/
public void onRadioButtonClickedTwo(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.behaviour_of_human_beings:
if (checked)
// Wrong answer
break;
case R.id.insects:
if (checked)
// This is the correct answer
break;
case R.id.origin_history:
if (checked)
// Wrong answer
break;
case R.id.rock_formation:
if (checked)
// Wrong answer
break;
}
}
/**
* This method is called when the checkboxes for question three are clicked
*/
public void onCheckboxThreeClicked(View view) {
//Is the button now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox is clicked
switch (view.getId()) {
case R.id.physics_chemistry:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.physiology_medicine:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.literature:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.peace_economics:
if (checked)
// One of the Correct answers
break;
}
}
/**
* This method is called when any of the radio buttons in question four is selected
*/
public void onRadioButtonClickedFour(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.labour_party:
if (checked)
// Wrong answer
break;
case R.id.nazi_party:
if (checked)
// This is the correct answer
break;
case R.id.leading_party:
if (checked)
// Wrong answer
break;
case R.id.democratic_party:
if (checked)
// Wrong answer
break;
}
}
/**
* This method is called when any of the radio buttons in question six is selected
*/
public void onRadioButtonClickedSix(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.develop_telescope:
if (checked)
// Wrong answer
break;
case R.id.discovered_jupiter:
if (checked)
// Wrong answer
break;
case R.id.movement_of_pendulum:
if (checked)
// Wrong answer
break;
case R.id.all_of_the_above:
if (checked)
// This is the correct answer
break;
}
}
/**
* This method is called when any of the radio buttons in question seven is selected
*/
public void onRadioButtonClickedSeven(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.foreign_finance:
if (checked)
// Wrong answer
break;
case R.id.film_finance:
if (checked)
// This is the correct answer
break;
case R.id.federation_football:
if (checked)
// Wrong answer
break;
case R.id.none:
if (checked)
// Wrong answer
break;
}
}
private int calculateTotalScore(boolean questionOneAnswer, boolean questionTwoAnswer, boolean questionThreeAnswer1,
boolean questionThreeAnswer2, boolean questionThreeAnswer3, boolean questionThreeAnswer4,
boolean questionFourAnswer, boolean questionSixAnswer, boolean questionSevenAnswer) {
if (questionOneAnswer) {
baseScore += 1 ;
}
if (questionTwoAnswer) {
baseScore += 1 ;
}
if (questionThreeAnswer1) {
baseScore += 1 ;
}
if (questionThreeAnswer2) {
baseScore += 1 ;
}
if (questionThreeAnswer3) {
baseScore += 1 ;
}
if (questionThreeAnswer4) {
baseScore += 1 ;
}
if (questionFourAnswer) {
baseScore += 1 ;
}
if (questionSixAnswer) {
baseScore += 1 ;
}
if (questionSevenAnswer) {
baseScore += 1 ;
}
// EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
// String answer = enterAnswerHere.getText().toString();
// {
// if (answer == "Africa") {
// baseScore = baseScore + 1;
// }
// }
return baseScore;
}
}
We can achieve by 2 ways,
You can diable the button by button.setEnable(false); once user click for for time
In your case if you don't have button object so you can use one global boolean and make that as false default and once user click for first time do the process and make that boolean as true, from next time check if the boolean is true just return it.
private boolean mIsSubmited = false;
public void submitResult(View view) {
if(mIsSubmited) {
return;
}
mIsSubmited = true;
RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
// Remaining code..
}
You can disable the submit button once it is clicked. For eg:
onclick() {
submitbutton.setEnabled(false);
}
Prevent user to click on submit again and again.
You can enable it back using:
submitbutton.setEnabled(true);
You can declare a Boolean globally and set it to false at first.
then on button click you can turn its value to true.
on the button click you can check if its false then only to increase the score.
You can use handler for this may be there is better solution then this.
Handler mHandler=new Handler();
//now in your click listener
mHandler.removeCallbacks(mRunnable);//
mHandler.postDelayed(mRunnable, 500);
Runnable mRunnable =new Runnable() {
#Override
public void run() {
//calculate it
}
};
//Wait for half sec before calculating the result if user clicked before then stop runnable from calcualting and again go for calculation.
Hope it helps.
I have several layouts; each containing a TextView. If the user drags a TextView unto another TextView, I'm going to execute the method swapFamily() but it fires several times, depending on the number of families.
FamilyItemLayout is just a custom LinearLayout with a String attribute attached to it and with add'tl getters and setter for that attribute.
However, my issue is that,
private class onDropFamily implements View.OnDragListener {
#Override
public boolean onDrag(View view, DragEvent event) {
TextView txtDragged = (TextView) event.getLocalState();
TextView txtTarget = (TextView) view;
String familyDragged = ((FamilyItemLayout) txtDragged.getParent()).getFamily();
String familyTarget = ((FamilyItemLayout) txtTarget.getParent()).getFamily();
switch (event.getAction()) {
// Signals the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
break;
// Signals to a View that the drag point has entered the bounding box of the View.
case DragEvent.ACTION_DRAG_ENTERED:
view.setBackgroundResource(R.mipmap.pill_sun);
break;
// Signals that the user has moved the drag shadow outside the bounding box of the View.
case DragEvent.ACTION_DRAG_EXITED:
view.setBackgroundResource(R.mipmap.pill_sky);
break;
// Signals to a View that the user has released the drag shadow, and the drag point is within the bounding box of the View.
case DragEvent.ACTION_DROP:
break;
// Signals to a View that the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
// Check if drag event was successful
if (dropEventHandled(event)) {
fixedPlan.swapFamily(familyDragged, familyTarget);
}
txtDragged.setVisibility(View.VISIBLE);
txtTarget.setBackgroundResource(R.mipmap.pill_sky);
break;
}
return true;
}
private boolean dropEventHandled(DragEvent dragEvent) {
return dragEvent.getResult();
}
}
I believe you should put those code in ACTION_DROP instead of ACTION_DRAG_ENDED
http://developer.android.com/reference/android/view/DragEvent.html
All views that received an ACTION_DRAG_STARTED event will receive the
ACTION_DRAG_ENDED event even if they are not currently visible when
the drag ends.
Also remember to return correct value for ACTION_DROP
The View should return true from its onDragEvent(DragEvent) handler or
OnDragListener.onDrag() listener if it accepted the drop, and false if
it ignored the drop.
i am developing/designing a quiz application in android, on the testActivity i have
a TextView which loads the questions and four answer(alternatives) buttons.
btnAnswer1, btnAnswer2, btnAnswer3, and btnAnswer4.
Problem:When i click on btnAnser1 as my answer, it should remain in selected state, however it should also be possible to unselect(normal state) it if i doubt the answer. and lastly if its selected, and i decide to go for btnAnswer2 whiles btnAnswer1 is selected, immediately i select btnAnswer2, btnAnswer1 should be unselected(normal) whiles btnAnswer2 is selected.
i hope my question is clear and it sounds quiet easy but am still new in android programming so will appreciate the help.
thanks.
optionone = (Button) findViewById(R.id.optionone);
optionone.setTag(1);
optionone.setId(i);
//optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
optionone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setSelected(true);
checkAnswer();
final int change = (Integer) v.getTag();
if (change == 1) {
optionone.setBackgroundColor(Color.parseColor("#B2B2B2"));
back.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
v.setTag(0);
} else {
optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
back.setVisibility(View.GONE);
next.setVisibility(View.GONE);
v.setTag(1);
}
}
});
Try to use toggle buttons instead of buttons, or just change background color of buttons when they pressed, not state.