Site icon C1CTech

Android StackView Example

<h3><strong><span style&equals;"color&colon; &num;000080&semi;">What is StackView&quest;<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><strong>Honeycomb Android version<&sol;strong><&sol;span>&comma; introduced some interesting widgets with collections&period; One of them is the Android StackView&period; <span style&equals;"color&colon; &num;008000&semi;"><strong>StackView<&sol;strong><&sol;span> helps in arranging items in the form of stacked cards&comma; where the front item can be flipped to bring the item behind it to the front&period;<&sol;p>&NewLine;<p>Here&comma; you will learn to stack images with text  in the StackView&period; So create a new Android project called <span style&equals;"color&colon; &num;008000&semi;"><strong>StackView<&sol;strong><&sol;span>&period;<code><&sol;code><&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;0000ff&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <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;arunk7839&sol;StackView"><strong>Here<&sol;strong><&sol;a><&sol;span>&period;<&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"nyVTyFbTi98" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;nyVTyFbTi98" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;nyVTyFbTi98&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;nyVTyFbTi98&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<ol>&NewLine;<li>In Android Studio&comma; go to <span style&equals;"color&colon; &num;008000&semi;"><strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong><&sol;span> and fill all the details required to create a new project&period; When it prompts to select a default activity&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Blank Activity<&sol;strong><&sol;span> and proceed&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<li>&NewLine;<p>In <span style&equals;"color&colon; &num;008000&semi;"> <strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> i have add  one <span style&equals;"color&colon; &num;008000&semi;"><strong>StackView<&sol;strong><&sol;span> with following basic properties&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<p><code><&sol;code><&sol;p>&NewLine;<pre><&excl;--&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;-->&NewLine;<strong>&NewLine;<strong> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> tools&colon;context&equals;"com&period;example&period;lenovo&period;stackview&period;MainActivity"&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;TextView<&sol;strong>&NewLine;<strong> <span style&equals;"color&colon; &num;000000&semi;">android&colon;id&equals;"&commat;&plus;id&sol;textview"<&sol;span><&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;centerHorizontal&equals;"true"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;margin&equals;"10dp"<&sol;strong>&NewLine;<strong> android&colon;text&equals;"stackview"<&sol;strong>&NewLine;<strong> android&colon;textAllCaps&equals;"true"<&sol;strong>&NewLine;<strong> android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<&sol;strong>&NewLine;<strong> android&colon;textSize&equals;"25sp"<&sol;strong>&NewLine;<strong> android&colon;textStyle&equals;"bold" &sol;&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;StackView<&sol;strong>&NewLine;<strong> android&colon;id&equals;"&commat;&plus;id&sol;stackview"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;below&equals;"&commat;&plus;id&sol;textview"<&sol;strong>&NewLine;<strong> android&colon;animateLayoutChanges&equals;"true"&gt&semi;<&sol;strong>&NewLine;&NewLine; &NewLine;<&sol;strong><&sol;pre>&NewLine;<p>The value of the <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;animateLayoutChanges<&sol;strong><&sol;span> attribute is set to true so that changes occurring in the layout will not mandate running LayoutTransition&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Writing the StackAdapter Class<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>After adding the<span style&equals;"color&colon; &num;008000&semi;"> <strong>StackView widget<&sol;strong><&sol;span>&comma; let’s start writing the<span style&equals;"color&colon; &num;008000&semi;"><strong> StackAdapter <&sol;strong><&sol;span>class to render the data&period;<&sol;p>&NewLine;<p>3&period; Create a class named StackItem and add following code &colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>StackItem&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<p><code><&sol;code><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;stackview&semi;&NewLine;&NewLine;public class StackItem &lbrace;&NewLine;&NewLine; private int imageResourseId&semi;&NewLine; private String text&semi;&NewLine;&NewLine; public StackItem&lpar;int imageResourseId&comma; String text&rpar; &lbrace;&NewLine; this&period;imageResourseId &equals; imageResourseId&semi;&NewLine; this&period;text &equals; text&semi;&NewLine; &rcub;&NewLine;&NewLine; public int getImageResourseId&lpar;&rpar; &lbrace;&NewLine; return imageResourseId&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setImageResourseId&lpar;int imageResourseId&rpar; &lbrace;&NewLine; this&period;imageResourseId &equals; imageResourseId&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getText&lpar;&rpar; &lbrace;&NewLine; return text&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setText&lpar;String text&rpar; &lbrace;&NewLine; this&period;text &equals; text&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;strong><&sol;pre>&NewLine;<p>4&period;Create a layout xml named <span style&equals;"color&colon; &num;008000&semi;"><strong>stack&lowbar;item&period;xml<&sol;strong> <&sol;span>with the below code&period; This layout file shows a single StackView item&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>stack&lowbar;item&period;xml <&sol;strong> <&sol;span><&sol;p>&NewLine;<p><code><&sol;code><&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;<pre><strong>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<&sol;strong>&NewLine;<strong>&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> android&colon;orientation&equals;"vertical"&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;ImageView<&sol;strong>&NewLine;<strong> android&colon;id&equals;"&commat;&plus;id&sol;img"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"180dp"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"180dp" &sol;&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;TextView<&sol;strong>&NewLine;<strong> android&colon;id&equals;"&commat;&plus;id&sol;tv"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;blue&lowbar;dark"<&sol;strong>&NewLine;<strong> android&colon;textSize&equals;"18sp"<&sol;strong>&NewLine;<strong> android&colon;textStyle&equals;"bold" &sol;&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong>&lt&semi;&sol;LinearLayout&gt&semi;<&sol;strong><&sol;pre>&NewLine;<p>5&period;Now create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>StackAdapter&period;java<&sol;strong><&sol;span> and add the below code&period;<&sol;p>&NewLine;<p>The adapter’s methods—<span style&equals;"color&colon; &num;008000&semi;"><strong>getCount&lpar;&rpar;&comma; getItem&lpar;&rpar;&comma; and getItemId&lpar;&rpar;<&sol;strong><&sol;span>—are used to determine the number of items to be displayed and the unique identifier of the specified items&period; The<strong> <span style&equals;"color&colon; &num;008000&semi;">getView&lpar;&rpar;<&sol;span><&sol;strong> method is used to retrieve the appropriate view at the specified position&period; The view defined in the<strong> <span style&equals;"color&colon; &num;008000&semi;">stack&lowbar;item&period;xml<&sol;span> <&sol;strong>file is accessed and is used to display items through the StackView&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>StackAdapter&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<p><code><&sol;code><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;stackview&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;BaseAdapter&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;&NewLine;public class StackAdapter extends BaseAdapter &lbrace;&NewLine;&NewLine; List items&semi;&NewLine; Context context&semi;&NewLine;&NewLine; public StackAdapter&lpar;Context context&comma; List items&rpar; &lbrace;&NewLine; this&period;items &equals; items&semi;&NewLine; this&period;context &equals; context&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public long getItemId&lpar;int pos&rpar; &lbrace;&NewLine; return pos&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public StackItem getItem&lpar;int i&rpar; &lbrace;&NewLine; return items&period;get&lpar;i&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getCount&lpar;&rpar; &lbrace;&NewLine; return items&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public View getView&lpar;int position&comma; View convertView&comma; ViewGroup parent&rpar; &lbrace;&NewLine; View v &equals; convertView&semi;&NewLine; if &lpar;v &equals;&equals; null&rpar; &lbrace;&NewLine; v &equals; LayoutInflater&period;from&lpar;context&rpar;&period;inflate&lpar;R&period;layout&period;stack&lowbar;item&comma; parent&comma; false&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; StackItem item &equals; items&period;get&lpar;position&rpar;&semi;&NewLine; if &lpar;item &excl;&equals; null&rpar; &lbrace;&NewLine; ImageView imageView &equals; &lpar;ImageView&rpar; v&period;findViewById&lpar;R&period;id&period;img&rpar;&semi;&NewLine; TextView textView &equals; &lpar;TextView&rpar; v&period;findViewById&lpar;R&period;id&period;tv&rpar;&semi;&NewLine; imageView&period;setImageResource&lpar;item&period;getImageResourseId&lpar;&rpar;&rpar;&semi;&NewLine; textView&period;setText&lpar;item&period;getText&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; return v&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;strong><&sol;pre>&NewLine;<p>6&period;Open <span style&equals;"color&colon; &num;008000&semi;"><strong>drawable<&sol;strong><&sol;span> folder under res directory and paste all the  photos which you have copied from somewhere else&period;<&sol;p>&NewLine;<p>7&period;Now open  <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and do the below changes&period; Here <span style&equals;"color&colon; &num;008000&semi;"><strong>prepareStackData&lpar;&rpar;<&sol;strong> <&sol;span>method adds sample data to list &period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;stackview&semi;&NewLine;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;widget&period;StackView&semi;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; List items&semi;&NewLine; StackAdapter adapter&semi;&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; StackView sv &equals; &lpar;StackView&rpar; findViewById&lpar;R&period;id&period;stackview&rpar;&semi;&NewLine; prepareStackData&lpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;3366ff&semi;">&sol;&sol;setting adapter<&sol;span>&NewLine; adapter &equals; new StackAdapter&lpar;getApplicationContext&lpar;&rpar;&comma; items&rpar;&semi;&NewLine; sv&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;3366ff&semi;">&sol;&sol;setting data in item list<&sol;span>&NewLine; void prepareStackData&lpar;&rpar; &lbrace;&NewLine; items &equals; new ArrayList&lpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;froyo&comma; "Froyo"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;gingerbread&comma; "GingerBread"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;honeycomb&comma; "HoneyComb"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;icecream&comma; "IceCream Sandwich"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;jellybean&comma; "JellyBean"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;kitkat&comma; "KitKat"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;lollipop&comma; "Lollipop"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;marshmallow&comma; "Marshmallow"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;naugat&comma; "Naugat"&rpar;&rpar;&semi;&NewLine; items&period;add&lpar;new StackItem&lpar;R&period;drawable&period;oreo&comma; "Oreo"&rpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;strong><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Now when you run the app it will look like this&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<p><img class&equals;"alignnone size-medium wp-image-159" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-06-15-21-011-180x300&period;png" alt&equals;"" width&equals;"180" height&equals;"300" &sol;>        <img class&equals;"alignnone size-medium wp-image-160" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-06-15-21-151-180x300&period;png" alt&equals;"" width&equals;"180" height&equals;"300" &sol;>        <img class&equals;"alignnone size-medium wp-image-161" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-06-15-21-291-180x300&period;png" alt&equals;"" width&equals;"180" height&equals;"300" &sol;>&NewLine;

Exit mobile version