Remove shadow in Button - android

I created a button, set the background to #null, but shadow is still there.
How can I remove the shadow?
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:background="#null"
android:text="Random" />

Try that:
android:background="#android:color/transparent"
BTW: your background as null works for me without the shadow

You have to specify a background via a drawable.
<Button
android:id="#+id/shadowless_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_button"
android:text="Press Me"
/>
and a background in the drawables folder
drawable/background_button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!-- view background color -->
<solid android:color="#android:color/darker_gray"></solid>
<!-- view border color and width -->
<stroke android:width="1dp" android:color="#android:color/black"></stroke>
<!-- If you want to add some padding -->
<!-- Here is the corner radius -->
<corners android:radius="4dp"></corners>
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!-- view background color -->
<solid android:color="#android:color/white"></solid>
<!-- view border color and width -->
<stroke android:width="1dp" android:color="#android:color/darker_gray"></stroke>
<!-- If you want to add some padding -->
<!-- Here is the corner radius -->
<corners android:radius="4dp"></corners>
</shape>
</item>
</selector>

Related

How to implement the round border and ?selectableItemBackground in textview android?

I am trying to have two text views inside a Linear Layout with a horizontal orientation. Now I want to have the following things for each text view:
An icon at left - implemented using android:drawableLeft="#drawable/ic_lightbulb_outline_blue_24dp"
A round border around the text view - implemented using android:background="#drawable/round_border"
Also, need that ripple effect on touching a touch item
The 3rd point is implemented in other text views using the att.
android:background="?selectableItemBackground"
android:clickable="true"
But there is already a background att. for the round border. Now my question is how to achieve that ripple effect? Is using following att. only way:
android:foreground="?selectableItemBackground"
Android shows this att. is not supported in lower api versions so worried.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/vision_mission"
android:gravity="center"
android:layout_margin="8dp"
android:padding="8dp"
android:textSize="16sp"
android:onClick="openVisionMission"
android:drawableLeft="#drawable/ic_lightbulb_outline_blue_24dp"
android:background="#drawable/round_border"
android:clickable="true"
android:focusable="true"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/team"
android:textSize="16sp"
android:drawableLeft="#drawable/ic_people_blue_24dp"
android:background="#drawable/round_border"
android:clickable="true"
android:focusable="true"
android:padding="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:gravity="center"/>
</LinearLayout>
You can achieve ripple effect and text view rounded background at the same time using the following code.
Custom ripple drawable, it require API level 21+
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/grey">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#android:color/holo_blue_dark" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#android:color/holo_blue_dark" />
</shape>
</item>
</ripple>
After that set background of your text view
android:background="#drawable/custom_ripple"
For pre lollypop ie. below API level 21
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#color/colorDarkGray"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<solid android:color="#color/colorButtonGreen"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorButtonGreenFocused"/>
</shape>
</item>
</selector>
It is not exactly what you are looking for.
You can achieve easily something similar with a MaterialButton with a OutlinedButton style.
Something like:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="#drawable/ic_add_24px"
android:text="...."
.../>
You can customize the padding between the icon and the text, the textColor, the strokeWidth, the strokeColor and also the color selector used for the app:rippleColor
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="#drawable/ic_add_24px"
app:strokeColor="#color/primaryDarkColor"
app:strokeWidth="2dp"
app:iconPadding="16dp"
.../>
you can change your background drawable with selector tag
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>

ToggleButton rectangle containing an icon

I am trying to have a ToggleButton rectangle that contains an icon, once clicked the background should change color but the icon should remain visible.
So far I'm able to have a rectangle change color on click like such:
<ToggleButton
android:background="#drawable/toggle_selector"
android:id="#+id/toggle"
android:checked="false"
android:textOn=""
android:textOff=""
android:layout_width="60dp"
android:layout_height="60dp" />
toggle_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/toggle_state_on"
android:state_checked="true" />
<item
android:drawable="#drawable/toggle_state_off"
android:state_checked="false" />
</selector>
toggle_state_on.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!-- Draw a 5dp width border around shape -->
<stroke
android:color="#4c975d"
android:width="5dp"
/>
</shape>
</item>
<!-- Overlap the left, top and right border using background color -->
<item
android:bottom="5dp"
>
<shape android:shape="rectangle">
<solid android:color="#6dd988"/>
</shape>
</item>
</layer-list>
toggle_state_off being the same as on but with different colors.
I'm not able to find how to put an icon at the center of the button I have created, can that be achieved? And how?
You may need to modify the below as required however this will have a icon the center for both state on and off. Add the line toggle_state_off and toggle_state_on android:drawable="#drawable/ic_attach_money_black_24dp"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!-- Draw a 5dp width border around shape -->
<stroke
android:color="#2c125d"
android:width="5dp"
/>
</shape>
</item>
<!-- Overlap the left, top and right border using background color -->
<item
android:bottom="5dp"
android:drawable="#drawable/ic_attach_money_black_24dp">
<shape android:shape="rectangle">
<solid android:color="#2c125d"/>
</shape>
</item>
</layer-list>
Mark it as answered if this helps

Drop shadow around all the four sides of an EditText in Android Studio

