Site icon C1CTech

Android GridLayoutManager Example With RecyclerView

&NewLine;<p>This article is about Android <span style&equals;"color&colon; &num;008000&semi;"><strong>GridLayoutManager<&sol;strong><&sol;span> and how to use it with <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span> to show items in uniform grid with the help of simple android application&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"6QoWgMyLf-s" title&equals;"Android GridLayoutManager Example With RecyclerView"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;6QoWgMyLf-s"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;6QoWgMyLf-s&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android GridLayoutManager Example With RecyclerView"><&sol;a><&sol;amp-youtube>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button is-style-outline is-style-outline--1"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;GridUsingRecyclerView" style&equals;"background-color&colon;&num;520599" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>GridLayoutManager<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>The&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>RecyclerView<&sol;strong><&sol;span>&nbsp&semi;widget is a more advanced and flexible version of&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>ListView<&sol;strong><&sol;span>&period; It is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">A <span style&equals;"color&colon; &num;0000ff&semi;"><strong>LayoutManager<&sol;strong><&sol;span> manages how the items in the RecyclerView are arranged&period; With RecyclerView&comma;&nbsp&semi; you can use <strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">GridLayoutManager<&sol;span><&sol;span> &lpar;to show items in grid form i&period;e&period; row and column&rpar; or <span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">LinearLayoutManager<&sol;span><&sol;span> &lpar;to show items in horizontal and vertical list&rpar;<&sol;strong>&period;&nbsp&semi;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Let’s implement GridLayoutManager using RecyclerView in simple android application&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong><span class&equals;"has-inline-color">Creating New Project<&sol;span><&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>1<&sol;strong>&period; In&nbsp&semi;<strong>Android Studio<&sol;strong>&comma; go to&nbsp&semi;<strong>File &Implies; New Project<&sol;strong>&comma; fill all the details required to create a new project and then click on&nbsp&semi;<strong>finish<&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>2<&sol;strong>&period; Open&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span>&comma; add the below <span style&equals;"color&colon; &num;0000ff&semi;"><strong>recyclerView<&sol;strong><&sol;span> dependency and then <span style&equals;"color&colon; &num;008000&semi;"><strong>sync<&sol;strong><&sol;span> the project&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">dependencies <strong>&lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;add recyclerView dependency&NewLine; <&sol;span><&sol;strong>implementation <strong>'androidx&period;recyclerview&colon;recyclerview&colon;1&period;1&period;0'&NewLine;&rcub;<&sol;strong><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>3<&sol;strong>&period; Open the layout file&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span>&nbsp&semi;and add the below code&period; This file consist of one <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span> only&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span class&equals;"has-inline-color" style&equals;"color&colon; &num;0000ff&semi;">&nbsp&semi;<strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version<&sol;strong><strong>&equals;"1&period;0" <&sol;strong><strong>encoding<&sol;strong><strong>&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;<br><&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>android<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br><&sol;strong> <strong>xmlns&colon;<&sol;strong><strong>app<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br><&sol;strong> <strong>xmlns&colon;<&sol;strong><strong>tools<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong> <strong>tools<&sol;strong><strong>&colon;context<&sol;strong><strong>&equals;"&period;MainActivity"<&sol;strong>&gt&semi;<br><br> &lt&semi;<strong>androidx&period;recyclerview&period;widget&period;RecyclerView<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;recyclerView"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toTopOf<&sol;strong><strong>&equals;"parent" <&sol;strong>&sol;&gt&semi;<br><br>&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>4&period;&nbsp&semi;<&sol;strong>Create a new layout file&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>recyclerview&lowbar;item&period;xml<&sol;strong>&nbsp&semi;<&sol;span><strong>&lpar;layout-&gt&semi;New-&gt&semi;Layout Resource File&rpar;<&sol;strong> and add the below code&period; This layout file represents an item of a recyclerView which consist of an <span style&equals;"color&colon; &num;008000&semi;"><strong>ImageView<&sol;strong><&sol;span> &lpar;represents Android version name image&rpar; and a <span style&equals;"color&colon; &num;008000&semi;"><strong>TextView<&sol;strong><&sol;span> &lpar;represents Android version name&rpar;&period;<span style&equals;"font-size&colon; inherit&semi;"> <&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>recyclerview&lowbar;item&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version<&sol;strong><strong>&equals;"1&period;0" <&sol;strong><strong>encoding<&sol;strong><strong>&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;<br><&sol;em><em><br><&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>android<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br><&sol;strong> <strong>xmlns&colon;<&sol;strong><strong>app<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;background<&sol;strong><strong>&equals;"&commat;color&sol;lightPurple"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;padding<&sol;strong><strong>&equals;"&commat;dimen&sol;padding&lowbar;10dp"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;margin<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;2dp"<&sol;strong>&gt&semi;<br><br> &lt&semi;<strong>ImageView<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;imgVersionName"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"&commat;dimen&sol;width&lowbar;150dp"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"&commat;dimen&sol;height&lowbar;150dp"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;scaleType<&sol;strong><strong>&equals;"fitXY"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toTopOf<&sol;strong><strong>&equals;"parent" <&sol;strong>&sol;&gt&semi;<br><br> &lt&semi;<strong>TextView<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;tvVersionName"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;marginTop<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;12dp"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;gravity<&sol;strong><strong>&equals;"center"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;textColor<&sol;strong><strong>&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;textSize<&sol;strong><strong>&equals;"&commat;dimen&sol;textSize&lowbar;18sp"<br><&sol;strong> <strong>android<&sol;strong><strong>&colon;textStyle<&sol;strong><strong>&equals;"bold"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;imgVersionName"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;imgVersionName"<br><&sol;strong> <strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toBottomOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;imgVersionName" <&sol;strong>&sol;&gt&semi;<br><br>&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>5<&sol;strong>&period; Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>Item&period;java<&sol;strong><&sol;span>&nbsp&semi;and add the below code&period; It consist of two properties&lpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>versionName<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>ImageResourceId<&sol;strong><&sol;span> &lpar;for Image&rpar;&rpar; and getter methods for each property&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong style&equals;"color&colon; rgb&lpar;0&comma; 0&comma; 255&rpar;&semi; font-size&colon; inherit&semi;">Item&period;java<&sol;strong><&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;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;gridusingrecyclerview&semi;&NewLine;&NewLine;<strong>public class <&sol;strong>Item &lbrace;&NewLine;&NewLine; <strong>private int imageResourseId<&sol;strong>&semi;&NewLine; <strong>private <&sol;strong>String <strong>versionName<&sol;strong>&semi;&NewLine;&NewLine; <strong>public <&sol;strong>Item&lpar;<strong>int <&sol;strong>imageResourseId&comma; String versionName&rpar; &lbrace;&NewLine; <strong>this<&sol;strong>&period;<strong>imageResourseId <&sol;strong>&equals; imageResourseId&semi;&NewLine; <strong>this<&sol;strong>&period;<strong>versionName <&sol;strong>&equals; versionName&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong>public int <&sol;strong>getImageResourseId&lpar;&rpar; &lbrace;&NewLine; <strong>return imageResourseId<&sol;strong>&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong>public <&sol;strong>String getVersionName&lpar;&rpar; &lbrace;&NewLine; <strong>return versionName<&sol;strong>&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>6<&sol;strong>&period; Open <strong><span style&equals;"color&colon; &num;0000ff&semi;">drawable<&sol;span><&sol;strong> folder under <strong><span style&equals;"color&colon; &num;0000ff&semi;">res<&sol;span><&sol;strong> directory and paste all the images which you have copied from somewhere else&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>7<&sol;strong>&period; Now open <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and add the below code&period; Here <span style&equals;"color&colon; &num;008000&semi;"><strong>prepareItemData&lpar;&rpar;<&sol;strong><&sol;span> method adds Item data to list&comma; which we are passing to recyclerView Adapter &lpar;<strong>ItemAdapter<&sol;strong>&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong style&equals;"color&colon; rgb&lpar;0&comma; 0&comma; 255&rpar;&semi; font-size&colon; inherit&semi;">MainActivity&period;java<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;gridusingrecyclerview&semi;&NewLine;&NewLine;<strong>import <&sol;strong>android&period;os&period;Bundle&semi;&NewLine;&NewLine;<strong>import <&sol;strong>java&period;util&period;ArrayList&semi;&NewLine;<strong>import <&sol;strong>java&period;util&period;List&semi;&NewLine;&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;<strong>import <&sol;strong>androidx&period;recyclerview&period;widget&period;GridLayoutManager&semi;&NewLine;<strong>import <&sol;strong>androidx&period;recyclerview&period;widget&period;RecyclerView&semi;&NewLine;&NewLine;<strong>public class <&sol;strong>MainActivity <strong>extends <&sol;strong>AppCompatActivity &lbrace;&NewLine;&NewLine; RecyclerView <strong>recyclerView<&sol;strong>&semi;&NewLine; <strong>private <&sol;strong>List&lt&semi;Item&gt&semi; <strong>list <&sol;strong>&equals; <strong>new <&sol;strong>ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&NewLine; &commat;Override&NewLine; <strong>protected void <&sol;strong>onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; <strong>super<&sol;strong>&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;<strong><em>activity&lowbar;main<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;get recyclerView by its id<&sol;strong><&sol;span>&NewLine; <strong>recyclerView <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>recyclerView<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;add Item data to list<&sol;strong><&sol;span>&NewLine; prepareItemData&lpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;set GridLayoutManager in recyclerView and show items in grid with two columns<&sol;strong><&sol;span>&NewLine; <strong>recyclerView<&sol;strong>&period;setLayoutManager&lpar;<strong>new <&sol;strong>GridLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma; 2&rpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;set adapter ItemAdapter in recyclerView<&sol;strong><&sol;span>&NewLine; <strong>recyclerView<&sol;strong>&period;setAdapter&lpar;<strong>new <&sol;strong>ItemAdapter&lpar;<strong>list<&sol;strong>&comma; <strong>this<&sol;strong>&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong>public void <&sol;strong>prepareItemData&lpar;&rpar; &lbrace;&NewLine; Item item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>eclair<&sol;em><&sol;strong>&comma; <strong>"Eclair"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>froyo<&sol;em><&sol;strong>&comma; <strong>"Froyo"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>gingerbread<&sol;em><&sol;strong>&comma; <strong>"Gingerbread"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>honeycomb<&sol;em><&sol;strong>&comma; <strong>"Honeycomb"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>ice&lowbar;cream&lowbar;sandwich<&sol;em><&sol;strong>&comma; <strong>"Ice Cream Sandwich"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>jellybean<&sol;em><&sol;strong>&comma; <strong>"Jelly Bean"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>kitkat<&sol;em><&sol;strong>&comma; <strong>"KitKat"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>lollipop<&sol;em><&sol;strong>&comma; <strong>"Lollipop"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>marshmallow<&sol;em><&sol;strong>&comma; <strong>"Marshmallow"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>nougat<&sol;em><&sol;strong>&comma; <strong>"Nougat"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>oreo<&sol;em><&sol;strong>&comma; <strong>"Oreo"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine; item &equals; <strong>new <&sol;strong>Item&lpar;R&period;drawable&period;<strong><em>pie<&sol;em><&sol;strong>&comma; <strong>"Pie"<&sol;strong>&rpar;&semi;&NewLine; <strong>list<&sol;strong>&period;add&lpar;item&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>8<&sol;strong>&period; Now create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>ItemAdapter&period;java<&sol;strong><&sol;span> and add the below code&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;008000&semi;"><strong>onCreateViewHolder&lpar;&rpar;<&sol;strong>&colon; <&sol;span>inflate the item layout and create the View Holder&period;<&sol;li><li><span style&equals;"color&colon; &num;008000&semi;"><strong>onBindViewHolder&lpar;&rpar;<&sol;strong>&colon;<&sol;span> set the view attributes based on the data&period;<&sol;li><li><strong style&equals;"color&colon; rgb&lpar;0&comma; 128&comma; 0&rpar;&semi; font-size&colon; inherit&semi;">getItemCount&lpar;&rpar;&colon;<&sol;strong><span style&equals;"font-size&colon; inherit&semi; color&colon; rgb&lpar;0&comma; 0&comma; 0&rpar;&semi;"> <&sol;span><span style&equals;"font-size&colon; inherit&semi; color&colon; rgb&lpar;0&comma; 0&comma; 0&rpar;&semi;">determine the number of items&period;<&sol;span><&sol;li><li><span style&equals;"color&colon; &num;008000&semi;"><strong>setOnClickListener&colon;<&sol;strong><&sol;span> show a <strong>Toast<&sol;strong> on click of each recyclerView Item&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong style&equals;"color&colon; rgb&lpar;0&comma; 0&comma; 255&rpar;&semi; font-size&colon; inherit&semi;">ItemAdapter&period;java<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;gridusingrecyclerview&semi;&NewLine;&NewLine;<strong>import <&sol;strong>android&period;content&period;Context&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;LayoutInflater&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;View&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;ViewGroup&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;ImageView&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;TextView&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Toast&semi;&NewLine;&NewLine;<strong>import <&sol;strong>java&period;util&period;List&semi;&NewLine;&NewLine;<strong>import <&sol;strong>androidx&period;recyclerview&period;widget&period;RecyclerView&semi;&NewLine;&NewLine;<strong>public class <&sol;strong>ItemAdapter <strong>extends <&sol;strong>RecyclerView&period;Adapter&lt&semi;ItemAdapter&period;MyViewHolder&gt&semi; &lbrace;&NewLine;&NewLine; <strong>private <&sol;strong>List&lt&semi;Item&gt&semi; <strong>list<&sol;strong>&semi;&NewLine; Context <strong>context<&sol;strong>&semi;&NewLine;&NewLine;&NewLine; <strong>public <&sol;strong>ItemAdapter&lpar;List&lt&semi;Item&gt&semi; list&comma; Context context&rpar; &lbrace;&NewLine; <strong>this<&sol;strong>&period;<strong>list <&sol;strong>&equals; list&semi;&NewLine; <strong>this<&sol;strong>&period;<strong>context <&sol;strong>&equals; context&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; <strong>public class <&sol;strong>MyViewHolder <strong>extends <&sol;strong>RecyclerView&period;ViewHolder &lbrace;&NewLine;&NewLine; <strong>public <&sol;strong>TextView <strong>textView<&sol;strong>&semi;&NewLine; <strong>public <&sol;strong>ImageView <strong>imageView<&sol;strong>&semi;&NewLine;&NewLine;&NewLine; <strong>public <&sol;strong>MyViewHolder&lpar;View view&rpar; &lbrace;&NewLine; <strong>super<&sol;strong>&lpar;view&rpar;&semi;&NewLine;&NewLine; <strong>textView <&sol;strong>&equals; view&period;findViewById&lpar;R&period;id&period;<strong><em>tvVersionName<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>imageView <&sol;strong>&equals; view&period;findViewById&lpar;R&period;id&period;<strong><em>imgVersionName<&sol;em><&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; <strong>public <&sol;strong>MyViewHolder onCreateViewHolder&lpar;ViewGroup parent&comma; <strong>int <&sol;strong>viewType&rpar; &lbrace;&NewLine; View itemView &equals; LayoutInflater&period;<em>from<&sol;em>&lpar;parent&period;getContext&lpar;&rpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>recyclerview&lowbar;item<&sol;em><&sol;strong>&comma; parent&comma; <strong>false<&sol;strong>&rpar;&semi;&NewLine; <strong>return new <&sol;strong>MyViewHolder&lpar;itemView&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onBindViewHolder&lpar;<strong>final <&sol;strong>MyViewHolder holder&comma; <strong>final int <&sol;strong>position&rpar; &lbrace;&NewLine;&NewLine; holder&period;<strong>textView<&sol;strong>&period;setText&lpar;<strong>list<&sol;strong>&period;get&lpar;position&rpar;&period;getVersionName&lpar;&rpar;&rpar;&semi;&NewLine; holder&period;<strong>imageView<&sol;strong>&period;setImageResource&lpar;<strong>list<&sol;strong>&period;get&lpar;position&rpar;&period;getImageResourseId&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; implement setOnClickListener event on item view&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> holder&period;<strong>itemView<&sol;strong>&period;setOnClickListener&lpar;<strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; Toast&period;<em>makeText<&sol;em>&lpar;<strong>context<&sol;strong>&comma; <strong>"Android Version " <&sol;strong>&plus; holder&period;<strong>textView<&sol;strong>&period;getText&lpar;&rpar; &plus; <strong>"&colon; Clicked"<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; <strong>public int <&sol;strong>getItemCount&lpar;&rpar; &lbrace;&NewLine; <strong>return list<&sol;strong>&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>Now when you run the app it will look like this&colon;&nbsp&semi; &nbsp&semi; &nbsp&semi;<&sol;strong><span style&equals;"font-size&colon; inherit&semi;"> <&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image is-style-default"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screenshot&lowbar;1601447430-1-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2260" width&equals;"467" height&equals;"830"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">RecyclerView showing Items in grid form&period;<&sol;span><&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>When you click on any recyclerView item it will show Toast message&colon; <&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screenshot&lowbar;1601447456-1-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2261" width&equals;"469" height&equals;"833"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">Show Toast message on click of Jelly Bean<&sol;span><&sol;strong>&period;<&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;

Exit mobile version