5

How to remove button background resource i refered this, But my need is to remove button background and that should change background as per deviceDefault theme. Means just removing the resource added last time, not assigning new Resource.

Can anybody help to solve this prob ? Thank You

2
  • I don't understand what you're asking. After "removal", should the button look like the system default buttons? If so, the answer you linked to does that. If not, please clarify.
    – Geobits
    Feb 20, 2013 at 14:06
  • yes you are correct. In that link btn_default is added which is default android button, But in my case i have my own theame ie DeviceDefault theme in which button look and feel is different'
    – Charan Pai
    Feb 20, 2013 at 14:09

6 Answers 6

29

try assigning background:@null in xml file for programetically try layout.setBackgroundResource(0);

4
  • adding your_button.setBackgroudResource(0) will remove any predefined backgroud. Feb 20, 2013 at 14:20
  • then you have to set the default button look and feel ,you will get that from android.R.drawable folder Feb 20, 2013 at 14:27
  • but it must work as you are applying default theme which you want,If this doesn't work that you have to create brand new button which do not have explicit background defined. Feb 20, 2013 at 14:32
  • btn_default.xml is a drawable file defined for default look of android button background in framework.
    – Charan Pai
    Feb 20, 2013 at 14:33
21

You can do this by changing the background resource. In the XML file, for the button's attributes.

<Button
    ...
    android:background="@null" />

Should do it.

16

Instead of setting the background to @null you can add the style:

<Button
    ...
    style="@style/Widget.AppCompat.Button.Borderless" />
2
  • I like this solution because its effects will show up in the design view, whereas "@null" does not. Jun 2, 2020 at 21:35
  • If anyone is inheriting another style for the buttons like me, you'll notice that changing the background to "@null" didn't take effect. So I can go with this solution that changes the style of the button (which did fit well for me), or change the backgroundResource programatically. Oct 24, 2020 at 16:59
0

If you're only doing this a few times, a simple way is to just save the previous background before altering it. You can store it in the tag field of the Button:

//store previous background drawable
myButton.setTag(myButton.getBackground());

//    ... alter background, do whatever


//restore background drawable from tag
myButton.setBackground((Drawable)myButton.getTag());
1
  • This logic can be applied i think, ll try and get you back . thank you
    – Charan Pai
    Feb 20, 2013 at 14:25
0
your_view.setBackground(null);

removes the pre-defined background successfully.

-2
    Button b;
    b=(Button)findViewById(R.id.button1);
    b.setBackgroundResource(R.drawable.ic_launcher);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

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

        }
    });

this should help you

1
  • 2
    This is the same ans in above mentioned link.
    – Charan Pai
    Feb 20, 2013 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.