Add drop shadow effects to EditText Field
Well.. @Shalini's answer helped me in this way but still I got another way to achieve 2D shadow with EditText Field and I am going to share with you.
We need to create custom XML view with three layer for EditText,
bottom shadow and right side shadow
Below is my code.
res/drawable/edittext_shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- most important is order of layers -->
<!-- Bottom right side 2dp Shadow -->
<item >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:right="3px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Now we can set this shadow view to our TextField using "Background" property
like this
res/layout/main.xml
<EditText android:layout_width="wrap_content"
android:id="@+id/txtpin"
android:maxLength="4"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:longClickable="false"
android:padding="2dp"
android:inputType="textPassword|number"
android:password="true"
android:background="@drawable/edittext_shadow"
android:layout_weight="0.98"
android:layout_marginLeft="15dp">
<requestFocus></requestFocus>
</EditText>
and the result screen is like I have posted in question above.
Thanks to SO, sharing knowledge.
Drop shadow around all the four sides of an EditText in Android Studio
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.
Android edittext with shadow like cardview
Try this into layer-list drawable file-
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="5dp" />
<solid android:color="#00CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="5dp" />
<solid android:color="#10CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="5dp" />
<solid android:color="#20CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="5dp" />
<solid android:color="#30CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="5dp" />
<solid android:color="#50CCCCCC" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#ffffff" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>
How to add a blurred shadow to an edit text?
The attributes: android:shadowColor
, android:shadowRadius
, android:shadowDx
, and android:shadowDy
create text shadow. If want to create a shadow behind your edittext box, you have to create a custom XML view in your res/drawable folder. Then set the attribute android:background="@drawable/customFileName" in your main.xml inside the edittext block. Here is a link to an answer to help you get started: Add drop shadow effects to EditText Field
Shadow for EditText Android
You can try using the "elevation" property. Here is the documentation: https://developer.android.com/training/material/shadows-clipping .
To change the color you will need to use Carbon (https://github.com/ZieIony/Carbon). You could also see this answer: Android change Material elevation shadow color .
Shadow Effect for a Text in Android?
Perhaps you'd consider using android:shadowColor, android:shadowDx
, android:shadowDy
, android:shadowRadius
; alternatively setShadowLayer() ?
How to add drop shadow to TextFormField In Flutter
You can Wrap your TextFormField
with a Material
widget and edit its properties such as elevation
and shadowColor
.
Example:
Material(
elevation: 20.0,
shadowColor: Colors.blue,
child: TextFormField(
obscureText: true,
autofocus: false,
decoration: InputDecoration(
icon: new Icon(Icons.lock, color: Color(0xff224597)),
hintText: 'Password',
fillColor: Colors.white,
filled: true,
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(borderRadius:BorderRadius.circular(5.0),
borderSide: BorderSide(color: Colors.white, width: 3.0))
),
),
)
You will Get something similar to the image below.
Related Topics
Remove Animation/Shifting Mode from Bottomnavigationview Android
How to Enable Cookies in Android Webview
Floatingactionbutton with Text Instead of Image
How to Disable Viewpager Adapter on Touching Specific Views
Gradle Android Build for Different Processor Architectures
Android Gcm Basic Implementation
Activitymanager.Getrunningtasks Is Deprecated Android
Asynctaskloader Basic Example. (Android)
Videoview Onresume Loses Buffered Portion of The Video
Onlocationchanged Callback Is Never Called
Android - Setting Layoutparams Programmatically
How to Make Part of The Text Bold in Android at Runtime
Actionlayout on Menuitem Does Nothing
Android Alarmmanager Setexact() Is Not Exact
Android Httpsurlconnection Eofexception
Android Sha1 Release Keystore Not Working with Google Maps
Android Intercept Recent Apps Button
How to Fix Android Studio Getting Stuck Executing Gradle Tasks