Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to use color from style in XML of another layout

I got this style in XML:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base"/>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->

        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/teal_500</item>

        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/teal_700</item>

        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">@color/light_blue_500</item>

        <!-- needed to suppress the old ActionBar when using the new Toolbar -->
        <item name="android:windowActionBar">false</item>

        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight & colorSwitchThumbNormal. -->
    </style>

</resources>

Question:

And inside the XML of a Fragment I'm using TextView onto which I would like to assign "colorPrimary" from the theme. But how?

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/text_input"
              android:textColor="@android:color/black"   <!-- use theme color here-->
              android:fontFamily="sans-serif-bold"
              android:text="some text"
              android:textSize="24dp"/>
like image 849
Matthias Avatar asked Jul 12 '15 18:07

Matthias


People also ask

Is styles XML and themes XML same?

The difference is how you apply it: instead of applying a style with the style attribute on a view, you apply a theme with the android:theme attribute on either the <application> tag or an <activity> tag in the AndroidManifest. xml file.

Where do styles go in XML?

Defining Styles This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file.

How do I change the color of my style on Android?

Here's how to change UI colors in Android 12 or higher:Open the Settings. Go to Wallpaper & style. You have two main options. You can let Android pick the system colors based on the wallpaper.


1 Answers

You can use like this

<TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/text_input"
              android:textColor="?attr/colorPrimary"   
              android:fontFamily="sans-serif-bold"
              android:text="some text"
              android:textSize="24dp"/>
like image 113
N J Avatar answered Oct 21 '22 17:10

N J