Application Programming Interface calling is the process of making requests to external web-based services to retrieve or manipulate data. APIs provide a standardized way for different software applications to communicate with each other. It involves sending a request from one application to another over the internet using a specific set of rules and protocols. The requesting application sends an API request to the target application, which then processes the request and sends back a response containing the requested data. API calls can be done using various programming languages and tools, such as cURL, Postman, Python, Java, and many others.Â
Meme
A meme is a cultural phenomenon that spreads rapidly through the internet, typically in the form of humorous images, video, or piece of text that is shared widely and often parodied or adapted.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout      android:layout_width = "match_parent"     android:layout_height = "match_parent"     android:orientation = "vertical"     android:gravity = "center_horizontal"     android:background = "#F3FCFF"     tools:context = ".MainActivity" >       < TextView         android:layout_width = "wrap_content"         android:layout_height = "wrap_content"         android:text = "My Meme App"         android:textSize = "40sp"         android:textStyle = "bold"         android:textColor = "#006875"         android:layout_marginTop = "40dp" />       < androidx.cardview.widget.CardView         android:layout_width = "340dp"         android:layout_height = "500dp"         android:layout_marginTop = "15dp"         android:layout_gravity = "center_horizontal"         app:cardCornerRadius = "25dp" >           < RelativeLayout             android:layout_width = "wrap_content"             android:layout_height = "wrap_content" >               < ImageView                 android:id = "@+id/imgMeme"                 android:layout_width = "match_parent"                 android:layout_height = "match_parent"                 android:paddingLeft = "20dp"                 android:scaleType = "fitCenter"                 android:paddingTop = "20dp"                 android:paddingRight = "20dp" />               < ProgressBar                 android:layout_width = "wrap_content"                 android:layout_height = "wrap_content"                 android:layout_centerVertical = "true"                 android:layout_marginLeft = "150dp"                 android:visibility = "invisible" />         </ RelativeLayout >         </ androidx.cardview.widget.CardView >       < androidx.cardview.widget.CardView         android:layout_width = "200dp"         android:layout_height = "60dp"         app:cardCornerRadius = "30dp"         android:layout_marginTop = "20dp"         android:outlineAmbientShadowColor = "@color/black"         android:outlineSpotShadowColor = "@color/white" >           < RelativeLayout             android:layout_width = "wrap_content"             android:layout_height = "wrap_content" >             < ImageView                 android:id = "@+id/btnShare"                 android:layout_width = "40dp"                 android:layout_height = "40dp"                 android:layout_marginTop = "10dp"                 android:layout_marginLeft = "30dp"                 android:src = "@drawable/share" />               < ImageView                 android:id = "@+id/btnNext"                 android:layout_width = "40dp"                 android:layout_height = "40dp"                 android:layout_marginTop = "10dp"                 android:src = "@drawable/next"                 android:layout_marginLeft = "120dp" />         </ RelativeLayout >     </ androidx.cardview.widget.CardView >   </ LinearLayout > |
Output UI:
Â
Step 3:
In your java folder create a new java file. Add this code to your new Java file.
Java
package com.anas.memeshareapp;   import android.content.Context; import android.graphics.Bitmap; import android.util.LruCache;   import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.Volley;   public class MySingleton {       private static MySingleton instance;     private RequestQueue requestQueue;     private ImageLoader imageLoader;     private static Context ctx;       private MySingleton(Context context) {         ctx = context;         requestQueue = getRequestQueue();           imageLoader = new ImageLoader(requestQueue,                 new ImageLoader.ImageCache() {                     private final LruCache<String, Bitmap>                             cache = new LruCache<String, Bitmap>( 20 );                       @Override                     public Bitmap getBitmap(String url) {                         return cache.get(url);                     }                       @Override                     public void putBitmap(String url, Bitmap bitmap) {                         cache.put(url, bitmap);                     }                 });     }       public static synchronized MySingleton getInstance(Context context) {         if (instance == null ) {             instance = new MySingleton(context);         }         return instance;     }       public RequestQueue getRequestQueue() {         if (requestQueue == null ) {             // getApplicationContext() is key, it keeps you from leaking the             // Activity or BroadcastReceiver if someone passes one in.             requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());         }         return requestQueue;     }       public <T> void addToRequestQueue(Request<T> req) {         getRequestQueue().add(req);     }       public ImageLoader getImageLoader() {         return imageLoader;     } } |
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
Java
package com.anas.memeshareapp;   import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity;   import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView;   import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.bumptech.glide.Glide; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target;   import org.json.JSONException; import org.json.JSONObject;   public class MainActivity extends AppCompatActivity {       ImageView imgMeme;     ImageView btnNext;     ImageView btnShare;       String url ;       @Override     protected void onCreate(Bundle savedInstanceState) {         super .onCreate(savedInstanceState);         setContentView(R.layout.activity_main);           imgMeme = findViewById(R.id.imgMeme);         btnNext = findViewById(R.id.btnNext);         btnShare = findViewById(R.id.btnShare);             apiCall();         btnNext.setOnClickListener( new View.OnClickListener() {             @Override             public void onClick(View v) {                   apiCall();             }         });           btnShare.setOnClickListener( new View.OnClickListener() {             @Override             public void onClick(View v) {                   BitmapDrawable bitmapDrawable = (BitmapDrawable) imgMeme.getDrawable();                 Bitmap bitmap = bitmapDrawable.getBitmap();                   String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(),bitmap, "Title" , null );                 Uri uri = Uri.parse(bitmapPath);                 Intent intent = new Intent(Intent.ACTION_SEND);                 intent.setType( "image/*" );                 intent.putExtra(Intent.EXTRA_STREAM,uri);                 startActivity(Intent.createChooser(intent, "share to :" ));             }         });     }       public void apiCall(){           JsonObjectRequest jsonObjectRequest = new JsonObjectRequest                 (Request.Method.GET, url, null , new Response.Listener<JSONObject>() {                       @Override                     public void onResponse(JSONObject response) {                         try {                               url = response.getString( "url" );                             Glide.with(MainActivity. this ).load(url).into(imgMeme);                           }                         catch (JSONException e){                             e.printStackTrace();                         }                       }                 }, new Response.ErrorListener() {                       @Override                     public void onErrorResponse(VolleyError error) {                         }                 });           MySingleton.getInstance( this ).addToRequestQueue(jsonObjectRequest);     } } |
Step 6: Make some changes in your gradle file
Add these dependencies to itÂ
implementation 'com.android.volley:volley:1.2.1' implementation 'com.github.bumptech.glide:glide:4.15.1'
Â
Note: Make sure that your emulator is connected to the internet.
Output:
Â