Android themes support on custom components using AppCompat-v7

2

I started using AppCompat-v7 for use the concept of Material Design in my project . But now I realized that my custom components even inheriting from native components are not applying themes like native components, for example:

I have the following custom component inheriting from android.widget.EditText :

public class EditTextPro extends android.widget.EditText {

    public EditTextPro(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

And in my layout I use the EditTextPro component like this:

<com.app.materialtest.EditTextPro
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/view"
    android:layout_below="@+id/editText3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

And another with a native component android.widget.EditText like this:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/editText3"
    android:layout_below="@+id/editText2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

I'm using the following theme:

<style name="AppTheme"  parent="Theme.AppCompat">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/palette_primary</item> <!-- #3F51B5 = Azul -->
    <item name="colorPrimaryDark">@color/palette_primary_dark</item> <!-- #303F9F = Azul Forte -->
    <item name="colorAccent">@color/palette_accent</item>  <!-- #FF4081 = Rosa -->
</style>

And the result of the 2 EditText is as follows (the first is the android.widget.EditText and the second the com.app.materialtest.EditTextPro ):

Focusing on android.widget.EditText colorAccent is displayed on the EditText line:

Alreadyfocusingoncom.app.materialtest.EditTextProcolorAccentisnotdisplayedontheEditTextline:

And even without focus, the colors shown are different, with a shade of gray for android.widget.EditText and Black for com.app.materialtest.EditTextPro

Questions:

  • Why does this occur? Since I do not make any changes in the styles of com.app.materialtest.EditTextPro in relation to android.widget.EditText , being that for logic (my logic = D) it was for com.app.materialtest.EditTextPro to inherit all styles of android.widget.EditText . Right?
  • Is there any way to resolve this? Make com.app.materialtest.EditTextPro have the same styles as android.widget.EditText .
  • Should I inherit instead of android.widget.EditText of some EditText of the support API? (Note: I looked in the support API and found no EditText ).
  

Note: I'm running on Android 4.4.4.

    
asked by anonymous 10.04.2015 / 15:38

1 answer

2

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

android.support.v7.internal.widget.TintEditText

Link to class: TintEditText.java

Read the Javadoc class, it says:

/ **
  * Uma tonalidade ciente {link android.widget.EditText}.
  * <P>
  * Isto será usada automaticamente quando você usa {link android.widget.EditText} em seus
  * Layouts. Você só precisa usar manualmente esta classe ao escrever views personalizadas.
  * /

In the Page (Using Support Library APIs) section a Caution that is: When using the Support Library classes, make sure to import the appropriate package class. For example, when you apply 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 the following, which is, if you are using a support library, always try to import or inherit from the appropriate package. (Nothing more or less than what is already written.)

    
10.04.2015 / 21:18