Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Change Text color depending on Theme

Posted on November 11, 2022 By

Solution 1 :

this may be not exactly what you want but it might help you.

it doesn’t matter what layout you use i’m just going to provide dummy code so you can understand.

Blockquote

Activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns_android="http://schemas.android.com/apk/res/android"
    xmlns_app="http://schemas.android.com/apk/res-auto"
    android_layout_width="match_parent"
    android_layout_height="match_parent">

            
            
            <TextView
                    android_layout_width="wrap_content"
                    android_layout_height="wrap_content" 
                    android_layout_gravity="center"
                    android_textSize="18sp"
                    android_text="Dark theme text"
                    />
            
            <TextView
                    android_layout_width="wrap_content"
                    android_layout_height="wrap_content" 
                    android_layout_margin="4dp"
                    android_layout_gravity="center"
                    android_textColor="@color/colorPrimaryDark"
                    android_text="Dummy text"
                    />
            
               
    </androidx.constraintlayout.widget.ConstraintLayout>

note : In order to change the color depending upon the current theme light or dark, it has to be dynamic.

now implement Dark theme:

create a new resource file and call attributes.xml, inside this file we will be declaring some attributes that we need for setting colors for widgets and layout.

and think of these attributes as controllers for colors in our layout.

add this snippet to attributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <attr name="metaColor" format="color"/>  
        <attr name="color" format="color"/>
        <attr name="textColor" format="color"/>   
</resources>

After that, we need to create two theme, light and dark in styles.xml file

inside styles.xml, here we make use of our attributes, add this code snippet:

<style name="Light" parent="AppTheme">
        <item name="textColor">#000000</item>
        <item name="metaColor">#606060</item>
        <item name="color">#ffffff</item>
</style>

<style name="Dark" parent="AppTheme">
        <item name="textColor">#ffffff</item>
        <item name="metaColor">#aeaeae</item>
        <item name="color">#000000</item>
</style>

then go to activity_main.xml

update it with this code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns_android="http://schemas.android.com/apk/res/android"
    xmlns_app="http://schemas.android.com/apk/res-auto"
    android_layout_width="match_parent"
    android_layout_height="match_parent">

            <TextView
                    android_layout_width="wrap_content"
                    android_layout_height="wrap_content" 
                    android_layout_gravity="center"
                    android_textSize="18sp"
                    android_textColor="?attributes/textColor"
                    android_text="Dark theme text"
                    />
            
            <TextView
                    android_layout_width="wrap_content"
                    android_layout_height="wrap_content" 
                    android_layout_gravity="center"
                    android_textColor="?attributes/metaColor"
                    android_text="Dummy text"
                    />



   
</androidx.constraintlayout.widget.ConstraintLayout>

now go to MainActivity and add this inside of onCreate function like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setTheme(R.style.Dark);
       setContentView(R.layout.activity_main);
}

@Override
    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setTheme(R.style.Light);
           setContentView(R.layout.activity_main);
    }

and here is a really cool repository to use to create your custom themes and change them dynamically with ripple animation:
https://github.com/imandolatkia/Android-Animated-Theme-Manager

READ  [FIXED] android - Activity won't start and initilazing Array Adapter syntax doesn't work
Powered by Inline Related Posts

Problem :

How do i change the text color depending on the theme in Android Studio with setTextColor()?

So that when dark mode is enabled it changes the Text to white and when white mode is enabled it changes to black

Comments

Comment posted by Kartik

Just make 2

Android Tags:android, android-studio

Post navigation

Previous Post: [FIXED] android.media.ImageWriter produces green screen screen on every second posted Image frame
Next Post: [FIXED] android – Notifee native module not found

Related Posts

[FIXED] java – Retrofit response body null but status code is 200 Android
[FIXED] Android Studio Activity Java File does not recognize XML Android
[FIXED] android – Firebase RTDB: Call keepSynced while offline Android
[FIXED] android – when switch to another fragment before completing the post execute the app gets crashed ,and show this error Android
[FIXED] java – I need admob smart banner to be on bottom of app (using XML if possible) Android
[FIXED] android – Http requests don’t return anything in Flutter, and no errors are thrown Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme