I recently encountered this problem when doing an android project.To keep the app style consistent,Need to change the original edittext bottom line color to orange.Searched for some solutions online,Here is a summary of the record.
The default edittext underline color is blue,
We want to achieve an orange effect
Implementation
1. Prepare two background images
One as the default background for edittext and the other as the background when typing
note
Use 9.png, do not use png, otherwise the picture will be blurred, Spent
Create an xml file with selector in folder drawable
<!-Drawable/edittext_shape.xml->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@ drawable/edittext_default" android:state_focused="false" />
<item android:drawable="@ drawable/edittext_focused" android:state_focused="true" />
</selector>
Create a new style in styles.xml under the values folder
This step is to reuse this style, You can also write code in the layout xml in the layout without style.
<!-Drawable/values /styles.xml->
<style name="smsedittext">
<item name="android:layout_marginleft">20dp</item>
<item name="android:layout_marginright">20dp</item>
<item name="android:layout_margintop">20dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textsize">16sp</item>
<item name="android:background">@ drawable/edittext_shape</item>
</style>
Reference custom edittext in layout file
<!-Drawable/layout/fragment_bomb.xml->
<linearlayout
android:id="@ + id/input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<edittext
android:id="@ + id/phone"
android:hint="@ string/phone_hint"
android:inputtype="phone"
android:maxlength="11"
android:maxlines="1" />
<edittext
android:id="@ + id/times"
android:hint="@ string/times_hint"
android:inputtype="number"
android:maxlines="1" />
</linearlayout>
Drawing lines using xml
View this article:Android using xml to achieve the dividing line
edittext remove border
android:background="@ null"
This code can remove the border of edittext
edittext with bottom line
Create a new line.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@ color/orange_normal" />
<size
android:height="1dp"
android:width="1000dp" />
</shape>
Referenced in the layout's layout file
<edittext
android:id="@ + id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@ null"
android:drawablebottom="@ drawable/line"
android:hint="@ string/phone_hint"
android:inputtype="phone"
android:maxlength="11"
android:maxlines="1" />
Related articles
- Android simultaneously control the number of EditText input characters and prohibit special character input
- EditText implementation of non-editable solution in Android
- Android programming set TextView color setTextColor usage examples
- Android change EditText underline color style
- Android set Edittext to get focus and pop up soft keyboard
- Comprehensive analysis of the method for monitoring EditText input in Android
- The pitfalls of the EditText setText method in Android