vlio20
vlio20

Reputation: 9295

toggling background of button not working

I have created an app that suppose to toggle the button background color on every click. Here is the code:

package com.example.flash.light;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button screen = (Button) findViewById(R.id.screen);
        Drawable background = getResources().getDrawable(R.drawable.black);
        screen.setBackgroundDrawable(background);

        screen.setOnClickListener(new Button.OnClickListener(){ 
            public void onClick(View v) { 
                Button screen = (Button) findViewById (R.id.screen);
                Drawable background = screen.getResources().getDrawable(screen.getId());
                if(background == getResources().getDrawable(R.drawable.black)){
                    Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
                    screen.setBackgroundDrawable(background);
                }
                if(background == getResources().getDrawable(R.drawable.white)){
                    Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
                    screen.setBackgroundDrawable(background);
                }
            } 
        }); 
    }

Click on the button doesn't response. Thanks

Upvotes: 1

Views: 225

Answers (2)

Danyal
Danyal

Reputation: 458

Try this :

public class MainActivity extends Activity {

    private boolean isBlack = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        // your code

        final Button screen = (Button) findViewById (R.id.screen);
        isBlack = true;
        screen.setBackgroundColor(Color.BLACK);
        screen.setOnClickListener(new Button.OnClickListener(){ 
            public void onClick(View v) { 
                if (isBlack){ 
                    screen.setBackgroundColor(Color.WHITE);
                    isBlack = false;
                }
                else{
                    screen.setBackgroundColor(Color.BLACK);
                    isBlack = true;
                }
            }

        });
}
}

Upvotes: 1

omg_scout
omg_scout

Reputation: 21

I belive the problem here is that the resource returned by your

getResources().getDrawable(int id)

is different everytime you call it. Android just constructs new instance of drawable class instead of returning old one. (At least I belive that there is no caching in this case)

Comparing three different instances with == operator will never return true.

Second thing is an obvious bug in the code:

            Button screen = (Button) findViewById (R.id.screen);
            Drawable background = screen.getResources().getDrawable(screen.getId());
            if(background == getResources().getDrawable(R.drawable.black)){
                Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
                screen.setBackgroundDrawable(**newBackground**);
            }
            if(background == getResources().getDrawable(R.drawable.white)){
                Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
                screen.setBackgroundDrawable(**newBackground**);
            }

instead of background you should have newBackground there.

Upvotes: 2

Related Questions