Button.setBackground(Drawable background) throws NoSuchMethodError

You might be testing on an API below level 16 (Jelly Bean).

The setBackground method is only available from that API level onwards.

I would try with setBackgroundDrawable (deprecated) or setBackgroundResource if that's the case.

For instance:

Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
Button one = new Button(this);
// mediocre
Button two = new Button(this);
// better

To create a homogeneous background for a View, you can create a drawable resource of type shape, and use that with the setBackgroundResource.


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#FF0000"/>    


Button b = (Button)findViewById(R.id.myButton);

But this will look pretty bad, flat and out of place. If you want a colored button that looks like a button, than you can either design it yourself (rounded corners, stroke, gradient fill...) or a fast and dirty solution is to add a PorterDuff filter to the button's background:

Button b = (Button)findViewById(R.id.myButton);
PorterDuffColorFilter redFilter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);