Site icon C1CTech

Android Glide Library Example

<p>On loading the list of images manually these three problems encounter i&period;e OutOfMemoryError&comma; slow loading and UI unresponsiveness&lpar;flickering&rpar;&period; To come out from these problems we can use <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Glide<&sol;strong><&sol;span> library&period; In this article&comma; we will talk about Glide in brief&comma; how glide solved the above problems and also build a simple image gallery app where all the images will be loaded from internet and displayed in a grid manner&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Introduction to Glide Library<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Glide is an open source image loading and caching library for Android APPs developed by <span style&equals;"color&colon; &num;008000&semi;"><strong>bumptech<&sol;strong><&sol;span>&period; It is fast&comma; efficient and widely used image loading library&comma; primarily focused on smooth scrolling&period; It is used in many popular Android Apps and one of the most popular library for image loading and caching&period;<&sol;p>&NewLine;<p>To load the image in an imageView using glide we have to simply write the below line&colon;<&sol;p>&NewLine;<pre>Glide&period;with&lpar;mContext&rpar;&NewLine;&period;load&lpar;imgUrl&rpar; &NewLine;&period;into&lpar;imageView&rpar;&semi;<&sol;pre>&NewLine;<p>Glide library solves the above discuss problems that come while loading a list of images manually &colon;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong class&equals;"kh lz">OutOfMemoryError<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Glide mainly takes these three as parameter i&period;e context&sol;fragment&sol;Activity&comma; imageUrl&sol;drawable image and imageView&period; In order to solve the <span style&equals;"color&colon; &num;008000&semi;"><strong>OutOfMemoryError<&sol;strong><&sol;span> problem&comma; it takes imageView&period; Let&&num;8217&semi;s say your server is sending an image of &lpar;2000&&num;215&semi;2000&rpar; but in your android application&comma; you are going to show an image of &lpar;200&&num;215&semi;200&rpar;&period; When you decode server image in memory it will take around 40mb&period;<&sol;p>&NewLine;<p>Glide has taken the imageView as a parameter so it knows the actual height and width of the imageView in which you want to show the image&period; It knows that it is &lpar;200&&num;215&semi;200&rpar;&period; So Glide instead of loading the full-size image&lpar;2000&&num;215&semi;2000&rpar; from the given URL into the disk cache it will first resize it to the size of the imageView&lpar;200&&num;215&semi;200&rpar; and then store it into the disk cache after that it will decode the resized image&lpar;200&&num;215&semi;200&rpar; into bitmap and load it into the memory so its size will become 4mb&period; In this way&comma; it helps to prevent your app from throwing popular <strong class&equals;"kh lz"><span style&equals;"color&colon; &num;008000&semi;">OutOfMemoryError<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Fast Loading and UI responsive<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Just like other systems do Glide will also cancel all the network call and only make network calls that are visible to user&period; For example&comma; you are scrolling list of images and you stop at 26th item&comma; so the network call will only be going for the 26 and above item that is visible&period;<&sol;p>&NewLine;<p>One more important thing that glide do internally is <span style&equals;"color&colon; &num;008000&semi;"><strong>caching<&sol;strong><&sol;span>&period; When we scroll the list of images from bottom to top it will not download the image again instead it will check the image in <span style&equals;"color&colon; &num;008000&semi;"><strong>memory cache<&sol;strong><&sol;span> if it exists then it will get it and display it to the user&period; But in case if it does not exist then it will check in <strong><span style&equals;"color&colon; &num;008000&semi;">disk cache<&sol;span><&sol;strong> if it is there then it will get the file image from the file system and decode it to bitmap&comma; store it to memory cache and return it to imageView&period; In case if the image is not available in disk cache also then only it will make a <strong><span style&equals;"color&colon; &num;008000&semi;">network call<&sol;span><&sol;strong> &comma; store it in both <strong><span style&equals;"color&colon; &num;008000&semi;">memory and disk cache<&sol;span><&sol;strong>&comma; get the image and display it&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1226" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;09&sol;Screenshot-2019-09-14-16&period;10&period;21-1024x550&period;png" alt&equals;"Screenshot 2019-09-14 16&period;10&period;21" width&equals;"777" height&equals;"417" &sol;><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Advantages of using Glide library &colon;<&sol;strong><&sol;span><&sol;h3>&NewLine;<ul>&NewLine;<li>Supports fetching&comma; decoding&comma; and displaying video stills&comma; images&comma; and animated GIFs&period;<&sol;li>&NewLine;<li>It is designed to work with Activity and Fragment’s life cycle too&period; You can pass the activity or fragment context with <span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"kh lz"><em class&equals;"lg">Glide&period;with&lpar;&rpar;<&sol;em><&sol;strong> <&sol;span>and it will brilliantly integrate with activity lifecycle callbacks such as <span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"kh lz"><em class&equals;"lg">onPause&lpar;&rpar;<&sol;em><&sol;strong><&sol;span> or <span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"kh lz"><em class&equals;"lg">onResume&lpar;&rpar;<&sol;em><&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>Glide supports <span style&equals;"color&colon; &num;008000&semi;"><strong>animated GIF<&sol;strong><&sol;span> images and because of Glide integrates with your activity life-cycle&comma; animated GIFs are also paused in <em class&equals;"lg"><strong><span style&equals;"color&colon; &num;008000&semi;">onStop&lpar;&rpar;<&sol;span><&sol;strong> <&sol;em>to avoid draining the battery in the background&period;<&sol;li>&NewLine;<li>Loads <strong><span style&equals;"color&colon; &num;008000&semi;">thumbnail<&sol;span><&sol;strong> &lpar;blurred&rpar; first and then loads the high-resolution image like in WhatsApp or Facebook<&sol;li>&NewLine;<li>Glide has its own implementation of <strong><span style&equals;"color&colon; &num;008000&semi;">HttpURLConnection<&sol;span><&sol;strong> which loads images from remote URLs over the network&period;<&sol;li>&NewLine;<li>One good thing about Glide is&comma; it provides various configuration and customization options&period; So&comma; you can tweak Glide library as per your requirement&period; You can read more about this at <strong><span style&equals;"color&colon; &num;008000&semi;"><a class&equals;"bz dk nf ng nh ni" style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;bumptech&sol;glide&sol;wiki&sol;Configuration" target&equals;"&lowbar;blank" rel&equals;"noopeneer noopener noreferrer">configuration<&sol;a><&sol;span><&sol;strong> page&period;<&sol;li>&NewLine;<li>You can also add <strong><span style&equals;"color&colon; &num;008000&semi;">placeholder<&sol;span><&sol;strong> image to display while loading the image or while the image loading fails&period;<&sol;li>&NewLine;<li>Supports <strong><span style&equals;"color&colon; &num;008000&semi;">Crossfading<&sol;span><&sol;strong> effects between the media<&sol;li>&NewLine;<li>Supports image arbitrary <strong><span style&equals;"color&colon; &num;008000&semi;">transformations<&sol;span><&sol;strong> like loading image in circular shape or any other shape&period;<&sol;li>&NewLine;<li>Provides better <strong><span style&equals;"color&colon; &num;008000&semi;">Memory and disk<&sol;span><&sol;strong> caching mechanisms&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">How to Use It&quest;<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Integrating <span style&equals;"color&colon; &num;008000&semi;"><strong>Glide<&sol;strong><&sol;span> in your project is very easy&period; First&comma; add the glide dependency to your <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; &NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;glide<&sol;span><&sol;strong>&NewLine; implementation 'com&period;github&period;bumptech&period;glide&colon;glide&colon;4&period;9&period;0'&NewLine; &NewLine;&rcub;<&sol;pre>&NewLine;<p>Second load the image into ImageView using below code snippet&period;<&sol;p>&NewLine;<pre>String imgUrl &equals; <strong><span style&equals;"color&colon; &num;008000&semi;">"https&colon;&sol;&sol;www&period;gettyimages&period;ca&sol;gi-resources&sol;images&sol;Homepage&sol;Hero&sol;UK&sol;CMS&lowbar;Creative&lowbar;164657191&lowbar;Kingfisher&period;jpg"<&sol;span><&sol;strong>&semi;&NewLine;&NewLine;ImageView imageView &equals; &lpar;ImageView&rpar; view&period;findViewById&lpar;R&period;id&period;thumbnail&rpar;&semi;&NewLine;&NewLine;Glide&period;with&lpar;mContext&rpar;&period;load&lpar;imgUrl&rpar;&NewLine;&period;thumbnail&lpar;0&period;5f&rpar;&NewLine;&period;crossFade&lpar;&rpar;&NewLine;&period;diskCacheStrategy&lpar;DiskCacheStrategy&period;ALL&rpar;&NewLine;&period;into&lpar;imageView&rpar;&semi;<&sol;pre>&NewLine;<p>Now let’s start building the image gallery app&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Building Image Gallery App<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; Create a new project in Android Studio from <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Project<&sol;strong><&sol;span>&period; Select <span style&equals;"color&colon; &num;008000&semi;"><strong>Blank Activity<&sol;strong><&sol;span> and then name the application as <strong><span style&equals;"color&colon; &num;008000&semi;">GlideDemo<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> and add <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;bumptech&sol;glide" target&equals;"&lowbar;blank" rel&equals;"nofollow noopener noreferrer">Glide<&sol;a><&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-recyclerview-example&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">RecyclerView<&sol;a><&sol;span><&sol;strong> dependencies&period;  <strong>RecyclerView<&sol;strong> is used to show the gallery images in a Grid fashion&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Add Dependencies Into build&period;gradle<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; implementation 'androidx&period;appcompat&colon;appcompat&colon;1&period;0&period;2'&NewLine; implementation 'androidx&period;constraintlayout&colon;constraintlayout&colon;1&period;1&period;3'&NewLine; testImplementation 'junit&colon;junit&colon;4&period;12'&NewLine; androidTestImplementation 'androidx&period;test&colon;runner&colon;1&period;1&period;1'&NewLine; androidTestImplementation 'androidx&period;test&period;espresso&colon;espresso-core&colon;3&period;1&period;1'&NewLine;&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&sol;&sol;glide<&sol;strong><&sol;span>&NewLine; implementation 'com&period;github&period;bumptech&period;glide&colon;glide&colon;4&period;9&period;0'&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; recyclerView<&sol;span><&sol;strong>&NewLine; implementation 'androidx&period;recyclerview&colon;recyclerview-selection&colon;1&period;0&period;0'&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p><strong>3<&sol;strong> &period;Open <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong> <span style&equals;"color&colon; &num;000000&semi;">and<&sol;span><&sol;span> add the <strong><span style&equals;"color&colon; &num;008000&semi;"><em>INTERNET<&sol;em><&sol;span><&sol;strong> permission as we need to get the image from Url&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;manifest xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; package&equals;"trendlife&period;glidedemo"&gt&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET"&sol;&gt&semi;<&sol;span><&sol;strong>&NewLine;&NewLine; &lt&semi;application&NewLine; android&colon;allowBackup&equals;"true"&NewLine; android&colon;icon&equals;"&commat;mipmap&sol;ic&lowbar;launcher"&NewLine; android&colon;label&equals;"&commat;string&sol;app&lowbar;name"&NewLine; android&colon;roundIcon&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round"&NewLine; android&colon;supportsRtl&equals;"true"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme"&gt&semi;&NewLine; &lt&semi;activity android&colon;name&equals;"&period;MainActivity"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine; &lt&semi;&sol;activity&gt&semi;&NewLine; &lt&semi;&sol;application&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;manifest&gt&semi;<&sol;pre>&NewLine;<p><strong>4<&sol;strong> &period;Open the layout file of your MainActivity and add the <span style&equals;"color&colon; &num;008000&semi;"><strong>recyclerView<&sol;strong><&sol;span> as shown below&colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;androidx&period;recyclerview&period;widget&period;RecyclerView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;recyclerview"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;scrollbars&equals;"vertical"&NewLine; &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;pre>&NewLine;<p><strong>5<&sol;strong> &period;Under <span style&equals;"color&colon; &num;008000&semi;"><strong>res &Implies; layout<&sol;strong><&sol;span>&comma; create a layout named <span style&equals;"color&colon; &num;008000&semi;"><strong>gallery&lowbar;thumbnail&period;xml<&sol;strong><&sol;span>&period; This layout contains an <span style&equals;"color&colon; &num;008000&semi;"><strong>ImageView<&sol;strong><&sol;span> to display the thumbnail image in the gallery view&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>gallery&lowbar;thumbnail&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"200dp"&NewLine; android&colon;layout&lowbar;height&equals;"200dp"&NewLine; android&colon;orientation&equals;"vertical"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;thumbnail"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;scaleType&equals;"fitXY" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;pre>&NewLine;<p><strong>6<&sol;strong> &period;Create a new model class <span style&equals;"color&colon; &num;008000&semi;"><strong>imageUrl&period;java<&sol;strong><&sol;span>&period; These URLs will be invoked by the Glide library on bind to display the images one by one in the image gallery&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">imageUrl&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package trendlife&period;glidedemo&semi;&NewLine;&NewLine;public class ImageUrl &lbrace;&NewLine;&NewLine;&NewLine; String imageUrl&semi;&NewLine;&NewLine; public String getImageUrl&lpar;&rpar; &lbrace;&NewLine; return imageUrl&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setImageUrl&lpar;String imageUrl&rpar; &lbrace;&NewLine; this&period;imageUrl &equals; imageUrl&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p><strong>7<&sol;strong> &period;Under <span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">the root<&sol;span><b> project <&sol;b><span style&equals;"color&colon; &num;000000&semi;">directory<&sol;span><&sol;span>&comma; create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>GalleryAdapter&period;java<&sol;strong><&sol;span> This is an adapter class which inflates the <span style&equals;"color&colon; &num;008000&semi;"><strong>gallery&lowbar;thumbnail&period;xml<&sol;strong><&sol;span> and renders the images in recyclerView&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>GalleryAdapter&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package trendlife&period;glidedemo&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;&NewLine;import com&period;bumptech&period;glide&period;Glide&semi;&NewLine;import com&period;bumptech&period;glide&period;Priority&semi;&NewLine;import com&period;bumptech&period;glide&period;request&period;RequestOptions&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;&NewLine;import androidx&period;annotation&period;NonNull&semi;&NewLine;import androidx&period;recyclerview&period;widget&period;RecyclerView&semi;&NewLine;&NewLine;public class GalleryAdapter extends RecyclerView&period;Adapter&lt&semi;GalleryAdapter&period;MyViewHolder&gt&semi; &lbrace;&NewLine;&NewLine; private ArrayList&lt&semi;ImageUrl&gt&semi; imageUrls&semi;&NewLine; private Context context&semi;&NewLine;&NewLine; public GalleryAdapter&lpar;Context context&comma; ArrayList&lt&semi;ImageUrl&gt&semi; imageUrls&rpar; &lbrace;&NewLine; this&period;context &equals; context&semi;&NewLine; this&period;imageUrls &equals; imageUrls&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;NonNull&NewLine; &commat;Override&NewLine; public MyViewHolder onCreateViewHolder&lpar;&commat;NonNull ViewGroup parent&comma; int viewType&rpar; &lbrace;&NewLine; View view &equals; LayoutInflater&period;from&lpar;parent&period;getContext&lpar;&rpar;&rpar;&period;inflate&lpar;R&period;layout&period;gallery&lowbar;thumbnail&comma; parent&comma; false&rpar;&semi;&NewLine; return new MyViewHolder&lpar;view&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onBindViewHolder&lpar;&commat;NonNull MyViewHolder holder&comma; int position&rpar; &lbrace;&NewLine; &NewLine; RequestOptions requestOptions &equals; new RequestOptions&lpar;&rpar;&NewLine; &period;placeholder&lpar;R&period;drawable&period;place&lowbar;holder&lowbar;image&rpar;&NewLine; &period;error&lpar;R&period;drawable&period;error&lowbar;img&rpar;&NewLine; &period;fitCenter&lpar;&rpar;&semi;&NewLine;&NewLine; Glide&period;with&lpar;context&rpar;&NewLine; &period;setDefaultRequestOptions&lpar;requestOptions&rpar;&NewLine; &period;load&lpar;imageUrls&period;get&lpar;position&rpar;&period;getImageUrl&lpar;&rpar;&rpar;&NewLine; &period;into&lpar;holder&period;img&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; class MyViewHolder extends RecyclerView&period;ViewHolder &lbrace;&NewLine;&NewLine; ImageView img&semi;&NewLine;&NewLine; public MyViewHolder&lpar;&commat;NonNull View itemView&rpar; &lbrace;&NewLine; super&lpar;itemView&rpar;&semi;&NewLine; img &equals; itemView&period;findViewById&lpar;R&period;id&period;thumbnail&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getItemCount&lpar;&rpar; &lbrace;&NewLine; return imageUrls&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p><strong>8<&sol;strong> &period;Finally&comma; open <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and pass the array list to recyclerView’s adapter class as shown below&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package trendlife&period;glidedemo&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;import androidx&period;recyclerview&period;widget&period;DefaultItemAnimator&semi;&NewLine;import androidx&period;recyclerview&period;widget&period;GridLayoutManager&semi;&NewLine;import androidx&period;recyclerview&period;widget&period;RecyclerView&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; RecyclerView recyclerView&semi;&NewLine; GalleryAdapter mAdapter&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; recyclerView &equals; findViewById&lpar;R&period;id&period;recyclerview&rpar;&semi;&NewLine;&NewLine;&NewLine; RecyclerView&period;LayoutManager layoutManager &equals; new GridLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma; 2&rpar;&semi;&NewLine; ArrayList imageUrlList &equals; prepareData&lpar;&rpar;&semi;&NewLine; mAdapter &equals; new GalleryAdapter&lpar;getApplicationContext&lpar;&rpar;&comma; imageUrlList&rpar;&semi;&NewLine;&NewLine;&NewLine; recyclerView&period;setLayoutManager&lpar;layoutManager&rpar;&semi;&NewLine; recyclerView&period;setItemAnimator&lpar;new DefaultItemAnimator&lpar;&rpar;&rpar;&semi;&NewLine; recyclerView&period;setAdapter&lpar;mAdapter&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; private ArrayList prepareData&lpar;&rpar; &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>&sol;&sol; here you should give your image URLs and that can be a link from the Internet<&sol;strong><&sol;span>&NewLine; String imageUrls&lbrack;&rsqb; &equals; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcQWqxdGGltL3Ap&lowbar;6e6j5L8Ga9Ly08VRSsIyGBPzkdMdCrSuDOomTA"&comma;&NewLine;"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcQ8lzDnZhrCxA6xeFtoRno&lowbar;VC2kmU5G5MCdLjscFT9EXcW9fRp1JA"&comma;&NewLine;"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcQ0Frq15F4tqShs3o8&lowbar;EOQWmRDnBpvqze2-Ivnh7&lowbar;hPcpFA&lowbar;nxJtA"&comma;&NewLine;"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcT1PLkjujhPe8bFhY&lowbar;C3eVTk3YZKV7D&lowbar;FvgmMdzRpU10f8cg2Xq"&comma;&NewLine;"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcR-BshanQe-72-&lowbar;aILEOrdy0tktLS3D0OyCuZETTHS-xspWyyTdgQ"&comma;&NewLine;"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcTIog4jVdAtTmktR6ZWeOLWSOuciGhIpnYcd&lowbar;ZaeG9fEFAoOKZsDw"&comma;&NewLine;"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcT2RiBylPwwMIa9Ky0eyss19pxrtKBcgiZGgY70G0YfbUXSqT1qNw"&comma;&NewLine;"https&colon;&sol;&sol;encrypted-tbn0&period;gstatic&period;com&sol;images&quest;q&equals;tbn&colon;ANd9GcQSpSvHExAZQVYjGSNFaG4aMoow4IIHXHDmVftcTnYf0ICaXtegPg"<&sol;span><&sol;strong>&NewLine;&rcub;&semi; &NewLine;&NewLine;ArrayList imageUrlList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi; &NewLine;for &lpar;int i &equals; 0&semi; i &lt&semi; imageUrls&period;length&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; ImageUrl imageUrl &equals; new ImageUrl&lpar;&rpar;&semi; &NewLine;imageUrl&period;setImageUrl&lpar;imageUrls&lbrack;i&rsqb;&rpar;&semi; &NewLine;imageUrlList&period;add&lpar;imageUrl&rpar;&semi; &rcub; &NewLine;Log&period;d&lpar;"MainActivity"&comma; "List count&colon; " &plus; imageUrlList&period;size&lpar;&rpar;&rpar;&semi; &NewLine;return imageUrlList&semi; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<p>If you run the app&comma; you can see the images displayed in a grid manner&period; Be sure that your device is connected to the internet&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1227" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;09&sol;Screenshot&lowbar;1568731358-576x1024&period;png" alt&equals;"Screenshot&lowbar;1568731358" width&equals;"372" height&equals;"662" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version