Android theme support (Themes) in custom components, using Appcompat-v7

Asked

Viewed 368 times

2

I started using the Appcompat-v7, for use the concept of Material Design in my project. Only now I realized that my custom components even inherited from native components, are not applying the themes as the native components, for example:

I have the following custom component inherited 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 component EditTextPro thus:

<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 thus:

<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 2 Edittext is the following (the first is the android.widget.EditText and the second the com.app.materialtest.EditTextPro):

With the focus on android.widget.EditText the colorAccent is displayed in the EditText:

inserir a descrição da imagem aqui

Already focusing on the com.app.materialtest.EditTextPro the colorAccent is not displayed in the EditText:

inserir a descrição da imagem aqui

And even without the focus the colors presented are different, being a shade of gray to the android.widget.EditText and Black to the com.app.materialtest.EditTextPro

Questions:

  • Why does this happen? Since I do not make any changes in the styles of com.app.materialtest.EditTextPro in relation to the android.widget.EditText, and by logic (my logic =D) was for the com.app.materialtest.EditTextPro inherit all styles of android.widget.EditText. Right?
  • Is there any way to resolve this? Make the com.app.materialtest.EditTextPro have the same styles as the android.widget.EditText.
  • I must inherit instead of android.widget.EditText of some EditText support API? (NOTE: I searched the support API and found none EditText).

Note: I am running on Android 4.4.4.

1 answer

2


Instead of inheriting from android.widget.EditText from 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 class Javadoc, he 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 passage from this page (Using Support Libraries API) has a warning (Caution) which is: When using the Support Library classes, be sure to import the appropriate package class. 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 interpreted the following, which is, if you are using a support library, always try to import or inherit from the appropriate package. (Nothing less than it is already written. Rsrs :D)

  • Amazing that was it! Thank you very much. Our had already tried so many things that we no longer had options of "gambiarras" to try. Perfect. (Much better than these gringos, who only give options of gambiarras. here, recommend translate and post your reply there)

  • @You’re welcome, I’m happy to help. I’ll translate my answer and put in the link you sent me. Thanks.

  • 1

    I’m already following this line of thinking of always using the options from within android.support.v7.app, instead of the native android.app (learned this after spending some time trying to understand why my ActionBar did not function as expected, and it was because it was using the android.app.ActionBar), but in the case of EditText, They screwed up by changing the name TintEditText, I had already looked for some EditText in the API of support and had not found, hehe.

  • 1

    Actually looking for a class that added a name on the front makes things a little more difficult. If the name were the same, I believe the IDE would warn you by telling you that there is more than one place to matter and giving you the option to choose. I already translated my answer and put in the American Stack.

  • 1

    Already received my +1, there too, after all this option besides being simpler, it works, unlike the other answers there, which work in part. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.