'How to add a Lottie animation in Java programatically
I tried to add Lottie Animation to Java (Android) programmatically, but I keep failing. I will show my code below I need to change from drawable to Lottie.
This is the code for drawables:
if (status.equals("connect")) {
vpnBtn.setBackgroundResource(R.drawable.but_connect_light);
logTv.setTextColor(getResources().getColor(R.color.font_color));
} else if (status.equals("connecting")) {
vpnBtn.setBackgroundResource(R.drawable.but_connecting_dark);
logTv.setTextColor(getResources().getColor(R.color.font_color));
} else if (status.equals("connected")) {
vpnBtn.setBackgroundResource(R.drawable.but_disconnect_dark);
logTv.setTextColor(getResources().getColor(R.color.purple));
} else if (status.equals("failed")) {
vpnBtn.setBackgroundResource(R.drawable.but_connect_dark);
}
This is the xml relating above
<ImageView
android:id="@+id/vpnBtn"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:tag="1"
android:layout_width="140dp"
android:layout_height="140dp"
android:background="@drawable/but_connect_light"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"/>
Solution 1:[1]
At last i achieved this with the below solution
String vpnsBtn;
if (check.equals("night")){
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connectingg.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connected.json");
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
break;
}
}else {
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connectingg.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connected.json");
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
break;
}
}
LottieAnimationView animationView = findViewById(R.id.vpnBtn);
animationView.setImageAssetsFolder("vpnconnect");
animationView.setAnimation(vpnsBtn);
animationView.loop(true);
animationView.playAnimation();
Hope this will help other who want to use dynamic button with Lottie on Android.
Solution 2:[2]
First Use this dependency:
implementation 'com.airbnb.android:lottie:3.6.1'
Add LottieAnimationView
in xml:
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_loop="true"
app:lottie_speed="1" />
Change animation according to your use:
animation.setAnimation("loading.json");
Note: Add your lottie animation json file in asset folder.
Solution 3:[3]
The above solutions didn't work for me. It crashes the app.
try using this
animationView.setAnimation(R.raw.your_lottie_file)
or, adding it to the answer from Infinite
`String vpnsBtn;
if (check.equals("night")){
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connectingg;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connected
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
break;
}
}else {
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connectingg;;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connected;;
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
break;
}
}
LottieAnimationView animationView = findViewById(R.id.vpnBtn);
animationView.setImageAssetsFolder("vpnconnect");
animationView.setAnimation(vpnsBtn);
animationView.loop(true);
animationView.playAnimation();`
Solution 4:[4]
For using Lottie I did these:
In build.gradle(:app)
add this and sync:
implementation ‘com.airbnb.android:lottie:$lottieVersion’
then, add assets
file. Then, add the JSON files there. Then, go to activity_main.xml
file, add:
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lav_actionBar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_fileName="circle-load.json" --> add your JSON file name
app:lottie_loop="true" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Infinite Support |
Solution 2 | ahmad bajwa |
Solution 3 | Chris Barth |
Solution 4 | Mohammadreza |