Programing

TextInputLayout을 사용할 때 EditText 힌트 색상 변경

crosscheck 2020. 6. 29. 08:04
반응형

TextInputLayout을 사용할 때 EditText 힌트 색상 변경


TextInputLayout디자인 라이브러리에서 새로운 것을 사용하고 있습니다. 플로팅 레이블의 색상을 표시하고 변경할 수 있습니다. 불행히도 실제 EditText힌트는 항상 흰색입니다.

XML, 스타일 및 프로그래밍 방식으로 hintColor를 변경하려고 시도했지만 또한 사용하여 시도 android.support.v7.widget.AppCompatEditText했지만 EditText힌트는 항상 흰색으로 표시됩니다.

여기 내 XML 내입니다 TextInputLayoutEditText

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android.support.design:hintTextAppearance="@style/GreenTextInputLayout">


    <EditText

    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/city"
        android:textColorHint="@color/black"
        android:hint="@string/city" />

</android.support.design.widget.TextInputLayout>

그리고 여기에 내가 사용하는 스타일이 있습니다 TextInputLayout(나는 hintTextColor속성을 검은 색으로 만들려고했지만 나에게 아무것도하지 않았습니다).

<style name="GreenTextInputLayout" parent="@style/TextAppearance.AppCompat">
    <item name="android:textColor">@color/homestory_green</item>
</style>

android:textColorHint애플리케이션 테마에서 정의하십시오 .

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>

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

출처


style.xml에서 사용자 정의 스타일 작성

<style name="EditTextHint" parent="TextAppearance.AppCompat">
    <item name="colorAccent">@android:color/white</item>
    <item name="android:textColorHint">@color/BackgroundtWhiteColor</item>
    <item name="colorControlNormal">@color/BackgroundtWhiteColor</item>
    <item name="colorControlActivated">@color/your color</item>
    <item name="colorControlHighlight">@color/BackgroundtWhiteColor</item>
</style>

그런 다음 레이아웃 xml 파일에서 다음과 같이 테마 속성을 추가하십시오.

 <android.support.design.widget.TextInputLayout
            android:theme="@style/EditTextHint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

      <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</android.support.design.widget.TextInputLayout>

이것이 효과가 있기를 바랍니다


텍스트 편집에 textColorHint 속성 추가

android:textColorHint="#F6F6F6"

또는 원하는 색상


같은 문제가 발생했습니다. 이 줄을 부모 테마 에 넣음으로써 해결되었습니다 .

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/ColorPrimaryDark</item>
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
    <item name="colorAccent">@color/AccentColor</item>
    <item name="android:windowTranslucentStatus">true</item>

    <item name="android:textColorHint">@color/BackgroundtWhiteColor</item>
    <item name="colorControlNormal">@color/BackgroundtWhiteColor</item>
    <item name="colorControlActivated">#ff0000</item>
    <item name="colorControlHighlight">@color/BackgroundtWhiteColor</item>
    <!-- Customize your theme here. -->
</style>

이걸 넣으면

<item name="android:textColorHint">@color/BackgroundtWhiteColor</item>
<item name="colorControlNormal">@color/BackgroundtWhiteColor</item>
<item name="colorControlActivated">#ff0000</item>
<item name="colorControlHighlight">@color/BackgroundtWhiteColor</item>

별도의 스타일로 TextInputLayout 또는 Edittext에 적용하면 표시되지 않습니다. 당신은 가질 응용 프로그램의 상위 테마에 배치 할 수 있습니다.


문제의 원인이 무엇인지 잘 모르겠습니다.이 코드가 완벽하게 작동합니다. 올바른 xml 네임 스페이스 ( xmlns) 를 사용하는 것과 관련이 있다고 생각합니다 . XML 네임 스페이스 속성없이 android.support.design 사용이 지원되는지 확실하지 않습니다. 또는 textColorHintEditText 자체에서 사용 하는 속성 과 관련이 있습니다.

나열한 것:

<android.support.design.widget.TextInputLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    app:hintTextAppearance="@style/GreenTextInputLayout">

    <android.support.v7.widget.AppCompatEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="test"
        android:inputType="text" />
</android.support.design.widget.TextInputLayout>

스타일:

<style name="GreenTextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">#00FF00</item>
</style>

아래 코드는 저에게 효과적이지만 모든 컨트롤 색상에 영향을 미칩니다.

 <!-- In your style.xml file -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
            <!--EditText hint color-->
            <item name="android:textColorHint">@color/default_app_white</item>
           <!-- TextInputLayout text color-->
            <item name="colorControlActivated">@color/default_app_green</item>
            <!-- EditText line color when EditText on-focus-->
            <item name="colorControlHighlight">@color/default_app_green</item>
            <!-- EditText line color when EditText  in un-focus-->
            <item name="colorControlNormal">@color/default_app_white</item>
        </style>

EditText를 보유하는 TextInputLayout에서 다음을 추가하십시오.

android:textColorHint="someColor"

android : textColorHint = "# FFFFFF"는 가끔 작동하지만 가끔 작동하지 않습니다. 나를 위해 아래 솔루션이 완벽하게 작동합니다.

아래 코드는 XML 파일에서 작동하며 TextLabel 테마는 style.xml에 정의되어 있습니다.

 <android.support.design.widget.TextInputLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:theme="@style/MyTextLabel">

     <EditText
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:hint="Floating Label"
         android:id="@+id/edtText"/>

 </android.support.design.widget.TextInputLayout>

스타일 폴더 텍스트 레이블 코드

 <style name="MyTextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/Color Name</item> 
