Runtime Change Android material theme - java

I have one setting screen for change app themes at runtime. I know how to create material design theme. I have create one in my style.xml file
Here is code of my style.xml:
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primaryBackground</item>
<item name="colorPrimaryDark">#color/primaryBackground</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorControlNormal">#color/primaryBackground</item>
<item name="android:colorControlActivated">#color/primaryBackground</item>
<item name="android:colorControlHighlight">#color/primaryBackground</item>
<item name="android:textColorPrimary">#color/primaryBackground</item>
<item name="android:textColorSecondary">#color/primaryBackground</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
<item name="android:textCursorDrawable">#drawable/cursor_indicator</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Now I want to change runtime app theme from green to purple or yellow. Anything can any one tell me how can I create a color picker from theme selection and how can I create multiple themes in my style.xml for change it to runtime.

Have you seen this demo?
MultipleThemeMaterialDesign
See this class:
public class Preferences {
private static final BoolToStringPref[] PREF_MIGRATION = new BoolToStringPref[]{
new BoolToStringPref(R.string.pref_dark_theme, false,
R.string.pref_theme, R.string.pref_theme_value_red),
};
public static void sync(PreferenceManager preferenceManager) {
Map<String, ?> map = preferenceManager.getSharedPreferences().getAll();
for (String key : map.keySet()) {
sync(preferenceManager, key);
}
}
public static void sync(PreferenceManager preferenceManager, String key) {
Preference pref = preferenceManager.findPreference(key);
if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry());
}
}
/**
* Migrate from boolean preferences to string preferences. Should be called only once
* when application is relaunched.
* If boolean preference has been set before, and value is not default, migrate to the new
* corresponding string value
* If boolean preference has been set before, but value is default, simply remove it
* #param context application context
* TODO remove once all users migrated
*/
public static void migrate(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
for (BoolToStringPref pref : PREF_MIGRATION) {
if (pref.isChanged(context, sp)) {
editor.putString(context.getString(pref.newKey), context.getString(pref.newValue));
}
if (pref.hasOldValue(context, sp)) {
editor.remove(context.getString(pref.oldKey));
}
}
editor.apply();
}
public static void applyTheme(ContextThemeWrapper contextThemeWrapper) {
if (Preferences.darkThemeEnabled(contextThemeWrapper)) {
contextThemeWrapper.setTheme(R.style.AppTheme_Blue);
}
}
private static boolean darkThemeEnabled(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getString(context.getString(R.string.pref_theme),
context.getString(R.string.pref_theme_value_red))
.equals(context.getString(R.string.pref_theme_value_blue));
}
private static class BoolToStringPref {
private final int oldKey;
private final boolean oldDefault;
private final int newKey;
private final int newValue;
private BoolToStringPref(#StringRes int oldKey, boolean oldDefault,
#StringRes int newKey, #StringRes int newValue) {
this.oldKey = oldKey;
this.oldDefault = oldDefault;
this.newKey = newKey;
this.newValue = newValue;
}
private boolean isChanged(Context context, SharedPreferences sp) {
return hasOldValue(context, sp) &&
sp.getBoolean(context.getString(oldKey), oldDefault) != oldDefault;
}
private boolean hasOldValue(Context context, SharedPreferences sp) {
return sp.contains(context.getString(oldKey));
}
}
}
Checkout that demo, It will helps you to understand more.

