How to check if a Checkbox is checked? - android

I have two check boxes,and I want to set tag as per its selection or check status,so can any one tell me how to set tags in my application,the following is my code tell me my mistakes,..
if(matchcheck.isChecked())//checked then
{
matchcheck.setTag("N1");//set No 1..
}
else
{
matchcheck.setTag("Y1");//set yes 1
}
if(newsletter.isChecked())
{
newsletter.setTag("N2");//set no 2
}
else
{
newsletter.setTag("Y2");//set yes 2
}

I think you are just setting the opposite of what you want.
if(matchcheck.isChecked())//checked then
{
matchcheck.setTag("Y1");//set yes 1
}
else
{
matchcheck.setTag("N1");//set No 1
}
if(newsletter.isChecked())
{
newsletter.setTag("Y2");//set yes 2
}
else
{
newsletter.setTag("N2");//set no 2
}
Should be your code.
But anyway, consider using RadioButtons and giving them the look and feel you want. It will be easier, they are intended for these kind of situations.

Related

Why does my radio buttons goes to "Please Try Again" after I clicked on the right answer

I have a problem. After I created a question, it suddenly goes to please try again rather than the answer is correct. Can you please explain what the problem is?
submitbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(questionchoice1.isChecked() || questionchoice2.isChecked() || questionchoice3.isChecked()) {
if (questionchoice1.equals(quizAnswer)) {
mAnswer.setText("The answer is correct!");
mAnswer.setVisibility(View.VISIBLE);
}
else if(questionchoice2.equals(quizAnswer))
{
mAnswer.setText("The answer is correct!");
mAnswer.setVisibility(View.VISIBLE);
}
else if(questionchoice3.equals(quizAnswer))
{
mAnswer.setText("The answer is correct!");
mAnswer.setVisibility(View.VISIBLE);
}
else if (!questionchoice1.equals(quizAnswer)){
mAnswer.setText("Please try again!");
mAnswer.setVisibility(View.VISIBLE);
}
else if (!questionchoice2.equals(quizAnswer)){
mAnswer.setText("Please try again!");
mAnswer.setVisibility(View.VISIBLE);
}
else if (!questionchoice3.equals(quizAnswer)){
mAnswer.setText("Please try again!");
mAnswer.setVisibility(View.VISIBLE);
}
}
else
{
mAnswer.setText("Please select an answer");
mAnswer.setVisibility(View.VISIBLE);
}
}
});
Your questionchoice is a radio button and your quizAnswer is a variable of a different type (String, int, ...? I'm guessing String if you're using .equals()). So the method is not comparing the value of the radio button, but its reference. Please post your layout and the variable types you're using and I can update my answer with further info on how to solve your problem.
UPDATE after comment:
Get the value of the radio button that’s checked using:
String quizChoice1 = questionchoice1.getText();
Then add the following to your check conditions:
if (quizChoice1.equals(quizAnswer)) {
//answer is correct
} else {
//answer is wrong
}
"questionchoice1" is a radio button so you have to get the text of the Radio button first then compare that text to value in your variable "quizAnswer".
in java we can get text of radio button as
String value = questionchoice1.getText();
and in kotlin:
val value = questionchoice1.Text
then we can compare tat value to variable as
value.equal(quizAnswer)

Set correct and incorrect buttons' templates simultaneously

I am having a method, handling the correct/incorrect answers entered in a quiz game:
public void answerButtonClickHandler(View v)
{
Answer answer = myAnswers.get(v.getId());
if (answer != null)
{
myQuestionsAnswered++;
if (answer.isCorrect())
{
myCorrectlyAnswered++;
v.setBackgroundResource(R.drawable.answer_button_correct);
}
else
{
v.setBackgroundResource(R.drawable.answer_button_incorrect);
}
}
What it is doing so far is to check if the answer is correct or not (this part works fine) and sets a corresponding template. What I'd want to add as functionality is when the wrong answer is marked, the correct one to be marked as well. I was trying to keep the state of the vies (v) in a new View variable, in both of the states, but have not done the trick so far. Thanks you!

Handle 2 conditions in if

If(bool1&&bool2)
{
//some code
} else{
//some code
}
And here what I want is that even if 1 condition return true then also to proceed to run the code but I also want to know which condition returned true
Edit
I'm using RootTools library for checking root permission
RootTools.isRootAvailable() &&RootTools.isAcessgiven()
So if
RootTools.isAcessgiven() returns false
I can tell root is not proper but if
RootTools. isAcessgiven() returns true and RootTools. isRootAvailable () returns true
I can tell him root is proper
If you just want to know which one returned true, you could just log it:
if (bool1 || bool2) {
log.i("bool1: " + bool1 ", bool2: " + bool2)
code......
}
then you need two if statements.
if(bool1) {
// code
} else if(bool2) {
// slightly different code
} else {
// final code
}
no way around it
You mean proceed to run code "//someother code" ?
if(bool1 || bool2){
// fun first code for any of bool1 or bool2 - true
// you need to check which one was true - bool1, or bool2 in other statement.
} else {
// run this if bool1 and bool2 is false
}
The one possible answer could be , Here both the if block will run .
if(bool1){
// some code
// print(in bool 1)
}
if(bool2)
{
// some code
// print(in bool 2)
}
The other answer you can do is my maintaining a another variable
boolean val1,val2;
if(val1=bool1 || val2=bool2 )
{
}

Comparing the background resources of buttons

button.setBackgroundResource(R.Drawable.abc);
if ( button.getBackground()==getResources().getDrawable(R.drawable.abc))
{
button.setBackgroundResource(R.drawable.xyz);
}
else if( button.getBackground()==getResources().getDrawable(R.drawable.xyz) )
{
button.setBackgroundResource(R.drawable.abc);
}
I want to compare the background images set on the button. The above code has been taken from Stack Overflow... but it doesn't seem to work
Please suggest a better method.
Try this
if ( button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.abc).getConstantState())
{
button.setBackgroundResource(R.drawable.xyz);
}

Remove the background of a textview

I set a background for a textview and I want to remove it dynamically but it dosen't work,
are there any suggestion?
if (mToday) {
monthView[mRow][mColumn].setBackgroundResource(R.color.black);
}
else {
monthView[mRow][mColumn].setBackgroundResource(0);
}
I found a reasonable explanation here why it is happen, but again didn't solve the problem.
try this.
txtEmail.setBackgroundResource(android.R.color.transparent);
try the following code just changed 0 to null in .setBackgroundDrawable thats all it will work check once :
if (mToday)
{
monthView[mRow][mColumn].setBackgroundResource(R.color.black);
}
else
{
monthView[mRow][mColumn].setBackgroundDrawable(null);
}
I think this should work
monthView[mRow][mColumn].setBackgroundDrawable(null);

Categories

Resources