Reputation: 11
I am trying to set theme run time. and facing one problem that i have one layout which i use further to change its color depend on theme. But it display the random color rather than color which i set.
Here is code which i had tried.
Xml 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.DashboardActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/gradient_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@android:color/background_light"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/company_name"
android:textColor="@android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.6"
android:background="@color/white_gray" />
</LinearLayout>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java Code in onCreate() is as :
preferences = getSharedPreferences("ThemePreference", MODE_PRIVATE);
themeColor = preferences.getInt("primaryColor", R.color.colorPrimary);
themeColorDark = preferences.getInt("darkColor", R.color.colorPrimary);
if (themeColor == R.color.orange_colorPrimary) {
setTheme(R.style.AppTheme_orange);
} else if (themeColor == R.color.red_colorPrimary) {
setTheme(R.style.AppTheme_red);
} else if (themeColor == R.color.green_colorPrimary) {
setTheme(R.style.AppTheme_green);
} else if (themeColor == R.color.colorPrimary) {
setTheme(R.style.AppTheme);
}
gradientLayout = findViewById(R.id.gradient_layout);
gradientLayout.setBackgroundColor(themeColor);
I want to set color in gradientLayout which color i will get in my preference primaryColor.
if anyone is having solution than please reply.
Thank You in advance !!
Upvotes: 1
Views: 261