AutoCompleteTextView styling with AppCompat.EditText not working

I am making a material style application

I want to change the style of AutoCompleteTextView
for style, as in android.support.v7.internal.widget.TintEditText

I added a style to my .xml style:

 <style name="AppTheme" parent="AppTheme.Base"/> <style name="AppTheme.Base" parent="Theme.AppCompat"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewAppTheme</item> </style> <style name="AutoCompleteTextViewAppTheme" parent="Base.Widget.AppCompat.EditText"/> 

This works, but the colors of the lines do not change. enter image description here

EditTexts with material design seems to use colorControlActivated and colorControlNormal . Therefore, I tried to override these properties in the previous style definition, but this does not affect.
What do I need to do to make it work?

+6
source share
3 answers

Try adding the desired style for your widget in xml as follows:

 <View style="@style/NameOfYourTheme" ... /> 

If this does not work, you can try it yourself. You will have to change the TextView attributes that you can find here .

You can change the textcolor for the instance by changing the android:textColor attribute, which should be added to your style, for example:

 <style name="AutoCompleteTextViewAppTheme" parent="Base.Widget.AppCompat.EditText"/> <item name="android:textColor">#ffffffff</item> </style> 

If you want to change the edittext line, you need to change the background attribute, for example, as follows:

 <item name ="android:background="@drawable/line_background"> </item> 

And add a new line_background.xml file to the drawing folder with similar content:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:color="#c0c000" android:width="3dp"></stroke> </shape> 
+5
source

I created a custom Tint Aware AutoCompleteTextView to solve this problem.

 package com.atrinax.gist; import android.content.Context; import android.support.v7.internal.widget.TintTypedArray; import android.util.AttributeSet; import android.widget.AutoCompleteTextView; public class TintAutoComplete extends AutoCompleteTextView { private static final int[] TINT_ATTRS = { android.R.attr.background }; public TintAutoComplete(Context context) { this(context, null); } public TintAutoComplete(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); } public TintAutoComplete(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, TINT_ATTRS, defStyleAttr, 0); setBackgroundDrawable(a.getDrawable(0)); a.recycle(); } } 

Add this class to your project. Use in xml like this:

 <com.atrinax.gist.TintAutoComplete android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

The disadvantage of this solution is that it relies on an inner class

 android.support.v7.internal.widget.TintTypedArray 

which may change or become unavailable in the future. Please take a look at Derek's answer below - this is a solution to the background problem that arises from my answer.

+3
source

To extrapolate to @Atrinax's answer, here is a class that will handle the popupBackground attribute by extending AutoCompleteTextView and adding it to TINT_ATTRS

 public class TintAutoCompleteTextView extends AutoCompleteTextView { private static final int[] TINT_ATTRS = { android.R.attr.background, android.R.attr.popupBackground }; public TintAutoCompleteTextView(Context context) { this(context, null); } public TintAutoCompleteTextView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.autoCompleteTextViewStyle); } public TintAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, TINT_ATTRS, defStyleAttr, 0); setBackgroundDrawable(a.getDrawable(0)); setDropDownBackgroundDrawable(a.getDrawable(1)); a.recycle(); } } 
+2
source

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


All Articles