Akhil C
Akhil C

Reputation: 331

Remove button shadow in java (Android)

I need to remove shadow from a button. Since it is created in java (not in xml) I can't see the option to do so. I know the following line can be used in xml.

android:stateListAnimator="@null"

Tried the same in java as follows.

myButton.setStateListAnimator(null);

But the problem is, it will work only on devices with 21 API or above. Is there any alternative way?

Upvotes: 2

Views: 1334

Answers (2)

Brandon Thompson
Brandon Thompson

Reputation: 125

Use Button.setShadowLayer(float radius, float dx, float dy, int Color) and set radius to 0.

Upvotes: 0

piotrek1543
piotrek1543

Reputation: 19351

put

android:background="@null"

I mean

myButton.setBackground(null);

EDIT: Other possibilities (sorry for xml):

  1. Set elevation as 0:

    mybutton.setElevation(0);

  2. Set background as transparent:

    android:background="@android:color/transparent"

    Check: How to set transparent background for Image Button in code?

  3. Set style to button:

        <Button
         ...
         style="?android:attr/borderlessButtonStyle"
          ....
         /> 
    

    In Java code:

    Button button = new Button(getApplicationContext(), null, android.R.attr
            .borderlessButtonStyle);
    button = (Button) findViewById(R.id.buttonEndTime);
    

You may also visit: Remove/add shadow effect dynamically

Upvotes: 3

Related Questions