29

So I'm setting a button's background doing this:

b.setBackgroundResource(R.drawable.custom_button1);

How do I programmatically set it back to the default (boring grey) Android button? Is there a R.android.boring_grey resource identifier I can reference without recreating those states myself? Couldn't seem to find it. Maybe my Googling skills are failing me.

Oh and by the way I tried this:

   b.setBackgroundResource(0);

And the button actually disappeared (blended with black background?).

Fraggle
  • 8,607
  • 7
  • 54
  • 86

6 Answers6

39

Have you tried this?

android.R.drawable.btn_default;
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • 1
    That's the one, thanks. Couldn't find it for the life of me. Saw a previous post about this, but they left off the "android.R" part and just had "R.drawable" – Fraggle Jun 24 '11 at 18:16
  • The answer below suits people who have initially customised their buttons differently. – A Person Mar 23 '13 at 09:11
  • 1
    Is there any way to get the system default button instead of what looks like the Gingerbread default button? – reubenjohn Apr 08 '15 at 09:12
  • This did not work as the btn_default may is not always the default for your app! I used @piotrpo's answer and it worked like a charm. – SilentNot Apr 01 '19 at 12:17
20

first get the default background of Button b; using

Drawable d = b.getBackground();

then set another background of your choice

b.setBackgroundResource(R.drawable.custom_button1);

if you need default background again use this

b.setBackgroundDrawable(d);
piotrpo
  • 12,398
  • 7
  • 42
  • 58
  • I would say this answer is better then the accepted one. The accepted answer gave me a android 2.3 style button, while this gave me exactly the original button (which it is way more likely to do then the accepted answer). – Timmiej93 Apr 19 '16 at 17:06
  • thank you so much for this answer! I searched for a proper solution for so long, and finally found it here. this is exactly what i wanted. – Diptangsu Goswami Jan 31 '17 at 16:59
  • Since btn_default is not the default for every app, I used this answer. Not the accepted one! – SilentNot Apr 01 '19 at 12:15
5

Use this

b.tr.setBackgroundDrawable(null);
Ebin Sebastian
  • 1,291
  • 1
  • 13
  • 15
2

I did it by

this.setBackgroundResource(android.R.drawable.btn_default);

Ade
  • 33
  • 4
1

try

button.setBackgroundResourses(R.drawable.yourimage);

it will set the default background of buttons. and you can read more default properties of android widgets from given link: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml

Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
AziFarooqi
  • 11
  • 1
0

Drawable for "modern" button in XML is @android:drawable/btn_default_material
It does not exist in android.R.drawable for some reason.

EDIT: it's private, so don't use it.

artdeell
  • 161
  • 1
  • 5