In my project (Target API 21 with AppCompat support), I need to extend the EditText
class. My problem is MyEditText
class does not inherit EditText
style customized 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:
EditText
focusedEditText
not focused (enabled)MyEditText
not focused (enabled)My question is: How can I inherit default EditText
style in MyEditText
?
In flutter, the inherited widget is a base class that allows those classes to extend the information under the tree from it. Inherited widgets are also a kind of state management technique. It works by telling registered build references when a change occurs.
Example Using Inherited Widgetconst InheritedWidget({ Key? key, required Widget child }) : super(key: key, child: child); As you can see the child widget is required since the InheritedWidget has to be at the root of the widget tree so it can send the data through the widget tree.
Types of Widgets: There are broadly two types of widgets in the flutter: Stateless Widget. Stateful Widget.
Has a way very, very, very easy.
Instead of inheriting from android.widget.EditText your Custom EditText, inherit this class and see if it works:
android.support.v7.internal.widget.TintEditText
Link to the class: TintEditText.java
Read the Javadoc of the class, he 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.
*/
In the stretch of this page (Using Support Library APIs) has a caution which is: When using classes from the Support Library, be certain you import the class from the appropriate package. For example, when applying the ActionBar class:
I interpreted this, that is, if you are using a support library, always try to import or inherit the appropriate package. (Nothing less than that is written lol.: D)
You can work around it using styles
In your Theme
<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 the drawable according to this hack https://stackoverflow.com/a/20891131
<?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 have also tried using selector as the "background" field, but it didn't work with either "android:state_selected" or "android:state_activated"
Here's a little example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With