Unable to set colorControlNormal and android: textColor in style

I am trying to create a material view EditText:

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#8AFFFFFF</item>

    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/white</item>
    <item name="colorControlHighlight">@color/white</item>
</style>

Then I apply the style to my theme:

<style name="AppTheme">
    <item name="editTextStyle">@style/AppTheme.EditText</item>
</style>

And apply the theme to the activity:

  <activity
        android:name=".MyActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="stateAlwaysHidden"
        android:theme="@style/AppTheme">

However, this does not change the color of the underline.

I know that I can change the accentColor to change the underline color, but I don't want to do this, because for some other controls my accent color will be different.

Can I adjust the underline color of the control as follows?

+4
source share
5 answers

, : colorControlNormal attr , android:textColor - style. colorControlNormal, , android:textColor, . colorControlNormal @style/Widget.AppCompat.EditText . :

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="colorControlNormal">@color/white</item>
</style>

: colorControlNormal, . , , :

1/ ThemeOverlay , :

<style name="AppTheme.EditText"> 
    <item name="colorControlNormal">@color/white</item>
</style>

2/ EditText:

<EditText
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.EditText"
/>

, , View theme ( ). .

colorControlNormal , .

+11

. s colorControlActivated, colorControlHighlight colorControlNormal edittext. , , android:textCursorDrawable . edittext style.xml(v21). , , . :

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--Edittext theme -->
        <item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

<style name="App_EditTextStyle" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/black</item>

    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/white</item>
    <item name="colorControlHighlight">@color/white</item>

    <item name="android:backgroundTint" >@color/white</item>
    <item name="backgroundTint">@color/white</item>

    <item name="android:textCursorDrawable">@color/white</item>
</style>

. API < 21

Drawable drawable = yourEditText.getBackground(); // get current EditText drawable 
    drawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP); // change the drawable color

    if(Build.VERSION.SDK_INT > 16) {
        yourEditText.setBackground(drawable); // set the new drawable to EditText
    }else{
        yourEditText.setBackgroundDrawable(drawable); // use setBackgroundDrawable because setBackground required API 16
    }
+1

, :

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#8AFFFFFF</item>

    <item name="android:backgroundTint">@color/white</item>
    <item name="backgroundTint">@color/white</item>
</style>

item android styles-v21.xml, Android Studio, 100% . API 19, , .

0

, .

1:

android:textCursorDrawable @null android:textColor .

<style name="editTextTheme" parent="@style/Widget.AppCompat.EditText">

        <item name="android:textColorHint">@color/white</item>

        <item name="colorControlNormal">@color/white</item>
        <item name="colorControlActivated">@color/white</item>
        <item name="colorControlHighlight">@color/white</item>

        <item name="colorAccent">@color/white</item>
        <item name="android:textCursorDrawable">@null</item>
        <item name="android:textColorPrimary">@color/white</item>

    </style>

2:

<style name="MyTheme">
        <item name="theme">@style/editTextTheme</item>
</style>

3:

<android.support.v7.widget.AppCompatEditText
        android:id="@+id/editText"
        style="@style/MyTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Name"
        android:inputType="textPersonName" />
0

style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="editTextStyle">@style/MyEditTextStyle</item>
</style>

<style name="MyEditTextStyle" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">#FF0</item>
</style>

<style name="AppTheme.MyEditTextTheme">
    <item name="android:textColorHint">#8AFFFFFF</item>

    <item name="colorControlNormal">#FF0</item>
    <item name="colorControlActivated">#FF0</item>
    <item name="colorControlHighlight">#FF0</item>
</style>

MyEditTextTheme EditText:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.MyEditTextTheme"/>

Now you have a yellow underline in this EditText only. You can also change colorAccent to MyEditTextTheme, this will not change the AppTheme colorAcent value.

0
source

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


All Articles