This is my first post. Please dont shoot me. LOL
I successfully write code for gradient AnimationDrawable in one of activity. But when I try to implement the code in Theme Activity, it say :-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.learn.notetoself, PID: 1194
java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
at com.learn.notetoself.ThemeActivity.onClick(ThemeActivity.java:936)
Gradle Scripts (module)
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.learn.notetoself"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
viewBinding true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.mediarouter:mediarouter:1.3.0'
def nav_version = "2.5.3"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.5.1'
implementation 'androidx.navigation:navigation-ui:2.5.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'com.github.xabaras:RecyclerViewSwipeDecorator:1.4'
implementation 'com.google.code.gson:gson:2.8.8'
}
SUCCESSFUL ACTIVITY (ARCHIED ACTIVITY)
XML with Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".ArchivedActivity">
<View
android:id="@+id/background_image_archive"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_list" />
JAVA in onCreate medhod (my code applied drawer navigation)
public class ArchivedActivity extends DrawerBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityArchivedBinding = ActivityArchivedBinding.inflate(getLayoutInflater());
// Load themes
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int selectedTheme = mPrefs.getInt("SelectedThemeNav", 0);
setDynamicTheme(selectedTheme);
setContentView(activityArchivedBinding.getRoot());
// Set animation of changing color for app background
// Initialize the constraintLayout reference with the ConstraintLayout instance in the Archivedlayout
View imageViewArchive = findViewById(R.id.background_image_archive);
AnimationDrawable animationDrawable = (AnimationDrawable) imageViewArchive.getBackground();
// Set parameter to our animation
// 2500 = 2.5 seconds, 5000 = 5 seconds
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
}
ERROR ACTIVITY (THEME ACTIVITY)
XML with Constraint Layout
<?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=".ThemeActivity">
<View
android:id="@+id/holderbg"
android:background="@drawable/gradient_list"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/btnColorfulNav"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:background="@drawable/background_green"
android:elevation="8dp"
app:layout_constraintStart_toEndOf="@+id/btnBlueBg"
app:layout_constraintTop_toTopOf="@+id/btnBlueBg" />
JAVA in onCreate medhod (I do not paste all contents) (my code applied drawer navigation)
SharedPreferences mPrefs;
SharedPreferences.Editor mEditor;
ImageButton btnColorfulNav,
View holderbg;
public class ThemeActivity extends DrawerBaseActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityThemeBinding = ActivityThemeBinding.inflate(getLayoutInflater());
// Load themes for navigation bar
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// mPrefs = getSharedPreferences("MODE", Context.MODE_PRIVATE);
int selectedThemeNav = mPrefs.getInt("SelectedThemeNav", 0);
setDynamicThemeNav(selectedThemeNav);
setContentView(activityThemeBinding.getRoot());
// Load themes for previous selected background
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int selectedThemeBg = mPrefs.getInt("SelectedThemeBg", 0);
// setDynamicThemeBg(selectedThemeBg);
setThemeButtonConfirm(selectedThemeNav);
btnColorfulNav = findViewById(R.id.btnColorfulNav);
holderbg = findViewById(R.id.holderbg);
btnColorfulNav.setOnClickListener(this);
private void setThemeButtonConfirm(int selectedThemeBg) {
switch (selectedThemeBg){
case 10:
// Set the scale of previous button clicked
btnColorfulNav.setScaleX(1.2f);
btnColorfulNav.setScaleY(1.2f);
btnThemeSave.setBackgroundResource(R.color.theme9ColorPrimary);
// holderbg.setBackgroundResource(R.color.theme9ColorOnSecondary);
AnimationDrawable animationDrawable = (AnimationDrawable) holderbg.getBackground();
// Set parameter to our animation
// 2500 = 2.5 seconds, 5000 = 5 seconds
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
break;
}
}
@SuppressLint("ResourceType")
@Override
public void onClick(View v) {
mEditor = mPrefs.edit();
switch (v.getId()){
case R.id.btnColorfulNav:
// Set scale animation
btnColorfulNav.animate().translationY(0).scaleX(1.2f).scaleY(1.2f).setDuration(800).start();
// Default the scale buttons
btnRedNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btnOrangeNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btnYellowNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btnGreenNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btnBlueNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btnIndigoNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btnVioletNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btnPinkNav.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
// Set animation of changing color for app background
// Initialize the constraintLayout reference with the ConstraintLayout instance in the main layout
AnimationDrawable animationDrawable = (AnimationDrawable) holderbg.getBackground();
// Set parameter to our animation
// 2500 = 2.5 seconds, 5000 = 5 seconds
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
break;
}
mEditor.apply();
}
ERROR refer to below code :-
AnimationDrawable animationDrawable = (AnimationDrawable) holderbg.getBackground();
What should I do? What is concept applied?
Your help will be very much appreciated!
Thank you!