Shane
Shane

Reputation: 972

ImageButton Background Not Changing

I have an imageButton and and I would like to change the background of it in code but I cannot figure out why this will not work...

public class Game extends Activity{

    ImageButton btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        btn1 = (ImageButton) findViewById(R.id.btn1);
        btn1.setBackgroundResource(R.drawable.image1);
}

Upvotes: 1

Views: 117

Answers (1)

Sandeep P
Sandeep P

Reputation: 4411

you have to use

btn1.setBackgroundDrawable(R.drawable.image1);

to change the background

setBackgroundResource indcates src of an image where as src and background has lot of difference

Upvotes: 1

Related Questions