Dynamic themes using style.xml
Here is my code
Style.xml
<resources>
<style name="AppTheme.Base.Green" parent="AppTheme.Green">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Base.Green.Dark" parent="AppTheme.Green">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg_black</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Base.Purple" parent="AppTheme.Purple">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Base.Purple.Dark" parent="AppTheme.Purple">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg_black</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<!-- Base application themes. -->
<style name="ThemeApp.Green" parent="AppTheme.Base.Green"/>
<style name="ThemeApp.Green.Dark" parent="AppTheme.Base.Green.Dark"/>
<style name="ThemeApp.Purple" parent="AppTheme.Base.Purple"/>
<style name="ThemeApp.Purple.Dark" parent="AppTheme.Base.Purple.Dark"/>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
</resources>
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.Green" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary_green</item>
<item name="colorPrimaryDark">#color/primary_dark_green</item>
<item name="colorAccent">#color/accent_green</item>
<item name="colorControlHighlight">#color/primary_green</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
</style>
<style name="AppTheme.Purple" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary_purple</item>
<item name="colorPrimaryDark">#color/primary_dark_purple</item>
<item name="colorAccent">#color/accent_purple</item>
<item name="colorControlHighlight">#color/primary_purple</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
</style>
</resources>
v21/style.xml
<resources>
<style name="AppTheme.Base.Green" parent="AppTheme.Green">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:navigationBarColor">#color/primary_green</item>
</style>
<style name="AppTheme.Base.Green.Dark" parent="AppTheme.Green">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg_black</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:navigationBarColor">#color/primary_green</item>
</style>
<style name="AppTheme.Base.Purple" parent="AppTheme.Purple">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:navigationBarColor">#color/primary_purple</item>
</style>
<style name="AppTheme.Base.Purple.Dark" parent="AppTheme.Purple">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#color/activity_bg_black</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:navigationBarColor">#color/primary_purple</item>
</style>
<!-- Base application themes. -->
<style name="ThemeApp.Green" parent="AppTheme.Base.Green"/>
<style name="ThemeApp.Green.Dark" parent="AppTheme.Base.Green.Dark"/>
<style name="ThemeApp.Purple" parent="AppTheme.Base.Purple"/>
<style name="ThemeApp.Purple.Dark" parent="AppTheme.Base.Purple.Dark"/>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
<!-- <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />-->
</resources>
method for change Theme runtime
private void setAppTheme() {
if (!MainController.preferenceGetString(Theme_Current, "").equals("")) {
if (MainController.preferenceGetString(Theme_Current, "").equals("Green")) {
setTheme(R.style.ThemeApp_Green);
} else if (MainController.preferenceGetString(Theme_Current, "").equals("Green_Dark")) {
setTheme(R.style.ThemeApp_Green_Dark);
} else if (MainController.preferenceGetString(Theme_Current, "").equals("Purple_Dark")) {
setTheme(R.style.ThemeApp_Purple_Dark);
} else if (MainController.preferenceGetString(Theme_Current, "").equals("Purple")) {
setTheme(R.style.ThemeApp_Purple);
}
} else {
setTheme(R.style.ThemeApp_Green);
}
}
after execute this method restart your activity needed
full source code
https://github.com/rkoshti/DynamicMaterialTheme

Related

Rarely Occured Error: Binary XML file line #196: <item> tag requires a 'drawable' attribute or child tag defining a drawable

