Picasso Library Visual Flair Using Android Application

Introduction
 
This article demonstrates how to add Picasso library to an Android application using Android Studio. 
 
Picasso Library 
 
Images add much-needed context and visual flair to an Android application. Picasso allows hassle-free image loading in your application -often in one line of code!

"http://square.github.io/picasso" Click Here!
 
 
 
Many common pitfalls of images loading on Android are handled automatically by Picasso. 
  • Handling ImageView Recycling and download cancelation in an adapter.
  • Complex image transformations with minimal memory use.
  • Automatic memory and disk caching 
Let's start.
 
Step 1
 
Open Android Studio and create a new project. Give an application name, company domain, check "include C++" option if you wish to use C++ for coding the project, and click "Next".
 
 
 
Step 2
 
After the process completes, select "Project Setting". I have selected Project Model.
 
 
 
Step 3
 
Next, extract the app >> src >> main >> res >> layout folder and click open the activity_main.xml page.
 
 
 
Select activity_main.xml page. The XML code will appear. Just replace that with the following code.
 
 
 
XML Code 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:orientation="vertical"  
  10.     android:paddingRight="@dimen/activity_horizontal_margin"  
  11.     android:paddingTop="@dimen/activity_vertical_margin"  
  12.     android:gravity="center"  
  13.     tools:context="com.example.ravir.app.MainActivity">  
  14.   
  15.     <ImageView  
  16.         android:scaleType="centerInside"  
  17.         android:id="@+id/imageview"  
  18.         android:src="@drawable/icons1"  
  19.         android:layout_width="300dp"  
  20.         android:layout_height="300dp" />  
  21.     <Button  
  22.         android:layout_width="200dp"  
  23.         android:id="@+id/button"  
  24.         android:layout_height="wrap_content"  
  25.         android:text="LOAD IMAGE"/>  
  26.   
  27. </LinearLayout>  
Step 4
 
Next, go to app >> src >>main >> res  >> AndroidManifest.xml. Click to open Manifest.xml file.
 
The XML code will apppear. Just enable or activate the INTERNET and ACCESS_NETWORK_STATE permissions.
 
 
 
Step 5
 
Next, go to GitHub, provide Picasso Library link (Click here), and copy this Gradle code to replace with your build gradle.
 
 
 
Step 6
 
Go to app >> build.gradle and click open the Gradle page. You will have the Gradle compile code; replace the  <dependencies> tag.
 
 
 
Step 7
 
Next, go to app >> src >> >>main >> java >> MainActivity.java.
 
The jave code will appear. Just replace that with the following jave code. You need to have your own "URL " for Android image link.
 
 
 
Java Code 
  1. package com.example.ravir.app;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.ImageView;  
  8.   
  9. import com.squareup.picasso.Picasso;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     ImageView imageview;  
  14.     Button button;  
  15.   
  16.     String url = "https://developer.android.com/_static/2f20c0c6d8/images/android/touchicon-180.png";  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         imageview =(ImageView)findViewById (R.id.imageview);  
  23.         button =(Button)findViewById (R.id.button);  
  24.   
  25.         button.setOnClickListener(new View.OnClickListener()  
  26.         {  
  27.             @Override  
  28.             public void onClick(View view)  
  29.             {  
  30.                   
  31.             }  
  32.         });  
  33.     }  
  34. }  
Step 8
    
Next, go to Picasso Library, select copy the one line code, and transform the image code.
 
 
Copy the image transformation URL, placeholder, error , into.
 
 
 
Next, in the following Picasso code, replace the MainActivity Page <onClick> tag.
 
 
 
<onClick> code
  1. Picasso.with(getApplicationContext())  
  2.                         .load(url)  
  3.                         .placeholder(R.drawable.icons1)  
  4.                         .error(R.drawable.icons2)  
  5.                         .into(imageview);  
Step 9 
   
Next, go to Android Studio and deploy the application. Select deploymet target.
 
 
 
OUTPUT 1
 
Run the application in your desired emulator (Shift + F10).
 
Click the "LOAD IMAGE" button to check if you have successfully set the URL image. 
 
 
 
Step 10 
   
If you Erase the URL, it shows an error image in your application. 
 
OUTPUT 2
 
Select Run application, then select your emulator (Shift + F10).
 
 
 
Finally, we have successfully created Picasso Library Android application.

Up Next
    Ebook Download
    View all
    Learn
    View all