Open In App

How to Make a Meme Sharing App in Android Studio?

Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisites:

In this article, we will learn how to create an app using Kotlin where memes will be fetched from Reddit using an API and will be shown on the screen. These memes can also be shared using other apps like(WhatsApp, Facebook, etc) to your friends. We will learn how to do API calls and how to use Volley and Glide libraries in our project. Note that we are going to implement this project using the Kotlin language.  A sample video is given below to get an idea about what we are going to do in this article.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and make the following layout or add the below code to that file. Below is the code for the activity_main.xml file. 

Design for Meme Share App

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <!--Layout where image will be loaded-->
    <ImageView
        android:id="@+id/memeImageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/shareButton"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars"/>
    
    <!--Progress bar which gets disabled whenever meme is loaded-->
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/memeImageView"
        app:layout_constraintLeft_toLeftOf="@id/memeImageView"
        app:layout_constraintRight_toRightOf="@id/memeImageView"
        app:layout_constraintTop_toTopOf="@id/memeImageView" />
    
    <!--Next Button which loads another meme-->
    <Button
        android:id="@+id/nextButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:onClick="nextmeme"
        android:text="NEXT"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/guideline"
        android:padding="32dp"
        tools:ignore="OnClick" />
    
    <!--Share button which allows you to share memes-->
    <Button
        android:id="@+id/shareButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="SHARE"
        android:onClick="sharememe"
        android:padding="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/guideline"
        tools:ignore="OnClick" />
  
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Add dependency and Modify the AndroidManifest.xml

Search for an API that has lots of memes, for eg we will be using this API. To load the memes from the API, we will be using the Volley library from which we will get a URL from the JSON object, to convert that URL to an actual image we will be using the Glide library. Navigate to the Gradle Scripts> build.gradle(Module : appname.app) and add the following dependencies

Kotlin




implementation 'com.android.volley:volley:1.2.1'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'


Navigate to the app > manifests > AndroidManifest.xml and add internet permission to your app’s manifest, without this, we won’t be able to connect to the internet. 

<uses-permission android:name="android.permission.INTERNET" /> 

Step 4: Working with the MainActivity.kt file

Now, open MainActivity.kt file here, you need to create three functions:-

loadmeme()

It loads a meme from API using Volley and shows it on the app screen.

Kotlin




private fun loadmeme() {
        // Initializing a progress bar which 
        // gets disabled whenever image is loaded.
        val progressBar: ProgressBar =findViewById(R.id.progressBar)
        progressBar.visibility= View.VISIBLE
        val meme: ImageView =findViewById(R.id.memeImageView)
          
        // Instantiate the RequestQueue.
        val queue = Volley.newRequestQueue(this)
        val url = "https://meme-api.herokuapp.com/gimme"
          
        // Request a JSON object response from the provided URL.
        val jsonObjectRequest = JsonObjectRequest(
            Request.Method.GET, url, null,
            { response ->
                // Storing the url of meme from the     API.
                presentImageUrl = response.getString("url")
                // Displaying it with the use of Glide.
                Glide.with(this).load(presentImageUrl).listener(object: RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        progressBar.visibility= View.GONE
                        return false
                    }
  
                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: com.bumptech.glide.request.target.Target<Drawable>?,
                        dataSource: com.bumptech.glide.load.DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        progressBar.visibility= View.GONE
                        return false
  
                    }
                }).into(meme)
            }
        ) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show()
        }
        // Add the request to the RequestQueue.
        queue.add(jsonObjectRequest)
    }


nextmeme()

It will load another meme whenever we click on the NEXT button on the app screen.

Kotlin




fun nextmeme(view: View) {
        // Calling loadmeme() whenever
        // Next button is clicked.
        loadmeme()
    }


sharememe()

It allows us to share the meme URL to other platforms like WhatsApp, Fb, etc by clicking on the button SHARE on the app screen.

Kotlin




fun sharememe(view: View) {
      // Creating an Intent to share
      // the meme with other apps.
      val intent= Intent(Intent.ACTION_SEND)
      intent.type="text/plain"
      intent.putExtra(Intent.EXTRA_TEXT, "Hey, Checkout this cool meme $presentImageUrl")
      val chooser = Intent.createChooser(intent, "Share this meme")
      startActivity(chooser)
}


Complete Code of MainActivity.kt File

Kotlin




package com.example.gfgmeme
  
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
  
class MainActivity : AppCompatActivity() {
    var presentImageUrl: String?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
          
        // Calling loadmeme() to display the meme.
        loadmeme()
    }
    private fun loadmeme() {
        // Initializing a progress bar which
        // gets disabled whenever image is loaded.
        val progressBar: ProgressBar =findViewById(R.id.progressBar)
        progressBar.visibility= View.VISIBLE
        val meme: ImageView =findViewById(R.id.memeImageView)
          
        // Instantiate the RequestQueue.
        val queue = Volley.newRequestQueue(this)
        val url = "https://meme-api.herokuapp.com/gimme"
          
        // Request a JSON object response from the provided URL.
        val jsonObjectRequest = JsonObjectRequest(
            Request.Method.GET, url, null,
            { response ->
                  
                // Storing the url of meme from the API.
                presentImageUrl = response.getString("url")
                  
                // Displaying it with the use of Glide.
                Glide.with(this).load(presentImageUrl).listener(object: RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        progressBar.visibility= View.GONE
                        return false
                    }
  
                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: com.bumptech.glide.request.target.Target<Drawable>?,
                        dataSource: com.bumptech.glide.load.DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        progressBar.visibility= View.GONE
                        return false
  
                    }
                }).into(meme)
            }
        ) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show()
        }
        // Add the request to the RequestQueue.
        queue.add(jsonObjectRequest)
    }
  
    fun nextmeme(view: View) {
        // Calling loadmeme() whenever 
        // Next button is clicked.
        loadmeme()
    }
    fun sharememe(view: View) {
        // Creating an Intent to share 
        // the meme with other apps.
        val intent= Intent(Intent.ACTION_SEND)
        intent.type="text/plain"
        intent.putExtra(Intent.EXTRA_TEXT, "Hey, Checkout this cool meme $presentImageUrl")
        val chooser = Intent.createChooser(intent, "Share this meme")
        startActivity(chooser)
    }
}


Output:



Last Updated : 30 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads