androidandroid-layoutandroid-text-color

How do you set a default textColor inside a LinearLayout that contains multiple TextViews?


I have a LinearLayout with that has multiple TextViews and want to set up a default global color for that layout only without having to add a textColor field inside each TextView. Also, if it's possible, would it also be possible to override the color value by adding it inside the TextView? i.e. If I set blue as a default color and black for a single TextView, would the blue change to black?


Solution

  • To set the default global TextView colors, first you can create your own theme in AndroidManifest.xml file for the following items:

    For example, in AndroidManifest.xml:

    <style name="TextViewTheme" parent="android:Widget.TextView">
        <!-- Set the default global color for TextViews to Holo Blue Dark -->
        <item name="android:textColorPrimary">@android:color/holo_blue_dark</item>
        <item name="android:textColorSecondary">@android:color/holo_blue_dark</item>
        <item name="android:textColorTertiary">@android:color/holo_blue_dark</item>
        <item name="android:textColorHint">@android:color/holo_blue_dark</item>
    </style>
    

    Next, set the theme style on your LinearLayout. You can also override the default for a single TextView to black color, like the following which set the first TextView Hint text color to black in activity_main.xml:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:theme="@style/TextViewTheme">
    
        <TextView
            android:id="@+id/phone_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/phone_tv"
            android:textColor="@android:color/black"
            android:textColorHint="@android:color/black" />
    
         <TextView
            android:id="@+id/email_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/email_tv" />
    
    </LinearLayout>
    

    Hope this helps!