As far as my knowledge aids me, there isn't any property to drop shadow around an EditText field, that can be set in the layout file. I searched for the problem and found solution that I've to provide a custom background xml drawable.
My drawable/background_with_shadow looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-lis xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="rectangle">
<solid android:color="#android:color/darker_gray" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
and I'm setting this property for my LinearLayout which contains an EditText
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_with_shadow">
<EditText>
.
.
</EditText>
<LinearLayout>
However it just drops the shadow around the bottom and bottom corners.
How would I do it for all the four sides and corners?
I'm trying to achieve this:
There are some better ways to get shadow around your edit text without using layer-list :
#1
Wrap your EditText in a CardView like this :
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="7dp"
android:layout_margin="10dp"
app:cardCornerRadius="0dp">
<EditText
android:id="#+id/msg_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:hint="Write a message"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:textColorHint="#c7c7c7"
android:textSize="15dp" />
</android.support.v7.widget.CardView>
OUTPUT :
2
Use a 9 patch as the background of your EditText :
<EditText
android:id="#+id/msg_box"
android:padding="20dp"
android:background="#drawable/shadow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:lines="5"
android:gravity="top"
android:hint="Enter your complaint..."
android:textColorHint="#a7a7a7"
android:textSize="15dp" />
OUTPUT
Read more about 9patch here.
Here's a great online tool to generate 9 patch shadow.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<item>
<shape>
<!-- set the shadow color here -->
<stroke
android:width="2.5dp"
android:color="#color/darker_gray" />
<!-- setting the thickness of shadow (positive value will give shadow on that side) -->
<!--Set shadow width as per your requirement -->
<padding
android:bottom="2dp"
android:left="0.5dp"
android:right="2dp"
android:top="0.5dp" />
<corners android:radius="2dp" />
</shape>
</item>
<!--White Background -->
<item>
<shape>
<solid android:color="#fff" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
Set this layout as your LinearLayout background.

Android EditText gradient shadow

I want a shadow which must have a GRADIENT shadow as shown in image, zoom image to see actual gradient effect
2nd image is about show effect in zoom
all I can see from the picture - you should use [layer-list drawable with 9-patch shadow] for every [state drawable] of the EditText.
this is my output edittext which is not expected..I expect more accurate
This is finally I got an answer..but Im not satisfied with this answer...so any one have any better solution???
<!-- most important is order of layers -->
<!-- Bottom right side 1dp Shadow -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/lighter_gray" />
</shape>
</item>
<!-- Bottom 2dp Shadow -->
<item
android:bottom="1px"
android:right="1px">
<shape android:shape="rectangle" >
<solid android:color="#color/light_gray" />
</shape>
</item>
<!-- Bottom 2dp Shadow -->
<item
android:bottom="3px"
android:right="3px">
<shape android:shape="rectangle" >
<solid android:color="#color/gray" />
</shape>
</item>
<!-- White Top color -->
<item
android:bottom="5px"
android:right="5px">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#color/selector_color" />
</shape>
</item>
</layer-list>
EditText
<EditText style="#style/match_width"
android:background="#drawable/border_with_shadow"
android:hint="Enter Order number"
android:padding="5dp"
android:paddingLeft="11dp"
android:singleLine="true"
android:textColor="#000000"
android:textSize="16sp" />

How to make RelativeLayout in state disabled?

I have a relative layout, which uses a statelist to display different drawables in the states pressed, focused and disabled. Now the states pressed, normal are working fine, but when i do a relativeLayoutObject.setEnabled(false), the correct drawable is not displayed.
I have also relativeLayoutObject.setClickable(false) and setting android:state_enabled="false" from XML but none of them is working.
This is my layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:clickable="true"
android:layout_height="match_parent">
<!--SOME ITMES INSIDE THE LAYOUT-->
<RelativeLayout>
and the selector is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_d" android:state_enabled="false"/>
<item android:drawable="#drawable/drawable_n" android:state_focused="true"/>
<item android:drawable="#drawable/drawable_p" android:state_pressed="true"/>
<item android:drawable="#drawable/drawable_n"/>
</selector>
I am applying the selector as : relativeLayoutObject.setBackgroundDrawable(R.drawabled.button_state_list);
Just came across your question, even it's been 3 yrs since you asked :)
RelativeLayouts do not have a disabled/enabled state, so what I did was to originally set a background drawable style to my RelativeLayout like this:
<RelativeLayout
android:id="#+id/activity_register_btn"
...
android:background="#drawable/style_button_yellow_disabled_rounded">
<!-- Register button -->
<TextView
... />
</RelativeLayout>
So my style's code (within drawbles folder):
style_button_yellow_disabled_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="#ffdca5" >
</solid>
<!-- view border color and width -->
<stroke
android:width="1dp"
android:color="#ffdca5" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="12dp"
android:top="12dp"
android:right="12dp"
android:bottom="12dp">
</padding>
<!-- Here is the corner radius -->
<corners
android:radius="30dp">
</corners>
</shape>
Then, in my java file, when needed, I changed the background to another xml which was the enabled view:
style_button_yellow_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="#fcc002" >
</solid>
<!-- view border color and width -->
<stroke
android:width="1dp"
android:color="#fcc002" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="12dp"
android:top="12dp"
android:right="12dp"
android:bottom="12dp">
</padding>
<!-- Here is the corner radius -->
<corners
android:radius="30dp" >
</corners>
</shape>

Categories

Resources