I have an issue its error: Binary XML file line #196: tag requires a 'drawable' attribute or child tag defining a drawable
This error occurs sometimes and sometimes not for the same actions where I use animation-list. It causes a crash. After the crash, I reopen the app and do the same thing, but I don't have that type of error.
An error occurred approximately one time every 10 actions are the same. I can't get what is wrong and why it occurs rarely and not all time.
Every attempt is after creating activity.
My java code:
package com.edulab.akiciokuma;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
public class AnimationActivity extends AppCompatActivity {
private MediaPlayer player;
private AnimationDrawable running;
private ImageView anime;
private String animwhich, strategy, condition;
private int level;
private FirebaseFirestore firebaseFirestore =
FirebaseFirestore.getInstance();
private String[] animalIntro = {"tosbaameeting", "zipzipintro",
"foxintro", "atintro", "deveintro", "cheetahintro"};
private String[] animalModel = {"tosbaamodel", "zipzipmodel",
"foxmodel", "atmodel", "devemodel", "cheetahmodel"};
private String[] animalModel2 = {"tosbaamodel2", "zipzipmodel2",
"foxmodel2", "atmodel2", "devemodel2", "cheetahmodel2"};
private String[] animalReread = {"tosbaareread", "zipzipreread",
"foxreread", "atreread", "devereread", "cheetahreread"};
private String[] animalReread2 = {"tosbaareread2", "zipzipreread2",
"foxreread2", "atreread2", "devereread2", "cheetahreread2"};
private String[] animalReread3 = {"tosbaareread3", "zipzipreread3",
"foxreread3", "atreread3", "devereread3", "cheetahreread3"};
private String[] animalshutup = {"tosbaailk6", "zipzipgozuacik2",
"foxacik5", "atacik6", "deveacik4", "cheetahacik5"};
private boolean intro = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
initializeVariables(); //initializing variables
if(animwhich.equals("intro")){
intro = true;
setvideo(animalIntro[(int) Math.floor(level/12.1)]);
}
else{
if(animwhich.equals("model")) {
if(strategy.equals("x"))
setvideo(animalModel2[(int) Math.floor(level/12.1)]);
else
setvideo(animalModel[(int) Math.floor(level/12.1)]);
}else {
if (animwhich.equals("reread")) {
switch (Integer.parseInt(condition)){
case 2:
setvideo(animalReread2[(int)
Math.floor(level/12.1)]);
break;
case 3:
setvideo(animalReread3[(int)
Math.floor(level/12.1)]);
break;
default:
setvideo(animalReread[(int)
Math.floor(level/12.1)]);
break;
}
}
}
}
}
private void initializeVariables() {
animwhich = configureText("animwhich", "model");
strategy = configureText("strategy", "y");
condition= configureText("condition", "1");
level = configureNumber("level");
anime = findViewById(R.id.imageView);
}
private void setvideo(final String file) {
anime.setImageDrawable(getResources().getDrawable(getResources().
getIdentifier("drawable/" + file, null,
getPackageName())));
running = (AnimationDrawable) anime.getDrawable();
running.start();
if (player == null) {
player = MediaPlayer.create(this,
getResources().getIdentifier(file, "raw", getPackageName()));
player.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
if (player != null) {
player.start();
}
}
private void stopPlayer() {
if (player != null) {
player.release();
player = null;
running.stop();
final Intent getData = getIntent();
anime.setImageDrawable(getResources().getDrawable(getResources().
getIdentifier("drawable/" + animalshutup[(int)
Math.floor(level/12.1)], null, getPackageName())));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
final Intent intent;
if (animwhich.equals("reread")) {
intent = new Intent(AnimationActivity.this,
Reread1Activity.class);
}
}
}
}, 2000);
}
}
#Override
protected void onStop() {
super.onStop();
stopPlayer();
}
private int configureNumber(String tag){
Intent getData = getIntent();
int value;
if(getData.getStringExtra(tag)==null)
value=1;
else
if(getData.getStringExtra(tag).matches("-?([1-
9]\\d*)")&&Integer.parseInt(getData.getStringExtra(tag))>0)
value= Integer.parseInt(getData.getStringExtra(tag));
else
value=1;
return value;
}
private String configureText(String tag, String deflt){
Intent getData = getIntent();
String value;
if(getData.getStringExtra(tag)==null)
value=deflt;
else
if(getData.getStringExtra(tag).isEmpty())
value=deflt;
else
value= getData.getStringExtra(tag);
return value;
}
}
My screen xml code is below:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".MainActivity">
<ImageView
app:srcCompat="#drawable/arkaplansabit"
android:id="#+id/backgr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
My animation list xml code is below:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="1600"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="300"/>
<item android:drawable="#drawable/zipzipgozukapali1"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali2"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali3"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali4"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali5"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali6"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="300"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="400"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozukapali1"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali2"
android:duration="50"/>
<item android:drawable="#drawable/zipzipgozukapali3"
android:duration="50"/>
<item android:drawable="#drawable/zipzipgozukapali4"
android:duration="50"/>
<item android:drawable="#drawable/zipzipgozukapali5"
android:duration="50"/>
<item android:drawable="#drawable/zipzipgozukapali6"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="1100"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="500"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozukapali1"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali2"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali4"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali5"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali6"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="800"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="500"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozukapali1"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali2"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali3"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali4"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali5"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozukapali6"
android:duration="100"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="400"/>
<item android:drawable="#drawable/zipzipgozuacik1"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik3"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik4"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik5"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik6"
android:duration="200"/>
<item android:drawable="#drawable/zipzipgozuacik2"
android:duration="20000"/>
Please help me with this issue.
THANK YOU

How to Set the theme in Fragments programmatically?

I wrote a code for setting a theme for activities setTheme(R.style.darkTheme); but i don't know how to write in fragments activity to set the theme.The code is for activities
if(sharedPref.loadNightModeState()==true)
{
setTheme(R.style.darkTheme);
}
else {
setTheme(R.style.AppTheme);
}
This is my style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="backgroundcolor">#color/background</item>
<item name="textcolor">#color/textcolor</item>
<item name="buttoncolor">#color/red</item>
</style>
<style name="darkTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#212121</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="backgroundcolor">#303030</item>
<item name="textcolor">#color/white</item>
<item name="buttoncolor">#color/red</item>
</style>
</resources>
This is my fragment code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_state, container, false);
sharedPref=new SharedPref(getActivity());
if(sharedPref.loadNightModeState()==true)
{
setTheme(R.style.darkTheme);
}
else {
setTheme(R.style.AppTheme);
}
return view;
}
error is coming in this setTheme(R.style.AppTheme) line .

How can I get dark mode to work on my app?

