
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Load ImageView by URL on Android Using Picasso
This example demonstrates about how do I load an ImageView on Android using Picasso.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Gradle Scripts from Project → Click build.gradle (Module: app) → add dependency − Implementation ‘com.squareup.picasso−Picasso:2.5.2 and click “Sync now”.
Step 3 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android:orientation="vertical"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Load ImageView by URL" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="20sp" /> <ImageView android:id="@+id/image_view" android:layout_width="fill_parent" android:layout_height="300dp" android:layout_marginTop="16dp" /> <TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:textSize="24sp" android:gravity="center|bottom" android:textStyle="bold" /> </LinearLayout>
Step 4 − Add the following code to src/MainActivity.java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.squareup.picasso.Picasso; public class MainActivity extends AppCompatActivity { private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = findViewById(R.id.image_view); String url = "https://images.pexels.com/photos/814499/pexels-photo814499.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"; Picasso.with(this).load(url).into(imageView); } }
Step 5 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Click here to download the project code.