Extend widget style inheritance

In my project (Target API 21 with AppCompat support) I need to extend the EditText class. My problem is the MyEditText class does not inherit the EditText style configured with:

 <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" > <item name="colorPrimary">@color/primary</item> <item name="colorControlNormal">@color/grey_light</item> <item name="colorControlActivated">@color/primary</item> <item name="colorControlHighlight">@color/primary</item> </style> 

with @color/primary green


Screenshot:

enter image description here

  • line 1: EditText focused
  • line 2: EditText not focused (included)
  • line 3: MyEditText out of focus (enabled)

My question is: how can I inherit the default EditText style in MyEditText ?

+5
source share
2 answers

Has a way very, very, very easy.

Instead of inheriting your Custom EditText from android.widget.EditText, inherit this class and see if it works:

 android.support.v7.internal.widget.TintEditText 

Class Link: TintEditText.java

Read the Javadoc class, it says:

 /** * An tint aware {@link android.widget.EditText}. * <p> * This will automatically be used when you use {@link android.widget.EditText} in your * layouts. You should only need to manually use this class when writing custom views. */ 

Throughout this page (using the support library APIs) there is caution which: When using classes from the support library, make sure you import the class from the appropriate package. For example, when applying the ActionBar class:

  • android.support.v7.app.ActionBar when using the support library.
  • android.app.ActionBar when developing only for API level 11 or higher.

I have interpreted this, that is, if you are using a support library, always try to import or inherit the appropriate package. (Nothing less than lol .: D)

+6
source

You can get around it with styles.

In your topic

 <style name="AppTheme.BaseNoActionBar" parent="Theme.AppCompat.Light"> <item name="android:editTextStyle">@style/Widget.EditText</item> </style> <!-- EditText style --> <style name="Widget.EditText" parent="Widget.AppCompat.EditText"> <item name="android:textCursorDrawable">@drawable/cursor_gray</item> <item name="android:background">@drawable/underline_gray</item> </style> 

then define the cursor

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="1dp" /> <solid android:color="@color/lightGrayLabel" /> </shape> 

and in accordance with this hack fooobar.com/questions/139369 / ...

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:top="-6dp" android:left="-6dp" android:right="-6dp"> <shape android:shape="rectangle"> <solid android:color="@android:color/transparent"/> <stroke android:color="@color/lightGrayLabel" android:width="3dp"/> </shape> </item> </layer-list> 

I also tried using the selector as the "background" field, but it did not work with either "android: state_selected" or with "android: state_activated"

Here is a small example

Before

After

+3
source

Source: https://habr.com/ru/post/1207251/


All Articles