简体   繁体   中英

android:textColor not working for TextView

android:textColor doesn't seem to work for TextView inside a nested LinearLayout . I had a look at the question at Textcolor selector not working . But it is also not working in my case. Following is my layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CreateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblTitle"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:hint="Title:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblTitleEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your title here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblVenue"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:hint="Venue:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblVenueEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your venue here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>
</LinearLayout>

Am I making any mistake here? Any help would be greatly appreciated.

You used hint as text here. So you have to apply color to your hint with the following code. or you can also change hint to text in your xml .

android:textColorHint="#000"

Add android: text, you give hint property and try to text color change

android:text="Title:"

Please check this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CreateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblTitle"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:text="Title:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblTitleEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your title here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblVenue"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:text="Venue:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblVenueEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your venue here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>
</LinearLayout>

For android:text="Title:" you should use android:textColor="#000000"

And

For android:hint="Title:" you should use android:textColorHint="#000000"

Happy Coding.

You are trying to give text color to android: hint and replace it with android: text .

See the answer below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CreateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblTitle"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:text="Title:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblTitleEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your title here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblVenue"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:text="Venue:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblVenueEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your venue here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>
</LinearLayout>

Here what you set in text view is hint text. So , you need to set textColorHint property of text-view.

What you was doing is setting color on text . Which will not work because you haven't entered any text in text-view .

     <TextView
        android:id="@+id/lblTitle"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:layout_weight="0.9"
        android:hint="Title:"
        android:textAlignment="textEnd"
        android:textSize="24sp"
        android:textColorHint="#000000"/>

As per your code, you wrote hint in a Textview so for the apply color in hint use like,

android:textColorHint="#000000"

Or if you want to use text color then you have to set text into your Textview as below:

android:text="Title:"
android:textColor="#000000"

If you are using a hint in a TextView then use as below:

android:hint="Title:"
android:textColorHint="#000000"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM