How to use gif image in android

Harshita Bambure
1 min readOct 5, 2021

--

In android, if we need to add gif images in our project so first of all, we need to add a dependency in the build.Gradle(:app) file.

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.17'

Let's start with the design part.

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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<pl.droidsonroids.gif.GifImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintWidth_percent="0.9"
app:layout_constraintVertical_bias="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@drawable/cart"
android:scaleType="fitXY"
android:layout_gravity="center"/>

<View
android:background="@android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<TextView
android:text="EMPTY CART"
android:textStyle="bold"
android:textColor="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.55"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:text="Looks like you haven't made \n your choice yet"
android:textStyle="bold"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:text="Back to Menu"
android:gravity="center"
android:backgroundTint="#FF5722"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>

I want to hide the action bar so I added the style in the themes folder.

themes.xml

<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">

Now we need to add this theme to the manifest file.

AndroidManifest.xml

<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/AppTheme2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
gif image of empty card UI.

That’s it. Happy Coding.

--

--

Harshita Bambure
Harshita Bambure

Written by Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .

No responses yet