-3

Possible Duplicate:
How to remove ImageButton's standard background image?

I've set a custom background to a Button. Now I want to revert this change and display the default background of the Button again. How can I do this?

Community
  • 1
  • 1
Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
  • 1
    Copied your question title into google, the first hit is [this question](http://stackoverflow.com/questions/5457138/how-to-remove-imagebuttons-standard-background-image). -1 for no research effort. –  Jul 13 '12 at 13:05
  • @alextsc I want to remove background of Button(and want to set default background) not of image-button .and if I try to set null or transparent image in background of Button then Button becomes disappears.why?????? – Atul Bhardwaj Jul 13 '12 at 13:13
  • r u setting background to imageButton somewhere?? – AkashG Jul 13 '12 at 13:18
  • @AtulBhardwaj The code is the same for both the ImageButton and the Button widget *(which you would have know if you tried before asking)*. Also you should reword your question, I misunderstood it *(along other people apparently)*. I was under the impression that you wanted to remove any background, including the default one, not that you wanted to reset a custom background to default. –  Jul 13 '12 at 13:19
  • @AkashG first of all It's not a image button,it is a button.What I do is that I am fist changing background of button then trying to make buttons background as it was by default. – Atul Bhardwaj Jul 13 '12 at 13:23
  • that is what i was asking u.pls post what u hv done?? – AkashG Jul 13 '12 at 13:24
  • @AkashG first I did this btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.checked)) ; .Now i want to remove this background and want to get Button's default background.how??? – Atul Bhardwaj Jul 13 '12 at 13:28

4 Answers4

2

You can use android:background="@null" for your Button.
or button.setBackgroundResource(0);
or button.setBackgroundDrawable(null);

Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • button.setBackgroundResources(0); This is giving error as this method is undefined for button. and button.setBackgroundDrawable(null); makes my button disappear. – Atul Bhardwaj Jul 13 '12 at 13:19
2

By programatically, you can do something like,

button.setBackgroundDrawable(null);
button.setBackgroundResource(0);
user370305
  • 108,599
  • 23
  • 164
  • 151
1

Finally I got solution of my problem btn.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); This makes my button's background as it has by default.

Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
1

One solution is simply to remember which Drawable you had as the background before you set it to a custom one, e.g. save it in onCreate().

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    button = (Button) findViewById(R.id.yourbutton);
    defaultButtonBackgroundDrawable = button.getBackground();

    // set a custom background here, or somewhere else. 
    // Just make sure that the default one is saved before you modify it.
}

where defaultButtonBackgroundDrawable is a member variable of your activity and button keeps a reference to your button. This way you can restore the default background pretty easily by doing

button.setBackgroundDrawable(defaultButtonBackgroundDrawable); 
  • 3
    thanks for this solution.It works fine for me But I found one other solution that also works btn.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); – Atul Bhardwaj Jul 13 '12 at 13:41