Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove material shadow from ToggleButton?

Today I've faced a problem:

In my custom layout there are LinearLayout with grey background and ToggleButton right after TextView inside this LinearLayout. Toggle button have transparent background. But in android Lollipop here are unnecessary shadow. I've tried elevation=0dp attribute. But this did't fixed my problem.

This happens when I'm set api = 21

enter image description here

And happens this on older apis < 21

enter image description here

Here is the sample layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:orientation="horizontal"
    android:background="@color/grey_light">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0123" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:button="@android:color/transparent" />
</LinearLayout>

How to remove this shadow?

like image 824
Denis Nek Avatar asked Feb 12 '15 10:02

Denis Nek


People also ask

How do I get rid of shadows on Android?

If you want an Android app to remove shadow from photo, you can use Remove Unwanted Content. Just like other apps, this app offers two selection tools including the lasso and brush tools which allows you to select the shadow areas from your image.

How do I get rid of shadow below toolbar on Android?

By default, android provides shadow for action bar. This example demonstrates How to remove shadow below the action bar. Step 1 - Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 - Add the following code to res/layout/activity_main.

What is ToggleButton how to create it explain with example?

A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a Switch object.

What does toggle mean on Android?

In android, Toggle Button is a user interface control that is used to display ON (Checked) or OFF (Unchecked) states as a button with a light indicator. The ToggleButton is useful for the users to change the settings between two states either ON or OFF.


2 Answers

ToggleButton manages its elevation and translationZ with included state list animator. You can disable it by setting:

android:stateListAnimator="@null"

or by providing your own selector to handle these values:

android:stateListAnimator="@drawable/my_selector"
like image 122
Michał Powłoka Avatar answered Sep 21 '22 12:09

Michał Powłoka


Try to use style="@android:style/Widget.Holo.Button.Borderless

Change the widget to appcompat (or whatever you are using).

I am using this:

style="?android:attr/borderlessButtonStyle"
like image 35
JavierSegoviaCordoba Avatar answered Sep 21 '22 12:09

JavierSegoviaCordoba