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
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