I'm in a situation where I have four radiobutton, next button and one question. now my issue is that if the user clicks next button without selecting any option it should show a toast message and if user clicks any one of the radio button, it should be checked whether it is right or wrong. I have partially implemented but I cant get the exact solution. any one help me with this.
Thanks in advance
I have posted my code below
Next.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
count++;
if(viewFlipper.getDisplayedChild()==0)
{
id1 = rag1.getCheckedRadioButtonId();
rab1=(RadioButton)findViewById(id1);
//System.out.println("1nd question answer"+rab1.getText());
if (rag1.getCheckedRadioButtonId()!=R.id.btn1_1||rag1.getCheckedRadioButtonId()!=R.id.btn1_2||rag1.getCheckedRadioButtonId()!=R.id.btn1_3||rag1.getCheckedRadioButtonId()!=R.id.btn1_4||rag1.getCheckedRadioButtonId()!=R.id.btn1_5&&rag1.getCheckedRadioButtonId()==-1)
{
Toast.makeText(getApplicationContext(), "Please enter the answer", Toast.LENGTH_SHORT).show();
}
if((rag1.getCheckedRadioButtonId()!=-1)&&rab1.getText().equals(c_alcorrectanswer.get(0)))
{
System.out.println("1nd question answer"+rab1.getText());
}
else
{
Toast.makeText(getApplicationContext(), "Please enter the correct answer", Toast.LENGTH_SHORT).show();
viewFlipper.stopFlipping();
}
}
You should check within one condition only. Whether any of your RadioButton of your RadioGroup is selected or not.
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
if(radioButtonID > 0) //return -1 if none of radiobutton is selected from radiogroup
{
//move to next
}
else
{
// show toast. Please select any option.
}
As per your code snippet:
if(viewFlipper.getDisplayedChild()==0)
{
id1 = rag1.getCheckedRadioButtonId();
if(id1 < 0) //return -1 if none of radio button is selected
{
//Toast. Please select any answer
}
}
developer.android.com documentation reference
Related
I have been looking everywhere but i can't find the answer, i'm just a beginner, thanks in advance for all answers.
I want to put two editText's in activity_main.xml and when the app is open on the phone the user can write some numbers in those two editTexts and then click on Button, and the sum of these two editTexts to output in textView.
try this on button submit.
`btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(validation()){
int result = Integer.parseInt(edObjOne.getText().toString().trim())+
Integer.parseInt(edObjTwo.getText().toString().trim());
textViewObj.setText(String.valueOf(result));
}
}
});`
private boolean validation() {
if (edObjOne.getText().toString().trim().equals("")) {
Toast.makeText(this, "Field one empty", Toast.LENGTH_SHORT).show();
return false;
}
if (edObjTwo.getText().toString().trim().equals("")) {
Toast.makeText(this, "Field two empty", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
set edittext property number & android:digits="1234567890" in xml layout.
.
I am developing Quiz App. In which there are two buttons for previous question and next question.What i want to do is when user selects answer from options (for options there are 4 radio buttons) and attempts next question but if the user wants to view previous question that time the same radio button should be enabled which ever previously selected by user. For that i used shared preferences for storing selected radio button id.
Here is my Code:
For next button click:
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int radioSelected = radioGroup.getCheckedRadioButtonId();
int userSelection = getSelectedAnswer(radioSelected);
sharedPreferences=getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("Ans",userSelection);
editor.commit();
int correctAnswerForQuestion =firstQuestion.getCorrectAnswer();
if(userSelection == correctAnswerForQuestion)
{
Toast.makeText(getApplicationContext(),"Correct Answer",Toast.LENGTH_LONG).show();
currentQuizQuestion++;
if(currentQuizQuestion>=quizCount)
{
Toast.makeText(getApplicationContext(),"End of quiz",Toast.LENGTH_LONG).show();
return;
}
else
{
firstQuestion=parsedObject.get(currentQuizQuestion);
txtquestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers=firstQuestion.getAnswers().split(",");
uncheckedRadioButton();
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
}
else
{
Toast.makeText(getApplicationContext(),"You choose wrong answer",Toast.LENGTH_LONG).show();
currentQuizQuestion++;
firstQuestion=parsedObject.get(currentQuizQuestion);
txtquestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers=firstQuestion.getAnswers().split(",");
uncheckedRadioButton();
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
}
});
And for previous button:
previousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
currentQuizQuestion--;
if(currentQuizQuestion<0)
{
return;
}
checkedRadioButton();
firstQuestion=parsedObject.get(currentQuizQuestion);
txtquestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers=firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
});
JSON_DATA_WEB_CALL();
}
private void checkedRadioButton() {
int ans=1;
int ans1=sharedPreferences.getInt("Ans",ans);
if(optionOne.getId()==ans1)
{
optionOne.setChecked(true);
}
}
Please help me. Thanks a lot.
Add tag to each radio button option optionOne.setTag("answer_tag", "one").
Store the selected tag, and compare radiobuttons.getTag("answer_tag) with the stored tag.
Hlw,
Problem1:
I am developing a app where I have a ImageView like favorite button where I click and change this image resource(it is done), but I want to set action when user again click this imageview and the image remain same.
Problem2:
I have two image button "plus" and "minus" I set condition to it that when user click + button middle textview increment by 1, and when it reach 10 then the button will unclickable, also for the minus button, when textviw equal=0 then it not works...
I done it by condition but when minus button reach 0 and after plus button click, it increment but not decrement it remain unclickable...
how can i solve this problem?
Image like this
productWrapper.plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context, "Plus", Toast.LENGTH_SHORT).show();
try
{
String presentValStr= finalProductWrapper2.selectedQuantity.getText().toString();
int presentIntVal=Integer.parseInt(presentValStr);
presentIntVal++;
if (presentIntVal>=10){
Toast.makeText(context,"You can select max 10 product",Toast.LENGTH_LONG).show();
finalProductWrapper2.plus.setEnabled(false);
}
finalProductWrapper2.selectedQuantity.setText(String.valueOf(presentIntVal));
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(context,"Error! please try again",Toast.LENGTH_LONG).show();
}
}
});
final ProductWrapper finalProductWrapper = productWrapper;
productWrapper.heart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finalProductWrapper.heart.setImageResource(R.drawable.heart2);
}
});
Point #1.
You can use boolean flag like below
if(flag == true){
flag = false;
// Change your Image Resource here
}
else {
flag = true;
// Do your other action
}
for Minus button put following condition
if(YOUR_POINTS >0){
// Do your action
}
else {
// Ignore
}
I'm working on a school project in Android Studio (a small game) and I now want to implement scores.
So here is the code where I want to implement the score:
private int score = 0;
final View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleResult1 > doubleResult2) {
Log.v("TAG", "you are right");
// Add 1 point to score
generatorEasy1();
generatorEasy2();
}
else {
Log.v("TAG", "you are wrong");
goToEndscreen(); // Go to Endacreen when wrong
}
}
};
So whenever you pressed the right button I want to add 1 point to the score. I've tried the following:
if(v.equals(buttonBigger) && doubleResult1 > doubleResult2) {
Log.v("TAG", "you are right");
score += 1;
textScore.setText("Score : " + score);
}
The problem is that if I run the app and I press the right button, the score stays 0.
I don't know what I'm doing wrong so it would be nice if someone could help me.
Not sure what you are trying to do with this game. But, you can set an on click listener directly on the buttons that are to be clicked. No need to add an if statement to check which view is initiating the listener.
biggerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Whatever you want to do when the
// button is clicked in here.
}
});
I have a star image in action bar. When I click on it, then the image should change to ON star. This is working fine. But how to know whether image is at ON state or OFF state.I want something that, if the image is at OFF mode and user taps on ON star, then it should set to ON star image. Initially, the image is set to OFF mode. So for this, I've wrote give line to turn on as user tap on it :
v.setBackgroundResource(android.R.drawable.star_big_on);
Guys pls suggest me for OFF mode if star image is already on. I am not getting any idea.
you can check it easily by setting tag of each image or by comparing there resource
comparision of resource method is shown below:
if (regProfile.getDrawable().getConstantState() ==
getResources().getDrawable(R.drawable.ivpic).getConstantState()){
Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show();
}
If my guess is correct you can achieve the functionality like this.change this code accordingly for image onclick
private Button button;
public static boolean isclick=false;
button.setOnClickListener(seathtlistner);
private View.OnClickListener seathtlistner = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(isclick){
button.setBackgroundResource(R.drawable.onstarimage);
}else{
button.setBackgroundResource(R.drawable.offstarimage);
}
isclick=!isclick;
}
You can use Tag property.
img_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Boolean tag_isOn = (Boolean) v.getTag();
tag_isOn = !tag_isOn;
v.setBackgroundResource(tag_isOn ?android.R.drawable.star_big_on:android.R.drawable.star_big_off);
v.setTag(tag_isOn);
}
});
if (img_like.getTag() != null && img_like.getTag().toString().equals("red")) {
img_like.setImageResource(R.drawable.heart);
img_like.setTag("heart");
} else {
img_like.setImageResource(R.drawable.red);
img_like.setTag("red");
}`enter code here`