Comparing the background resources of buttons - android

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);
}

Related

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!

Espresso - how can you perform an OR ( || ) check?

I need only one of these two espresso checks to pass. How can I achieve this? If either of these ViewAssertions pass, I will be happy.
onView(withId(R.id.mainview))
.check(
matches(not(isDisplayed()))
)
or
onView(withId(R.id.mainview))
.check(
doesNotExist()
)
This isn't the best method but it'll work. Ideally you should use a custom view matcher... but we're all lazy.
try {
// See if view is displayed
onView(withId(R.id.mainview))
.check(
matches(not(isDisplayed()))
);
}
catch (NoMatchingViewException e){
// Otherwise check it doesn't exist
onView(withId(R.id.mainview))
.check(
doesNotExist()
);
}
I think your only option is a custom matcher.

How to check if a Checkbox is checked?

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.

If-else statement for images.setvisibilty

i wanna ask about how can i do an if-statement using the code below? I have tried it and it give me an error; its say cannot convert void to bool. any suggestion?
if(images.setVisibility(View.GONE)){
Display();
}
You need to get visibilty and check it..
if(images.getVisibility()==View.GONE){
Display();
}
You can simply use below of the two codes:
if(images.getVisibility==view.GONE)
{
Display();
}
the getVisibility() returns an integer.
you can also check with the integer code
i.e.
VISIBLE-0
GONE-8
INVISIBLE-4
2.
if(images.getVisibility==0)
//VISIBLE
if(images.getVisibility==4)
//INVISIBLE
if(images.getVisibility==8)
//GONE
Try this:
if(images.getVisibility()==View.GONE){
Display();
}
Make sure in your code, images.setVisibility() images must be instance of View. then use View.getVisibility() method
if(images.getVisibility() == View.GONE)
Display();

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