I have set up a ListPreference with different values for the user's dark mode preference. Right now, I just want the app to work with the new dark mode implementation introduced in Android Q. If I set the system dark mode, the app changes because I have enabled isforcedarkallowed in styles.xml.
SettingsFragment:
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
PreferenceScreen screen = getPreferenceScreen();
final ListPreference listPreference = (ListPreference) findPreference("theme");
Preference preference = (Preference) findPreference("notq");
int api = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
if (api > 28) {
screen.removePreference(preference);
}
if (api < 29) {
screen.removePreference(listPreference);
}
listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
listPreference.setValue(newValue.toString());
theme = String.valueOf(listPreference.getEntry());
Log.d("debug", theme);
if (theme.equals("Light")) {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
}
if (theme.equals("Dark")) {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
}
if (theme.equals("System default")) {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM);
}
return true;
}
});
}
}
Arrays.xml (values for dark mode preference)
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Reply Preference -->
<string-array name="theme_entries">
<item>Light</item>
<item>Dark</item>
<item>System default</item>
</string-array>
<string-array name="theme_values">
<item>light</item>
<item>dark</item>
<item>default1</item>
</string-array>
</resources>
My listpreference:
<ListPreference
android:id="#+id/list"
app:entries="#array/theme_entries"
app:entryValues="#array/theme_values"
app:key="theme"
app:title="#string/theme_title"
app:useSimpleSummaryProvider="true" />
And the app theme I'm using (styles.xml):
<style name="AppTheme2" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:forceDarkAllowed" tools:targetApi="q">true</item>
<item name="android:isLightTheme" tools:targetApi="q">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#FFFFFF</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Any suggestions?
In your styles.xml you should not take Theme.AppCompat.DayNight.DarkActionBar as a parent while you enabled forceDarkAllowed. Change that to Theme.AppCompat or Theme.MaterialComponents. You cannot inherit from a daynight theme while forceDarkAllowed is set to true. You can read about that here https://developer.android.com/guide/topics/ui/look-and-feel/darktheme section forcedark.
String theme = String.valueOf(listPreference.getEntry());
Should work fine, you just didn't declare theme as a string
:)

PreferenceFragmentCompat won't change color when change theme and recreate activity

I'm using PreferenceFragmentCompat for preferences and when I change theme preference using ListPreference to a dark theme and recreate activity text stays dark and becomes not visible. If I reopen this fragment text becomes white, but it returns to black color on orientation change
Here is my styles:
<style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="preferenceTheme">#style/preferenceLight</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:textColorPrimary">#color/primaryText</item>
<item name="android:textColorSecondary">#color/secondaryText</item>
</style>
<style name="AppThemeDark" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="preferenceTheme">#style/preferenceDark</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowBackground">#color/primaryText</item>
<item name="android:textColorPrimary">#color/primaryTextDark</item>
<item name="android:textColorSecondary">#color/secondaryText</item>
</style>
<style name="preferenceLight" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:textColor">#color/primaryText</item>
<item name="android:textColorPrimary">#color/primaryText</item>
<item name="android:textColorSecondary">#color/secondaryText</item>
</style>
<style name="preferenceDark" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:textColor">#color/primaryTextDark</item>
<item name="android:textColorPrimary">#color/primaryTextDark</item>
<item name="android:textColorSecondary">#color/secondaryText</item>
</style>
and fragment
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener{
private lateinit var advertisePreferences: AdvertisePreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
advertisePreferences = AdvertisePreferences(context!!)
}
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).supportActionBar?.title = getString(R.string.settings)
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.app_preferences, rootKey)
}
override fun onPreferenceTreeClick(preference: Preference): Boolean {
when(preference.key){
}
return super.onPreferenceTreeClick(preference)
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
when(key){
getString(R.string.theme_key) -> {
activity?.recreate()
}
}
}
}
It seems it was bug in library. It now fixed in androidx.preference:preference:1.1.0-alpha02

Pass two Activities with Fade Animation Automatically

I have one activity that show my logo and i want change to second activity after that two seconds automatically.
I have done this but it doesn´t apply the effect.
Logo.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.logo_layout);
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent i = new Intent(Logo.this, Loggin.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(Logo.this);
startActivity(i, options.toBundle());
finishAfterTransition();
}
}, TIME);
}
res/transition/my_trasition.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<fade
android:duration="1000">
</fade>
</transitionSet>
res/values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppFullScreen" parent="#style/Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowEnterTransition">#transition/transicion</item>
<item name="android:windowExitTransition">#transition/transicion</item>
<item name="android:windowAllowEnterTransitionOverlap">false</item>
<item name="android:windowAllowReturnTransitionOverlap">false</item>
</style>

Categories