<!--The size of the text appear on label in false state -->
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/Color Name</item>
    <item name="colorControlNormal">@color/Color Name</item>
    <item name="colorControlActivated">@color/Color Name</item>
 </style>

false 상태에서 레이블의 색상을 변경하려는 경우 colorAccent, colorControlNormal 및 colorControlActivated는 필요하지 않습니다.


많은 답변이 있지만 이것에 대한 줄만 있으면됩니다.

android : textColorHint = "# 000000"

    <EditText
        android:id="@+id/edtEmail"
        android:textColorHint="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

당신은 android:textColorHint="@color/color_black"내부에서 사용할 수 있습니다 TextInputlayout그것은 나를 위해 일했다.

<android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_10"
                android:textColorHint="@color/color_black"
                app:hintTextAppearance="@style/TextLabel">

                <EditText
                    android:id="@+id/et_state"
                    style="@style/profile_editTextStyle"

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/billingState"
                    android:text="@string/billingState"
                     />
            </android.support.design.widget.TextInputLayout>

이에 따르면 https://code.google.com/p/android/issues/detail?id=176559

이 시도:

<android.support.design.widget.TextInputLayout
    android:textColorHint="#A7B7C2"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <EditText
        android:id="@+id/et_confirm_password"
        android:textColor="@android:color/black"
        android:hint="Confirm password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 </android.support.design.widget.TextInputLayout>

23.1.1에서 작동합니다


상위 뷰에 EditText를 흰색으로 만드는 타사 라이브러리의 스타일이있는 것 같습니다.

android:theme="@style/com_mixpanel_android_SurveyActivityTheme"

일단 이것을 제거하면 모든 것이 잘 작동했습니다.


이 도움이 될 수도 있습니다 시도

      edittext.addTextChangedListener(new  TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "onTextChanged", 2000).show();;
            //newuser.setTextColor(Color.RED);
            edittext.setHintTextColor(Color.RED);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "afterTextChanged", 2000).show();;

        }
    });

<item name="colorAccent">@android:color/black</item>

테마 내에서 원하는 색상으로 색상을 변경하면 EditText 줄, 커서 및 힌트가 변경됩니다.

또는 style.xml에이 스타일을 포함시킬 수도 있습니다.

<style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@android:color/black</item>
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@android:color/black</item>
    <item name="colorControlNormal">@android:color/black</item>
    <item name="colorControlActivated">@android:color/black</item>
</style>

그런 다음 TextInputLayout 안에 다음과 같이 넣을 수 있습니다

<android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/TextLabel">
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/password"
                android:drawableStart="@drawable/password"
                android:maxLines="1"
                android:hint="password"
                android:inputType="textWebPassword" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Name"
                android:textColor="@color/black"
                android:textColorHint="@color/grey"/>
        </android.support.design.widget.TextInputLayout>

textColorHint를 사용하여 EditText의 힌트 색상으로 원하는 색상을 설정하십시오. 문제는 EditText의 힌트가 무언가를 입력 할 때가 아니라 EditText가 멋진 애니메이션과 함께 포커스를 얻으면 즉시 사라진다는 것입니다. 포커스를 멀리두고 EditText로 전환하면이 사실을 분명히 알 수 있습니다.


늦었지만이 같은 사람을 도울 수 있습니다

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:textColorHint="#8cffffff">

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/email_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/security_email"
            android:inputType="textEmailAddress"
            android:textColor="@color/white"
            app:backgroundTint="#8cffffff" />
    </android.support.design.widget.TextInputLayout>

TextInputLayout에 힌트 색상을 설정해야합니다.


 <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_passdword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="#d3d3d3">
        <EditText
            android:id="@+id/etaddrfess_ciwty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:bottomLeftRadius="10dp"
            android:bottomRightRadius="50dp"
            android:fontFamily="@font/frutiger"
            android:gravity="start"
            android:hint="@string/address_city"
            android:padding="10dp"
            android:textColor="#000000"
            android:textColorHint="#000000"
            android:textSize="14sp"
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp" />
    </android.support.design.widget.TextInputLayout>``

With the Material Components library you can customize the TextInputLayout the hint text color using:

  • In the layout:

    • app:hintTextColor attribute : the color of the label when it is collapsed and the text field is active
    • android:textColorHint attribute: the color of the label in all other text field states (such as resting and disabled)

Something like:

<com.google.android.material.textfield.TextInputLayout
     app:hintTextColor="@color/mycolor"
     android:textColorHint="@color/text_input_hint_selector"
     .../>

enter image description hereenter image description here


Try code below:

The high light color is White:

 <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/TextStyle"
        >

        <EditText
            android:id="@+id/input_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:drawablePadding="14dp"
            android:drawableLeft="8dp"
            android:textColor="@color/white"
            android:hint="Mobile" />
    </android.support.design.widget.TextInputLayout>

style: TextStyle

    <style name="TextStyle" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/white</item>
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/white</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/white</item>
     </style>

Here is my experience with this similar issue & the required solution.

Requirement:

  • Need to create customize TextInputEditText (can say some should be transparent and some should be not but use custom colors for various events)

Problem faced:

  • android:textColorHint is not working as required.

Solutions tried, which failed:

  • Used android:textColorHint in style and applied to TextInputEditText. No results or difference.
  • Set android:textColorHint in application's theme applied. It do helped but it was now set for entire application. (Not up-to mark to requirement)

After a lot of RnD, the best SOLUTION found was in an article which described that we need to apply theme to TextInputLayout or to it's parent view rather then only applying style to TextInputEditText or to it's parent view.

Please check TextInputLayout proper Theming which told that how we are at wrong track.

참고URL : https://stackoverflow.com/questions/30824009/change-edittext-hint-color-when-using-textinputlayout

반응형