<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Material Design Archives - Truiton</title>
	<atom:link href="https://www.truiton.com/tag/material-design/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Technology Reaching Us In Time - Online</description>
	<lastBuildDate>Thu, 21 Nov 2019 09:04:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i0.wp.com/www.truiton.com/wp-content/uploads/2022/11/cropped-truiton_new_logo_half_white.png?fit=32%2C32&#038;ssl=1</url>
	<title>Material Design Archives - Truiton</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">54534495</site>	<item>
		<title>Android Chips Example: Material Design</title>
		<link>https://www.truiton.com/2018/10/android-chips-example-material-design/</link>
					<comments>https://www.truiton.com/2018/10/android-chips-example-material-design/#respond</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Sun, 21 Oct 2018 06:27:20 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Material Design]]></category>
		<category><![CDATA[Material Design Backward Compatibility]]></category>
		<guid isPermaLink="false">https://www.truiton.com/?p=2119</guid>

					<description><![CDATA[<p>Google&#8217;s material design has been evolving year by year. At first when Material Design was introduced a lot of new components were listed in its documentation. But their implementation or official widgets were not released at that time. But&#160;with every support library release, few new significant components are released. With this year&#8217;s Google I/O, Androidx&#8230;&#160;<a href="https://www.truiton.com/2018/10/android-chips-example-material-design/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android Chips Example: Material Design</span></a></p>
<p>The post <a href="https://www.truiton.com/2018/10/android-chips-example-material-design/">Android Chips Example: Material Design</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter is-resized"><img data-recalc-dims="1" fetchpriority="high" decoding="async" data-attachment-id="2182" data-permalink="https://www.truiton.com/2018/10/android-chips-example-material-design/featured-android_chips/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?fit=950%2C530&amp;ssl=1" data-orig-size="950,530" data-comments-opened="1" data-image-title="Android Chips Example Featured" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?fit=950%2C530&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?resize=580%2C323&#038;ssl=1" alt="Android Chips Example Featured" class="wp-image-2182" width="580" height="323" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?w=950&amp;ssl=1 950w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?resize=300%2C167&amp;ssl=1 300w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?resize=768%2C428&amp;ssl=1 768w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?resize=640%2C357&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Featured-Android_Chips.jpg?resize=600%2C335&amp;ssl=1 600w" sizes="(max-width: 580px) 100vw, 580px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Google&#8217;s material design has been evolving year by year. At first when Material Design was introduced a lot of new components were listed in its documentation. But their implementation or official widgets were not released at that time. But&nbsp;with every support library release, few new significant components are released. With this year&#8217;s Google I/O, Androidx was released as a new implementation for Android&#8217;s support library framework, along with this a new material design component library was also released. As I mentioned above a few new components were released this year too, one of them is Android Chip. This new Android chips widget is now a part of design support library v28 and has also been introduced in this new material design component library 1.0.0. This library has many components but in this article we will be discussing on Android chips only and how to make an Android chips example, via the new <a href="https://material.io/develop/android/docs/getting-started/" target="_blank" rel="noopener noreferrer">Material Component library</a>.</p>



<p class="wp-block-paragraph">Android Pill button or chips have always been a part of our apps. One way or the other building a button like a pill has never been a difficult task for us. But now that Google has&nbsp;officially released a widget by the name of <a href="https://developer.android.com/reference/com/google/android/material/chip/Chip" target="_blank" rel="noopener noreferrer">Chips</a>, it will definitely make our lives easier to some level. In this Android chips example we would learn how to make an android chip button which looks like a pill shaped interact-able element. Please note: To make use of this example, you might have to switch over to&nbsp;<code>AndroidX</code>&nbsp;and&nbsp;<code>com.google.android.material</code>&nbsp;packages by refactoring from the Refactor menu of the Android Studio, as the new material library does not work with old support packages. If you wish to use old support libraries for the time being, please use the v28 of it. It has the Chip widget.</p>



<h2 class="wp-block-heading">Exploring Material Design : Android Chips Example</h2>



<p class="wp-block-paragraph">Generally Android chips are used to capture user input, filter content, trigger actions and simply to make a selection from a choice of options. It should not be used in a situation where a single call to action has to be performed. One of the most obvious example of Android Chips, that you must have seen, should be the Gmail app. In Gmail app when an email address is entered, it is shown in an Android Chip like view. If you wish to make a UI element which looks like the gmail email address pill button type view, you should use the Android Chip widget. To start with the implementation part, first you would have to include the following dependency in your gradle file:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dependencies { 
implementation 'com.google.android.material:material:1.0.0' 
}</pre>



<p class="wp-block-paragraph"><span style="color: #23282d; font-family: Roboto; font-size: 1.4em; font-weight: inherit;">Using Android Chips with Styles</span></p>



<p class="wp-block-paragraph">In simple terms Android Chip is just a combined object of text, icon and optional close icon. With this, the new material design component also gives us a few new attributes to change its appearance and behavior. It allows us to make it&nbsp;represent an attribute, text, entity, or perform an action. This eventually allows the user to trigger an action, filter content, select a choice, or enter information. Also if you have to represent a contact with image, this Android chip can be of good use. To have a more detailed look at the chips, lets have a look at its styles.</p>



<h4 class="wp-block-heading">Action Chip (Default)</h4>



<p class="wp-block-paragraph">The most basic chip of all the available ones is the Action chip. Its a single actionable chip, in short its an alternative to the button widget. If you have to perform an action, based on users tap, this is the chip that you should use. Generally this type of chip is placed after the content of the page and is used as a clickable, just like a button. Have a look at the images below to understand its usage.</p>



<ul data-carousel-extra='{&quot;blog_id&quot;:1,&quot;permalink&quot;:&quot;https://www.truiton.com/2018/10/android-chips-example-material-design/&quot;}'  class="wp-block-gallery columns-2 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"><li class="blocks-gallery-item"><figure><a href="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-1.jpg?ssl=1"><img data-recalc-dims="1" decoding="async" width="426" height="213" data-attachment-id="2184" data-permalink="https://www.truiton.com/2018/10/android-chips-example-material-design/android-chips-example-1/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-1.jpg?fit=426%2C213&amp;ssl=1" data-orig-size="426,213" data-comments-opened="1" data-image-title="Android chips example -1" data-image-description="" data-image-caption="&lt;p&gt;Android Action Chip Example 1&lt;/p&gt;
" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-1.jpg?fit=426%2C213&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-1.jpg?resize=426%2C213&#038;ssl=1" alt="Android chips example -1" data-id="2184" class="wp-image-2184" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-1.jpg?w=426&amp;ssl=1 426w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-1.jpg?resize=300%2C150&amp;ssl=1 300w" sizes="(max-width: 426px) 100vw, 426px" /></a></figure></li><li class="blocks-gallery-item"><figure><a href="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-2.jpg?ssl=1"><img data-recalc-dims="1" decoding="async" width="426" height="213" data-attachment-id="2185" data-permalink="https://www.truiton.com/2018/10/android-chips-example-material-design/android-chips-example-2/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-2.jpg?fit=426%2C213&amp;ssl=1" data-orig-size="426,213" data-comments-opened="1" data-image-title="Android chips example -2" data-image-description="" data-image-caption="&lt;p&gt;Android Action Chip Example 2&lt;/p&gt;
" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-2.jpg?fit=426%2C213&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-2.jpg?resize=426%2C213&#038;ssl=1" alt="Android chips example -2" data-id="2185" class="wp-image-2185" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-2.jpg?w=426&amp;ssl=1 426w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-2.jpg?resize=300%2C150&amp;ssl=1 300w" sizes="(max-width: 426px) 100vw, 426px" /></a></figure></li></ul>



<p class="wp-block-paragraph">To make a working example of Android action chip, you can either define it in the XML or you can insert it dynamically by Java/Kotlin code. In this Android chips example, we will do so by XML and will use its properties in Java code. Please have a look below:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;com.google.android.material.chip.Chip
        android:id="@+id/action_chip"
        style="@style/Widget.MaterialComponents.Chip.Action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/action_chip_txt"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:padding="10dp"
        android:text="Hello World"
        app:chipIcon="@drawable/ic_android_24dp"
        app:iconStartPadding="5dp"/></pre>



<p class="wp-block-paragraph">In the above code, please see that we have defined a style tag <code>style="@style/Widget.MaterialComponents.Chip.Action"</code>. This is used to define the style for action chip, but in case if this is missing, it would still render an Android chip with &#8216;Action&#8217; style. As this is also the default style. Moving ahead, to capture this Android chip&#8217;s click event, we use this piece of code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        Chip actionChip = findViewById(R.id.action_chip);
        actionChip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "Clicked action chip");
            }
        });</pre>



<h4 class="wp-block-heading">Entry Chip (Input Chip)</h4>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="666" height="207" data-attachment-id="2193" data-permalink="https://www.truiton.com/2018/10/android-chips-example-material-design/android-chips-example-3/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-3.jpg?fit=666%2C207&amp;ssl=1" data-orig-size="666,207" data-comments-opened="1" data-image-title="Android chips example -3" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-3.jpg?fit=666%2C207&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-3.jpg?resize=666%2C207&#038;ssl=1" alt="Android Chips Example - Entry Chip" class="wp-image-2193" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-3.jpg?w=666&amp;ssl=1 666w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-3.jpg?resize=300%2C93&amp;ssl=1 300w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-3.jpg?resize=640%2C199&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-3.jpg?resize=600%2C186&amp;ssl=1 600w" sizes="auto, (max-width: 666px) 100vw, 666px" /></figure></div>



<p class="wp-block-paragraph">Another type of chip is the input one, this chip used display captured user input in a non editable format. Its a very nice way to showcase the user input. One of the most obvious usages of Android Entry chip is shown in the gmail app. Particularly the email input experience- when an email is entered by the user, it is showcased as an entry chip, sort of a pill shaped layout. Now according to the material design this element should be an Android Chip with style as &#8216;Entry&#8217;. This chip can also have a close icon along with a chip icon. Generally this type of chip is contained in a <code><code></code></code><a href="https://developer.android.com/reference/com/google/android/material/chip/ChipGroup" target="_blank" rel="noopener noreferrer">ChipGroup</a>. ChipGoup class is similar to <code><code></code></code><a href="https://developer.android.com/reference/android/widget/RadioGroup" target="_blank" rel="noopener noreferrer">RadioGroup</a> class, as it is used to hold multiple chips. It has the properties to display all the chips in a single line. Have a look at the images below to have a better understanding on how the entry chips would look:</p>



<p class="wp-block-paragraph">In case of Android entry chips, in the XML we will create an empty <code>ChipGroup</code> and add chips programmatically. Lets have a look at the layout.xml of the activity:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;com.google.android.material.chip.ChipGroup
        android:id="@+id/entry_chip_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/entry_chip_txt">

    &lt;/com.google.android.material.chip.ChipGroup></pre>



<p class="wp-block-paragraph">Now since we will be adding chips dynamically through code, a big challenge would be to maintain style along the chips. As per my understanding to maintain consistent styles there are two ways, either by code we style it and use in a loop. Or we create a standard chip style in XML and use it across. Interestingly Android Chips widget also supports a <code><code></code></code><a href="https://developer.android.com/reference/com/google/android/material/chip/ChipDrawable" target="_blank" rel="noopener noreferrer">ChipDrawable</a> class through which we can create a chip dynamically, and this <code>ChipDrawable</code> object can be created by using an XML resource. Therefore as per the principle I like this way better, have a look at my chip style which I used to inject a chip in <code>ChipGroup</code> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;chip
    style="@style/Widget.MaterialComponents.Chip.Entry"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:checkable="false"
    app:chipIcon="@drawable/ic_android_24dp"
    app:iconStartPadding="5dp"/></pre>



<p class="wp-block-paragraph">Next to create an Input Chip dynamically we need to define a method :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    private Chip getChip(final ChipGroup entryChipGroup, String text) {
        final Chip chip = new Chip(this);
        chip.setChipDrawable(ChipDrawable.createFromResource(this, R.xml.my_chip));
        int paddingDp = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 10,
                getResources().getDisplayMetrics()
        );
        chip.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);
        chip.setText(text);
        chip.setOnCloseIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                entryChipGroup.removeView(chip);
            }
        });
        return chip;
    }</pre>



<p class="wp-block-paragraph">And this is how an Android Entry Chip is inserted in the chip group:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">final ChipGroup entryChipGroup = findViewById(R.id.entry_chip_group);
final Chip entryChip = getChip(entryChipGroup, "Hello World");
final Chip entryChip2 = getChip(entryChipGroup, "Test");
entryChipGroup.addView(entryChip);
entryChipGroup.addView(entryChip2);</pre>



<h4 class="wp-block-heading">Filter Chip</h4>



<p class="wp-block-paragraph">These type of chips should be used in situations exactly as its name suggests, i.e. for creating filters. Android filter chips are similar to check boxes, in a way they should be used to filter the content. For example if you have to show a UI where there&#8217;s a list and user has to filter a specific type of content from it, in this situation a filter chip would be an apt solution. Generally filter chips do not have a icon attached to them, as a check icon is displayed once a chip is selected. Please have a look at the images below:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="462" height="195" data-attachment-id="2196" data-permalink="https://www.truiton.com/2018/10/android-chips-example-material-design/android-chips-example-4/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-4.jpg?fit=462%2C195&amp;ssl=1" data-orig-size="462,195" data-comments-opened="1" data-image-title="Android chips example -4" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-4.jpg?fit=462%2C195&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-4.jpg?resize=462%2C195&#038;ssl=1" alt="Android chips example -4" class="wp-image-2196" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-4.jpg?w=462&amp;ssl=1 462w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-4.jpg?resize=300%2C127&amp;ssl=1 300w" sizes="auto, (max-width: 462px) 100vw, 462px" /></figure></div>



<p class="wp-block-paragraph">Further lets define the XML for Android Filter Chips, they have&nbsp;<code>style="@style/Widget.MaterialComponents.Chip.Filter"</code> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;com.google.android.material.chip.ChipGroup
        android:id="@+id/filter_chip_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/filter_chip_txt">

        &lt;com.google.android.material.chip.Chip
            android:id="@+id/filter_chip"
            style="@style/Widget.MaterialComponents.Chip.Filter"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:text="Dogs"/>

        &lt;com.google.android.material.chip.Chip
            android:id="@+id/filter_chip2"
            style="@style/Widget.MaterialComponents.Chip.Filter"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Cats"/>
    &lt;/com.google.android.material.chip.ChipGroup></pre>



<p class="wp-block-paragraph">Now that we have filter chips in place, we need to detect clicks on the filter chips. We can do so simply by attaching a OnCheckedChangeListener to it:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        ChipGroup filterChipGroup = findViewById(R.id.filter_chip_group);
        Chip filterChip = findViewById(R.id.filter_chip);
        Chip filterChip2 = findViewById(R.id.filter_chip2);
        CompoundButton.OnCheckedChangeListener filterChipListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i(TAG, buttonView.getId() + "");
            }
        };
        filterChip.setOnCheckedChangeListener(filterChipListener);
        filterChip2.setOnCheckedChangeListener(filterChipListener);</pre>



<h4 class="wp-block-heading">Filter Chip &#8211; Single Selection Mode</h4>



<p class="wp-block-paragraph">In this <a href="https://www.truiton.com/2018/10/android-chips-example-material-design/">Android Chips Example</a>, I will also show a filter chip with single selection mode. In this mode, filter chips would start acting as a radio button. This means in a way only one selection can be made at a time. This functionality is achieved by adding all the filter chips in a <code>ChipGroup</code> and setting the property : <code>app:singleSelection="true"</code>&nbsp;of Chip Group. To have a better understanding, lets have a look at the layout XML file:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;com.google.android.material.chip.ChipGroup
        android:id="@+id/filter_chip_SS_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/filter_chip_ss_txt"
        app:singleSelection="true">

        &lt;com.google.android.material.chip.Chip
            android:id="@+id/filter_ss_chip"
            style="@style/Widget.MaterialComponents.Chip.Filter"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:text="Dogs"/>

        &lt;com.google.android.material.chip.Chip
            android:id="@+id/filter_ss_chip2"
            style="@style/Widget.MaterialComponents.Chip.Filter"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Cats"/>
    &lt;/com.google.android.material.chip.ChipGroup></pre>



<p class="wp-block-paragraph">Next to capture the selected chip out of all chips, we need to attach a&nbsp;<code>ChipGroup.OnCheckedChangeListener()</code> on the <code>ChipGroup</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        ChipGroup filterChipGroupSingleSelection = findViewById(R.id.filter_chip_SS_group);
        filterChipGroupSingleSelection.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(ChipGroup group, @IdRes int checkedId) {
                // Handle the checked chip change.
                Log.i(TAG, "ID: " + group.getCheckedChipId());
            }
        });</pre>



<p class="wp-block-paragraph">Please Note: This <code>ChipGroup.OnCheckedChangeListener()</code> would return callbacks only when single selection mode is set to <code>true</code>.</p>



<h4 class="wp-block-heading">Choice Chip</h4>



<p class="wp-block-paragraph">A perfect example of choice chip is similar to the filter chip one. Choice chip allows the user to select only one of the available options just like the filter chips. But with one difference, it does not have a check icon on it. Instead according to material design guidelines, it displays the selection with a change of color, when it is selected. If you wish you can use a chipIcon with this chip. But ideally they should be used wherever you feel the need to show RadioButton. Lets have a look at the UI for Android Choice Chip:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="360" height="189" data-attachment-id="2198" data-permalink="https://www.truiton.com/2018/10/android-chips-example-material-design/android-chips-example-5/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-5.jpg?fit=360%2C189&amp;ssl=1" data-orig-size="360,189" data-comments-opened="1" data-image-title="Android chips example -5" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-5.jpg?fit=360%2C189&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-5.jpg?resize=360%2C189&#038;ssl=1" alt="Android chips example -5" class="wp-image-2198" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-5.jpg?w=360&amp;ssl=1 360w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2018/10/Android-chips-example-5.jpg?resize=300%2C158&amp;ssl=1 300w" sizes="auto, (max-width: 360px) 100vw, 360px" /></figure></div>



<p class="wp-block-paragraph">To use the choice chip, we use&nbsp;<code>style="@style/Widget.MaterialComponents.Chip.Choice"</code> parameter in the XML. Lets have a look at the XML:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;com.google.android.material.chip.ChipGroup
        android:id="@+id/choice_chip_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/choice_chip_txt"
        app:singleSelection="true">

        &lt;com.google.android.material.chip.Chip
            android:id="@+id/choice_chip"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:text="Cars"/>

        &lt;com.google.android.material.chip.Chip
            android:id="@+id/choice_chip2"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Trucks"/>
    &lt;/com.google.android.material.chip.ChipGroup></pre>



<p class="wp-block-paragraph">As shown in this Android Chips Example earlier, ChipGroup can return the selected chip only when single selection mode is set to true. Even here we will utilize the same property and capture the selection:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        ChipGroup choiceChipGroup = findViewById(R.id.choice_chip_group);
        choiceChipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(ChipGroup chipGroup, @IdRes int i) {
                Log.i(TAG, i + "");
            }
        });</pre>



<p class="wp-block-paragraph">If you wish to have a look at all the working sample of the Android Chip types mentioned above, please have a look at the full source code here:&nbsp;</p>



<p class="wp-block-paragraph" style="text-align:center"><a class="fasc-button fasc-size-large fasc-type-flat fasc-rounded-medium ico-fa fasc-ico-before fa-github" style="background-color: #0364af; color: #ffffff;" target="_blank" rel="noopener noreferrer" href="https://github.com/Truiton/ChipsExample">Full Source Code</a>&nbsp;</p>



<p class="wp-block-paragraph">In this Android Chips Example, as you can see we built a working sample of all the chip styles, by using the latest Google&#8217;s material library. Here in this tutorial we also showcased, that a chip has the properties of a radio button as well as a check box. In a way its a better representation of both.&nbsp;You can use it either in a single selection or in multiple selection mode. Both ways it would give a great experience.&nbsp;As the Android Chips are compact, relevant and focused. Hope this example made you understand things better, for latest news on Android connect with us on Twitter, Facebook and Google+. Also subscribe to our newsletter, for news right in your inbox.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2018/10/android-chips-example-material-design/">Android Chips Example: Material Design</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2018/10/android-chips-example-material-design/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2119</post-id>	</item>
		<item>
		<title>Android Bottom Navigation Bar Example</title>
		<link>https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/</link>
					<comments>https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Sat, 28 Jan 2017 14:23:50 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[app design guidelines]]></category>
		<category><![CDATA[Design Support Library]]></category>
		<category><![CDATA[Material Design]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=1666</guid>

					<description><![CDATA[<p>Its been a while since Google introduced Material Design, but recently with Design Support Library v25&#160;they released a new widget to make an Android Bottom Navigation Bar, as described in the official material design specs. This new navigation component is called the&#160;BottomNavigationView and as mentioned, its packaged under the design support library, having full backward&#8230;&#160;<a href="https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android Bottom Navigation Bar Example</span></a></p>
<p>The post <a href="https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/">Android Bottom Navigation Bar Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="950" height="530" data-attachment-id="1671" data-permalink="https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/android-bottom-navigation-bar-featured/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-Bottom-Navigation-Bar-Featured.jpg?fit=950%2C530&amp;ssl=1" data-orig-size="950,530" data-comments-opened="1" data-image-title="Android Bottom Navigation Bar &amp;#8211; Featured" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-Bottom-Navigation-Bar-Featured.jpg?fit=950%2C530&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-Bottom-Navigation-Bar-Featured.jpg?resize=950%2C530" alt="Android Bottom Navigation Bar" class="wp-image-1671" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-Bottom-Navigation-Bar-Featured.jpg?w=950&amp;ssl=1 950w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-Bottom-Navigation-Bar-Featured.jpg?resize=600%2C335&amp;ssl=1 600w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-Bottom-Navigation-Bar-Featured.jpg?resize=300%2C167&amp;ssl=1 300w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-Bottom-Navigation-Bar-Featured.jpg?resize=768%2C428&amp;ssl=1 768w" sizes="auto, (max-width: 950px) 100vw, 950px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Its been a while since Google introduced <a href="http://www.truiton.com/2015/02/android-material-design-backward-compatibility/" target="_blank" rel="noopener noreferrer">Material Design</a>, but recently with <a href="https://developer.android.com/topic/libraries/support-library/revisions.html#rev25-0-0" target="_blank" rel="noopener noreferrer">Design Support Library v25</a>&nbsp;they released a new widget to make an Android Bottom Navigation Bar, as described in the <a href="https://material.io/guidelines/components/bottom-navigation.html" target="_blank" rel="noopener noreferrer">official material design</a> specs. This new navigation component is called the&nbsp;<a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html" target="_blank" rel="noopener noreferrer">BottomNavigationView</a> and as mentioned, its packaged under the design support library, having full backward support. Although if you browse the internet, you may find many examples, showing how to implement an Android bottom navigation bar. But this new Android BottomNavigationView widget makes it very easy for the developers to build and deploy apps with bottom navigation, as its an&nbsp;official component.</p>



<h2 class="wp-block-heading">Android BottomNavigationView Widget</h2>



<p class="wp-block-paragraph">At first when you look at the new BottomNavigationView widget it may look similar to the <code>TabLayout</code> of the same library. But but do not get confused, as its implementation is a lot different than that of the&nbsp;later one. As the new bottom bar navigation implementation of Android is based out of&nbsp;menu items. Hence to populate a <code>BottomNavigationView</code> you need to specify a menu resource file, unlike in the <code>TabLayout</code>. Although if you look at the final output i.e. the screen, there is still a striking similarity between the two. But putting this discussion to rest, lets have a look at the new features of the Android BottomNavigationView.</p>



<p class="wp-block-paragraph">Whenever a new component is introduced, along with it some new methods and attributes are also added with it. Hence with the Android&#8217;s official bottom navigation widget the&nbsp;<a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html" target="_blank" rel="noopener noreferrer">BottomNavigationView</a>&nbsp;following new XML attributes are added:</p>



<ul class="wp-block-list"><li><code><code></code></code><a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html#attr_android.support.design:itemBackground" target="_blank" rel="noopener noreferrer">android.support.design:itemBackground</a>&nbsp;&#8211; It is used to set the background color of the navigation item. It does not take a <a href="https://developer.android.com/reference/android/content/res/ColorStateList.html" target="_blank" rel="noopener noreferrer">ColorStateList</a>, as per design specifications.</li><li><code><code></code></code><a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html#attr_android.support.design:itemIconTint" target="_blank" rel="noopener noreferrer">android.support.design:itemIconTint</a>&nbsp;&#8211; It is used to set the icon tint color, use a&nbsp;<a href="https://developer.android.com/reference/android/content/res/ColorStateList.html" target="_blank" rel="noopener noreferrer">ColorStateList</a>&nbsp;to specify colors for all states.</li><li><code><code></code></code><a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html#attr_android.support.design:itemTextColor" target="_blank" rel="noopener noreferrer">android.support.design:itemTextColor</a>&nbsp;&#8211; It is used to set the text color of the navigation item,&nbsp;use a&nbsp;<a href="https://developer.android.com/reference/android/content/res/ColorStateList.html" target="_blank" rel="noopener noreferrer">ColorStateList</a>&nbsp;to specify colors for all states.</li></ul>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="600" height="120" data-attachment-id="1673" data-permalink="https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/android-bottom-navigation-bar-1/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/android-bottom-navigation-bar-1.jpg?fit=600%2C120&amp;ssl=1" data-orig-size="600,120" data-comments-opened="1" data-image-title="android-bottom-navigation-bar-1" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/android-bottom-navigation-bar-1.jpg?fit=600%2C120&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/android-bottom-navigation-bar-1.jpg?resize=600%2C120" alt="android bottom navigation bar" class="wp-image-1673" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/android-bottom-navigation-bar-1.jpg?w=600&amp;ssl=1 600w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/android-bottom-navigation-bar-1.jpg?resize=300%2C60&amp;ssl=1 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></figure></div>



<p class="wp-block-paragraph">As I mentioned&nbsp;<code>BottomNavigationView</code> takes a <a href="https://developer.android.com/guide/topics/resources/menu-resource.html" target="_blank" rel="noopener noreferrer">menu resource</a> file to inflate the navigation items. To do so following are the ways in which it can be done:</p>



<ul class="wp-block-list"><li>XML
<ul>
<li><a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html#inflateMenu(int)" target="_blank" rel="noopener noreferrer"><code>app:menu</code></a></li>
</ul>
</li><li>Java
<ul>
<li><code><code></code></code><a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html#inflateMenu(int)" target="_blank" rel="noopener noreferrer">BottomNavigationView.inflateMenu()</a></li>
</ul>
</li></ul>



<p class="wp-block-paragraph">Once a <code>BottomNavigationView</code> is inflated and visible, a click listener can be attached to it:</p>



<ul class="wp-block-list"><li><code><code></code></code><a href="https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.OnNavigationItemSelectedListener.html" target="_blank" rel="noopener noreferrer">BottomNavigationView.OnNavigationItemSelectedListener</a></li></ul>



<p class="wp-block-paragraph">Method used to select a specific item in the BottomNavigationView:</p>



<ul class="wp-block-list"><li><code>BottomNavigationView.getMenu().getItem(index).setChecked(true);</code></li></ul>



<p class="wp-block-paragraph">Although this component gives us the&nbsp;flexibility of changing screen content dynamically. But it is advised to not implement <a href="https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-behavior" target="_blank" rel="noopener noreferrer">swiping views</a>&nbsp;with an Android bottom navigation bar as per specs. Therefore&nbsp;this rules out the possibility of using a ViewPager with the bottom navigation bar. Instead a simple view or a set of fragments for complex implementation can be used. Therefore here in this tutorial, I will show how to implement a BottomNavigationView with a set of fragments.&nbsp;Lets have look at the example below.</p>



<h2 class="wp-block-heading">Android Bottom Navigation Bar Example</h2>



<p class="wp-block-paragraph">To demonstrate a working example of Android BottomNavigationView widget, I would make three fragments and switch between them by using the bottom navigation bar.&nbsp;Here are the steps:</p>



<h3 class="wp-block-heading">Step 1 : Add the dependencies</h3>



<p class="wp-block-paragraph">To start off with an example of bottom navigation bar on android , first we need to include following dependencies in our build.gradle file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:support-v4:25.1.0'</pre>



<h3 class="wp-block-heading">&nbsp;</h3>



<h3 class="wp-block-heading">Step 2 : Define the Fragments</h3>



<p class="wp-block-paragraph">For this example I&nbsp;will be building a screen with three fragments, which could be switched by the BottomNavigationView. Hence before jumping on to the main screen, lets define three fragments.</p>



<p class="wp-block-paragraph">Fragment 1:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.truiton.bottomnavigation.ItemOneFragment">

    &lt;ImageView
        android:id="@+id/truiton_image"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:src="@mipmap/truiton"/>

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/truiton_image"
        android:layout_centerHorizontal="true"
        android:text="Fragment 1"
        android:textSize="30sp"/>

&lt;/RelativeLayout>
</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.bottomnavigation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ItemOneFragment extends Fragment {
    public static ItemOneFragment newInstance() {
        ItemOneFragment fragment = new ItemOneFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_item_one, container, false);
    }
}
</pre>



<p class="wp-block-paragraph">Fragment 2:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.truiton.bottomnavigation.ItemTwoFragment">

    &lt;ImageView
        android:id="@+id/truiton_image"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:src="@mipmap/truiton"/>

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/truiton_image"
        android:layout_centerHorizontal="true"
        android:text="Fragment 2"
        android:textSize="30sp"/>

&lt;/RelativeLayout>
</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.bottomnavigation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ItemTwoFragment extends Fragment {
    public static ItemTwoFragment newInstance() {
        ItemTwoFragment fragment = new ItemTwoFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_item_two, container, false);
    }
}
</pre>



<p class="wp-block-paragraph">Fragment 3:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.truiton.bottomnavigation.ItemThreeFragment">

    &lt;ImageView
        android:id="@+id/truiton_image"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:src="@mipmap/truiton"/>

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/truiton_image"
        android:layout_centerHorizontal="true"
        android:text="Fragment 3"
        android:textSize="30sp"/>

&lt;/RelativeLayout>
</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.bottomnavigation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ItemThreeFragment extends Fragment {
    public static ItemThreeFragment newInstance() {
        ItemThreeFragment fragment = new ItemThreeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_item_three, container, false);
    }
}
</pre>



<h3 class="wp-block-heading">Step 3 : Defining the bottom navigation menu XML</h3>



<p class="wp-block-paragraph">As mentioned before to populate the actual items into an&nbsp;Android bottom navigation bar, a menu type XML is used. This XML should contain at minimum three attributes:</p>



<ul class="wp-block-list"><li>android:title</li><li>android:icon</li><li>android:id</li></ul>



<p class="wp-block-paragraph">For this example of BottomNavigationView, I would make a menu of three items:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;menu xmlns:android="http://schemas.android.com/apk/res/android">
    &lt;item
        android:id="@+id/action_item1"
        android:icon="@drawable/ic_account_box_black_24dp"
        android:title="@string/item_1"/>
    &lt;item
        android:id="@+id/action_item2"
        android:icon="@drawable/ic_account_circle_black_24dp"
        android:title="@string/item_2"/>
    &lt;item
        android:id="@+id/action_item3"
        android:icon="@drawable/ic_assignment_ind_black_24dp"
        android:title="@string/item_3"/>
&lt;/menu></pre>



<h3 class="wp-block-heading">Step 4 : Defining the Activity XML</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.truiton.bottomnavigation.MainActivity">

    &lt;FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:animateLayoutChanges="true">

    &lt;/FrameLayout>

    &lt;android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/nav_item_state_list"
        app:itemTextColor="@color/nav_item_state_list"
        app:menu="@menu/bottom_navigation_items"/>
&lt;/RelativeLayout>
</pre>



<p class="wp-block-paragraph">As you can see above, for attributes&nbsp;<code>app:itemIconTint</code> and <code>app:itemTextColor</code>, I have used a&nbsp;<a href="https://developer.android.com/reference/android/content/res/ColorStateList.html" target="_blank" rel="noopener noreferrer">ColorStateList</a>. Please make sure when you make a color state list, its according to the <a href="https://material.io/guidelines/components/bottom-navigation.html" target="_blank" rel="noopener noreferrer">Material design specs</a> for bottom navigation bar. Full source code is available at the end of the tutorial.</p>



<h3 class="wp-block-heading">&nbsp;</h3>



<h3 class="wp-block-heading">Step 5 : Defining the activity with bottom navigation bar</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.bottomnavigation;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNavigationView = (BottomNavigationView)
                findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.action_item1:
                                selectedFragment = ItemOneFragment.newInstance();
                                break;
                            case R.id.action_item2:
                                selectedFragment = ItemTwoFragment.newInstance();
                                break;
                            case R.id.action_item3:
                                selectedFragment = ItemThreeFragment.newInstance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

        //Manually displaying the first fragment - one time only
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, ItemOneFragment.newInstance());
        transaction.commit();

        //Used to select an item programmatically
        //bottomNavigationView.getMenu().getItem(2).setChecked(true);
    }
}
</pre>



<p class="wp-block-paragraph">As you can see above when making a screen with the help of BottomNavigationView we have to initialize it with a default view. As there is no <code>ViewPager</code> here, we are doing it all manually. By default when a <code>BottomNavigationView</code> is initialized it is loaded with the first item selected. But&nbsp;if you wish to load the screen with any other item selected, you can do so by getting the menu item and setting it checked as shown above. For full source code, please refer to the following link:</p>



<p class="wp-block-paragraph" style="text-align:center"><a class="fasc-button fasc-size-large fasc-type-flat fasc-rounded-medium ico-fa fasc-ico-before fa-github" style="background-color: #0364af; color: #ffffff;" target="_blank" rel="noopener noreferrer" href="https://github.com/Truiton/BottomNavigation">Full Source Code</a></p>



<p class="wp-block-paragraph">The final output would look like this:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="600" height="967" data-attachment-id="1676" data-permalink="https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/android-bottom-navigation-bar-2/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-bottom-navigation-bar-2.png?fit=600%2C967&amp;ssl=1" data-orig-size="600,967" data-comments-opened="1" data-image-title="Android bottom navigation bar -2" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-bottom-navigation-bar-2.png?fit=600%2C967&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-bottom-navigation-bar-2.png?resize=600%2C967" alt="Android bottom navigation bar" class="wp-image-1676" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-bottom-navigation-bar-2.png?w=600&amp;ssl=1 600w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-bottom-navigation-bar-2.png?resize=186%2C300&amp;ssl=1 186w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2017/01/Android-bottom-navigation-bar-2.png?resize=300%2C484&amp;ssl=1 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></figure></div>



<h2 class="wp-block-heading">BottomNavigationView Limitations</h2>



<p class="wp-block-paragraph">Although the view itself is recently released, hence it is bound to have some issues. But if you are trying to build an Android screen with bottom navigation, here is a list of&nbsp;issues you might face:</p>



<ul class="wp-block-list"><li>Item Background cannot be colored for selected state.</li><li>It does not have a bottom navigation behavior, hence no integration is available for FAB, Snackbar, and CoordinatorLayout.</li><li>Ethically swiping of views should not be done &#8211; Stated in <a href="https://material.io/guidelines/components/bottom-navigation.html" target="_blank" rel="noopener noreferrer">Material Design guidelines</a>.</li><li>Text and icon sizes cannot be changed.</li><li>No way to hide title for non selected items.</li><li>Only 5 items can be added at a time.</li></ul>



<p class="wp-block-paragraph">But besides all this, building an Android bottom navigation bar has become a lot more simpler with the new BottomNavigationBar widget. As this is the new official component for bottom navigation.&nbsp;Also when speaking of shaping a product, giving upfront options like the bottom navigation has always gotten more traction,&nbsp;since it presents all the options to a user right away. Hope this helped, follow us on Twitter, Facebook and Google+ for more updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/">Android Bottom Navigation Bar Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/feed/</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1666</post-id>	</item>
		<item>
		<title>Android Bottom Sheet Example</title>
		<link>https://www.truiton.com/2016/07/android-bottom-sheet-example/</link>
					<comments>https://www.truiton.com/2016/07/android-bottom-sheet-example/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Sun, 17 Jul 2016 17:22:29 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Material Design]]></category>
		<category><![CDATA[Material Design Backward Compatibility]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=1535</guid>

					<description><![CDATA[<p>When in 2014, Google introduced Material Design, they introduced many new design components, like Floating Action button, Tabs, Cards, and Bottom Sheets for Android. Although most of the new components were not available&#160;for&#160;Android&#160;at that moment. But over time they released a support library by the name of Design Support Library. Which thankfully helped the developers&#160;to&#8230;&#160;<a href="https://www.truiton.com/2016/07/android-bottom-sheet-example/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android Bottom Sheet Example</span></a></p>
<p>The post <a href="https://www.truiton.com/2016/07/android-bottom-sheet-example/">Android Bottom Sheet Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="950" height="530" data-attachment-id="1536" data-permalink="https://www.truiton.com/2016/07/android-bottom-sheet-example/android-bottom-sheet-featured/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Featured.jpg?fit=950%2C530&amp;ssl=1" data-orig-size="950,530" data-comments-opened="1" data-image-title="Android Bottom Sheet &amp;#8211; Featured" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Featured.jpg?fit=950%2C530&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Featured.jpg?resize=950%2C530" alt="Android Bottom Sheet - Featured" class="wp-image-1536" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Featured.jpg?w=950&amp;ssl=1 950w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Featured.jpg?resize=600%2C335&amp;ssl=1 600w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Featured.jpg?resize=300%2C167&amp;ssl=1 300w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Featured.jpg?resize=768%2C428&amp;ssl=1 768w" sizes="auto, (max-width: 950px) 100vw, 950px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">When in 2014, Google introduced <a href="https://material.google.com/" target="_blank" rel="noopener noreferrer">Material Design</a>, they introduced many new design components, like <a href="http://www.truiton.com/2015/04/floating-action-buttons-android/" target="_blank" rel="noopener noreferrer">Floating Action button</a>, <a href="http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/" target="_blank" rel="noopener noreferrer">Tabs</a>, <a href="http://www.truiton.com/2015/03/android-cardview-example/" target="_blank" rel="noopener noreferrer">Cards</a>, and <a href="https://material.google.com/components/bottom-sheets.html" target="_blank" rel="noopener noreferrer">Bottom Sheets</a> for Android. Although most of the new components were not available&nbsp;for&nbsp;Android&nbsp;at that moment. But over time they released a support library by the name of <a href="http://android-developers.blogspot.in/2015/05/android-design-support-library.html" target="_blank" rel="noopener noreferrer">Design Support Library</a>. Which thankfully helped the developers&nbsp;to utilize the power of material design on Android. Recently a new design component was introduced in this support library called the Bottom Sheet. Here in this Android Bottom Sheet example, we would make a short example using the new component to understand its working.&nbsp;But first lets understand what is an Android Bottom Sheet.</p>



<p class="wp-block-paragraph">According to material design, <a href="https://material.google.com/components/bottom-sheets.html" target="_blank" rel="noopener noreferrer">Bottom Sheets</a> can be of two types: <strong>Persistent</strong> and <strong>Modal</strong>. Here in this example we would make samples for both types of Android Bottom sheets. As per documentation persistent bottom sheets are supposed to be in-app content.&nbsp;Hence they are included right inside the main layout. In a way they are supposed to be integrated with the app or screen. On the other hand Modal Bottom sheets are a little different; they are supposed to be used as the layer on top of your app. Mostly they are inferred as an external entity, like a menu. Also since they are like menus, therefore&nbsp;if they are visible to the user, they do not allow the user to interact with main screen in background. First they need to dismiss&nbsp;the Android Bottom Sheet,&nbsp;only then the user would be allowed to interact with main activity. Lets learn more about them in the next sections, also full source code is available for this example at the bottom of this write-up. But first,&nbsp;lets&nbsp;please include these dependencies in our&nbsp;build.gradle file:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
}</pre>



<h2 class="wp-block-heading">Android Bottom Sheet : Persistent</h2>



<p class="wp-block-paragraph">To make some content beautifully slide in and out of a screen, we can use the persistent Bottom Sheets on Android.&nbsp;To make a view as persistent Bottom Sheet in your screen all you need to do is have <code>CoordinatorLayout</code> at the top level of your layout. Next a behavior&nbsp;should be specified in the layout you need to make a Bottom Sheet. To understand better lets define the layout:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.truiton.bottomsheet_persistent.MainActivity">

    &lt;android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        &lt;RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

            &lt;ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/truiton"/>

            &lt;Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/image"
                android:layout_centerHorizontal="true"
                android:text="Show Bottom Sheet"/>
        &lt;/RelativeLayout>
    &lt;/android.support.v4.widget.NestedScrollView>

    &lt;RelativeLayout
        android:id="@+id/design_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:behavior_hideable="true"
        app:behavior_peekHeight="50dp"
        app:elevation="4dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        &lt;TextView
            android:id="@+id/bottomsheet_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Introducing Bottom Sheets"
            android:textColor="#FFFFFF"/>

    &lt;/RelativeLayout>


&lt;/android.support.design.widget.CoordinatorLayout>
</pre>



<p class="wp-block-paragraph">This would result in a screen with Android Bottom sheet like this:</p>



<ul data-carousel-extra='{&quot;blog_id&quot;:1,&quot;permalink&quot;:&quot;https://www.truiton.com/2016/07/android-bottom-sheet-example/&quot;}'  class="wp-block-gallery columns-3 is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex"><li class="blocks-gallery-item"><figure><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="893" data-attachment-id="1539" data-permalink="https://www.truiton.com/2016/07/android-bottom-sheet-example/android-bottom-sheet-persistent-1/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-1.png?fit=500%2C893&amp;ssl=1" data-orig-size="500,893" data-comments-opened="1" data-image-title="Android Bottom Sheet Persistent -1" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-1.png?fit=500%2C893&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-1.png?resize=500%2C893&#038;ssl=1" alt="Android Bottom Sheet Persistent -1" data-id="1539" class="wp-image-1539" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-1.png?w=500&amp;ssl=1 500w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-1.png?resize=168%2C300&amp;ssl=1 168w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-1.png?resize=300%2C536&amp;ssl=1 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></figure></li><li class="blocks-gallery-item"><figure><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="893" data-attachment-id="1540" data-permalink="https://www.truiton.com/2016/07/android-bottom-sheet-example/android-bottom-sheet-persistent-2/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-2.png?fit=500%2C893&amp;ssl=1" data-orig-size="500,893" data-comments-opened="1" data-image-title="Android Bottom Sheet Persistent -2" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-2.png?fit=500%2C893&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-2.png?resize=500%2C893&#038;ssl=1" alt="Android Bottom Sheet Persistent -2" data-id="1540" class="wp-image-1540" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-2.png?w=500&amp;ssl=1 500w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-2.png?resize=168%2C300&amp;ssl=1 168w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Persistent-2.png?resize=300%2C536&amp;ssl=1 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></figure></li></ul>



<p class="wp-block-paragraph">One of the great parts about persistent Bottom Sheets is that, they can be dragged to go full screen, even if they don&#8217;t have the content for a full page. On the other hand Modal Bottom Sheets does not allow this.</p>



<p class="wp-block-paragraph">Also if you closely observe&nbsp;the Android bottom sheet shown above, you may see that it stays below the Action bar. If you wish to make it go over the action bar. Please change the activity theme to <code>Theme.AppCompat.Light.NoActionBar</code> and add the elevation of&nbsp;<code>4dp</code>&nbsp;in Bottom Sheet. It would start sliding right over the Toolbar (if added). Next lets have a look at the java file for this screen.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.bottomsheet_persistent;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View bottomSheet = findViewById(R.id.design_bottom_sheet);
        final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                    case BottomSheetBehavior.STATE_DRAGGING:
                        Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_DRAGGING");
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_SETTLING");
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED:
                        Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_EXPANDED");
                        break;
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_COLLAPSED");
                        break;
                    case BottomSheetBehavior.STATE_HIDDEN:
                        Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_HIDDEN");
                        break;
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.i("BottomSheetCallback", "slideOffset: " + slideOffset);
            }
        });

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                } else {
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }
        });
    }
}
</pre>



<p class="wp-block-paragraph">In the above piece of code, you may see that I have added a <code>BottomSheetBehavior</code> callback interface, to listen to the changes in Bottom Sheet states. Although in the above code its not serving any purpose, but its added for reference. Full source code is available here:</p>



<p class="wp-block-paragraph" style="text-align:center"><a class="fasc-button fasc-size-large fasc-type-flat fasc-rounded-medium ico-fa fasc-ico-before fa-github" style="background-color: #0364af; color: #ffffff;" target="_blank" rel="noopener noreferrer" href="https://github.com/Truiton/BottomSheetBehavior">Source Code</a></p>



<h2 class="wp-block-heading">Android Bottom Sheet :&nbsp;Modal</h2>



<p class="wp-block-paragraph">Second&nbsp;type of Bottom Sheet is the modal sheet. According to the documentation this component should act as a menu or&nbsp;a sheet which does not allow the users to interact with main screen until it&nbsp;is dismissed.&nbsp;Surprisingly when speaking of Android, this design pattern&nbsp;is similar to a dialog box. Hence a new class <code>BottomSheetDialogFragment</code> was introduced; extending the <code>AppCompatDialogFragment</code>. Which resulted in a perfect modal type Android Bottom Sheet.</p>



<p class="wp-block-paragraph">Now technically speaking; when we talk of modal type Android bottom sheets, we have to create an XML outside of the activity separately to inflate in the&nbsp;<code>BottomSheetDialogFragment.</code> To start off lets have a look at the XMLs first:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/colorAccent"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin">

    &lt;TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Introducing Modal Bottom Sheet"
        android:textColor="#FFFFFF"/>

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        android:textColor="#FFFFFF"/>

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        android:textColor="#FFFFFF"/>

&lt;/LinearLayout></pre>



<p class="wp-block-paragraph">Also have a look at the XML for MainActivity:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.truiton.bottomsheet_modal.MainActivity">

    &lt;ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/truiton"/>

    &lt;Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image"
        android:layout_centerHorizontal="true"
        android:text="Show Modal Bottom Sheet"/>
&lt;/RelativeLayout>
</pre>



<p class="wp-block-paragraph">Now that we have XMLs in place. Lets have a look at some code for the MainActivity:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.bottomsheet_modal;

import android.os.Bundle;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final BottomSheetDialogFragment myBottomSheet = MyBottomSheetDialogFragment.newInstance("Modal Bottom Sheet");

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myBottomSheet.show(getSupportFragmentManager(), myBottomSheet.getTag());
            }
        });
    }
}
</pre>



<p class="wp-block-paragraph">Over here you may see that, to actually show a&nbsp;Bottom Sheet&nbsp;we&nbsp;are using an instance of the&nbsp;<code>BottomSheetDialogFragment</code>. Therefore lets have a look at the class for bottom sheet.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.bottomsheet_modal;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by MG on 17-07-2016.
 */
public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {

    String mString;

    static MyBottomSheetDialogFragment newInstance(String string) {
        MyBottomSheetDialogFragment f = new MyBottomSheetDialogFragment();
        Bundle args = new Bundle();
        args.putString("string", string);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mString = getArguments().getString("string");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_sheet_modal, container, false);
        TextView tv = (TextView) v.findViewById(R.id.text);
        return v;
    }
}
</pre>



<p class="wp-block-paragraph">As you can see, the working of above class is just like any <code>DialogFragment</code> in Android. Hence&nbsp;we inflated the layout in <code>onCreateView()</code> method, which made the Bottom Sheet to look like:</p>



<ul data-carousel-extra='{&quot;blog_id&quot;:1,&quot;permalink&quot;:&quot;https://www.truiton.com/2016/07/android-bottom-sheet-example/&quot;}'  class="wp-block-gallery columns-3 is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex"><li class="blocks-gallery-item"><figure><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="893" data-attachment-id="1543" data-permalink="https://www.truiton.com/2016/07/android-bottom-sheet-example/android-bottom-sheet-modal-2/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-2.png?fit=500%2C893&amp;ssl=1" data-orig-size="500,893" data-comments-opened="1" data-image-title="Android Bottom Sheet Modal -2" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-2.png?fit=500%2C893&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-2.png?resize=500%2C893&#038;ssl=1" alt="Android Bottom Sheet Modal -2" data-id="1543" class="wp-image-1543" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-2.png?w=500&amp;ssl=1 500w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-2.png?resize=168%2C300&amp;ssl=1 168w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-2.png?resize=300%2C536&amp;ssl=1 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></figure></li><li class="blocks-gallery-item"><figure><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="893" data-attachment-id="1544" data-permalink="https://www.truiton.com/2016/07/android-bottom-sheet-example/android-bottom-sheet-modal-1/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-1.png?fit=500%2C893&amp;ssl=1" data-orig-size="500,893" data-comments-opened="1" data-image-title="Android Bottom Sheet Modal -1" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-1.png?fit=500%2C893&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-1.png?resize=500%2C893&#038;ssl=1" alt="Android Bottom Sheet Modal -1" data-id="1544" class="wp-image-1544" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-1.png?w=500&amp;ssl=1 500w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-1.png?resize=168%2C300&amp;ssl=1 168w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2016/07/Android-Bottom-Sheet-Modal-1.png?resize=300%2C536&amp;ssl=1 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></figure></li></ul>



<p class="wp-block-paragraph">Another interesting thing about the modal type android bottom sheets is that, they render themselves on top of the view hierarchy making the background a little dull. Also this type of Bottom sheet automatically covers the action bar or toolbar, as its on top of the view hierarchy. Another thing that distinguishes between the modal and persistent Bottom sheets is that; modal sheets expand until the content is visible, once whole content is visible they don&#8217;t expand any further. Unlike the persistent type Android bottom sheet which cover the screen regardless of content. To view the full source code, please refer to the link below:</p>



<p class="wp-block-paragraph" style="text-align:center"><a class="fasc-button fasc-size-large fasc-type-flat fasc-rounded-medium ico-fa fasc-ico-before fa-github" style="background-color: #0364af; color: #ffffff;" target="_blank" rel="noopener noreferrer" href="https://github.com/Truiton/BottomSheetBehavior">Source Code</a></p>



<p class="wp-block-paragraph">Many apps are starting to use this component; as it has the inbuilt sliding feature, which&nbsp;adds on to a&nbsp;good user experience. In a way the new <a href="http://www.truiton.com/2016/07/android-bottom-sheet-example/">bottom sheet design component</a> could turn out to be very useful in an Android app. As it gives a new hidden space within the screen, which the user can relate to and slide out at any time. Also&nbsp;since now it has been fully integrated in to the Android platform, I recommend all of the apps should use this component to make apps more interactive and more connected to the user. Hope this helps, connect with us on Twitter, Facebook and Google+ for more updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2016/07/android-bottom-sheet-example/">Android Bottom Sheet Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2016/07/android-bottom-sheet-example/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1535</post-id>	</item>
		<item>
		<title>Android Material Design Buttons &#8211; Best Approach</title>
		<link>https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/</link>
					<comments>https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Thu, 09 Apr 2015 12:38:54 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Material Design]]></category>
		<category><![CDATA[Material Design Backward Compatibility]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=1065</guid>

					<description><![CDATA[<p>One of the most basic design component in any Android app is a button. Google released a new design pattern lately by the name of Material Design. With material design, Google released new standards for buttons. These buttons have a unique feature that they show a ripple when touched. When speaking of Android material design&#8230;&#160;<a href="https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android Material Design Buttons &#8211; Best Approach</span></a></p>
<p>The post <a href="https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/">Android Material Design Buttons &#8211; Best Approach</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="950" height="534" data-attachment-id="1066" data-permalink="https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/android-material-design-button-featured/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-Featured.jpg?fit=950%2C534&amp;ssl=1" data-orig-size="950,534" data-comments-opened="1" data-image-title="Android Material Design Button &amp;#8211; Featured" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-Featured.jpg?fit=950%2C534&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-Featured.jpg?resize=950%2C534" alt="Android Material Design Button" class="wp-image-1066" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-Featured.jpg?w=950&amp;ssl=1 950w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-Featured.jpg?resize=300%2C169&amp;ssl=1 300w" sizes="auto, (max-width: 950px) 100vw, 950px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">One of the most basic design component in any Android app is a button. Google released a new design pattern lately by the name of <a href="http://www.google.co.in/design/spec/material-design/introduction.html" target="_blank" rel="noopener noreferrer">Material Design</a>. With material design, Google released new standards for buttons. These buttons have a unique feature that they show a ripple when touched. When speaking of Android material design buttons, they are broadly categorized into four types:</p>



<ol class="wp-block-list"><li>Raised Button &#8211; A normal button</li><li>Flat Button &#8211; A button for cards, toolbars and dialogs</li><li><a title="Floating Action Buttons In Android" href="http://www.truiton.com/2015/04/floating-action-buttons-android/" target="_blank" rel="noopener noreferrer">Floating Button</a> &#8211; A hovering button for most important or promoted action</li><li>Other Buttons &#8211; Icons, drop downs etc.</li></ol>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Here we will discuss how to make raised and flat buttons according to the material design specifications for Android, with full backward support. Google has very clearly defined, how a material design button should look and feel when touched. But one of the most basic problem still prevails. How should a button look in the earlier versions of Android?</p>



<p class="wp-block-paragraph">As of now, if you create a normal button with <code>Button</code> xml tag, it would look like a material design button in Android 5.0 and above. But before that it would look, as it should on the respective version. Another point that I would like to make is here is that, with android support v7 AppCompat library, you can support material theme to an extent. But sadly even with support library, as of now you may not be able to show Android material design buttons on all versions of android.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android Material Design Raised Button</h2>



<h3 class="wp-block-heading">1) Standard Button</h3>



<p class="wp-block-paragraph">Use the code below to make a raised button:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:text="Normal Button"/></pre>



<p class="wp-block-paragraph">First button in the screenshot below shows this button.</p>



<h3 class="wp-block-heading">2) Material design raised button with CardView</h3>



<p class="wp-block-paragraph">If you want to make a button, which looks like the material design button, with backward support. Try using the CardView library for it. Have a look at the code below:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardBackgroundColor="#d9d9d9"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="1dp">

        &lt;RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            &lt;TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:minWidth="88dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:singleLine="true"
                android:text="CARDVIEW BUTTON"
                android:textColor="#000000"/>
        &lt;/RelativeLayout>
    &lt;/android.support.v7.widget.CardView></pre>



<p class="wp-block-paragraph">The above piece of code would make a raised button according to the Android material design guideline, with support from API 11 and above. If you closely notice I have used the&nbsp;<code>"?android:attr/selectableItemBackground"</code> property to show ripples on this view. This property can make any view to show ripples, if they detect touch events. Also please don&#8217;t forget to make CardView clickable by adding&nbsp;<code>android:clickable="true"</code> property as shown above. This button would look exactly the same across all supported devices. Learn more about <a title="Android CardView Example" href="http://www.truiton.com/2015/03/android-cardview-example/" target="_blank" rel="noopener noreferrer">CardView here</a>. Have a look in the screen shot below, its the third button:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="805" data-attachment-id="1069" data-permalink="https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/android-material-design-button/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button.png?fit=500%2C805&amp;ssl=1" data-orig-size="500,805" data-comments-opened="1" data-image-title="Android Material Design Button" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button.png?fit=500%2C805&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button.png?resize=500%2C805" alt="Android Material Design Button" class="wp-image-1069" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button.png?w=500&amp;ssl=1 500w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button.png?resize=186%2C300&amp;ssl=1 186w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button.png?resize=300%2C483&amp;ssl=1 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">You can attach an OnClickListener to this button and make it work just like a normal button:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        CardView cv = (CardView)findViewById(R.id.card_view);
        cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Activity", "Clicked");
            }
        });</pre>



<p class="wp-block-paragraph">Although the ripple effect would show only in 5.0 and above devices as even CardView or any other view does not support it on earlier versions. On <span style="text-decoration: underline;">earlier versions of Android</span>, this is how the selection would appear:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="276" height="98" data-attachment-id="1070" data-permalink="https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/android-material-design-button-cardview/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-CardView.png?fit=276%2C98&amp;ssl=1" data-orig-size="276,98" data-comments-opened="1" data-image-title="Android Material Design Button &amp;#8211; CardView" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-CardView.png?fit=276%2C98&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Material-Design-Button-CardView.png?resize=276%2C98" alt="Android Material Design Button - CardView" class="wp-image-1070"/></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">But the button would still appear like a card, as expected. Therefore the above piece of code would make an Android material design button with backward support but with some limitations. Unfortunately there is no support from Google on it right now. Also in some of their posts they have mentioned, that ripple effect is not added in support library as it degrades performance. Just a personal thought: maybe in future, they might start supporting ripples too.</p>



<h2 class="wp-block-heading">Android Material Design Flat Button</h2>



<p class="wp-block-paragraph">Another type of button is flat button. These type of buttons are used to avoid gratuitous layering. For example it is suggested to use this type of button on a CardView, instead of normal raised button. The main purpose of this button is to minimize the user distraction from content. To make a button flat button just add <code>style="?android:attr/borderlessButtonStyle"</code> in your button layout. Just like the code below:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;Button
        android:id="@+id/button2"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Borderless Button"/></pre>



<p class="wp-block-paragraph">This button is shown at number 2 in the screen shot above.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">Although android material design buttons are not fully supported by all the versions till now. Still I would say that they add, great value to the style quotient of your app. They give a neat and clean look to your app. Hence I would suggest to use them with all the backward support available. Also I have not mentioned here, a lot of open-source libraries are also available which, endorse material design buttons. Hope this helped, please connect with us on Facebook, Google+ and Twitter for more updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/">Android Material Design Buttons &#8211; Best Approach</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2015/04/android-material-design-buttons-best-approach/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1065</post-id>	</item>
		<item>
		<title>Floating Action Buttons In Android</title>
		<link>https://www.truiton.com/2015/04/floating-action-buttons-android/</link>
					<comments>https://www.truiton.com/2015/04/floating-action-buttons-android/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Tue, 07 Apr 2015 17:34:02 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Material Design]]></category>
		<category><![CDATA[Material Design Backward Compatibility]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=1044</guid>

					<description><![CDATA[<p>Even though I am an Android developer, sometimes I feel, a lot of new concepts in Android are released in a jiffy. Take for an example, Android material design. It greatly emphasizes on the usage of Floating Action Buttons. But as a matter of fact, when material design was released; no supporting views were released&#8230;&#160;<a href="https://www.truiton.com/2015/04/floating-action-buttons-android/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Floating Action Buttons In Android</span></a></p>
<p>The post <a href="https://www.truiton.com/2015/04/floating-action-buttons-android/">Floating Action Buttons In Android</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="950" height="534" data-attachment-id="1048" data-permalink="https://www.truiton.com/2015/04/floating-action-buttons-android/android-floating-action-button/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Floating-Action-Button.jpg?fit=950%2C534&amp;ssl=1" data-orig-size="950,534" data-comments-opened="1" data-image-title="Android Floating Action Button" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Floating-Action-Button.jpg?fit=950%2C534&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Floating-Action-Button.jpg?resize=950%2C534" alt="Android Floating Action Button" class="wp-image-1048" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Floating-Action-Button.jpg?w=950&amp;ssl=1 950w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-Floating-Action-Button.jpg?resize=300%2C169&amp;ssl=1 300w" sizes="auto, (max-width: 950px) 100vw, 950px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Even though I am an Android developer, sometimes I feel, a lot of new concepts in Android are released in a jiffy. Take for an example, Android material design. It greatly emphasizes on the usage of <a title="Android Material Design Buttons" href="http://www.google.co.in/design/spec/components/buttons.html" target="_blank" rel="noopener noreferrer">Floating Action Buttons</a>. But as a matter of fact, when material design was released; no supporting views were released with it, to display a floating action button in android. Although they did provide a <a title="Android Floating Action Button Sample" href="https://developer.android.com/samples/FloatingActionButtonBasic/index.html" target="_blank" rel="noopener noreferrer">sample here</a>, but it had a limitation that it can be used on API 21 and above only. In a way it is correct, that it should not be used on earlier versions of Android. Since material design guidelines came with API 21. But on the other hand the AppCompat support library v7 makes makes most of the <a title="Android Material Design With Backward Compatibility" href="http://www.truiton.com/2015/02/android-material-design-backward-compatibility/" target="_blank" rel="noopener noreferrer">material design compatible</a> with earlier versions of Android. Therefore, when we use the Android floating action button, we are bound to display it on all versions of android which are supported by our app.</p>



<p class="wp-block-paragraph">This brings up a fresh debate on how to display a floating action button in an android app, with backward support. Thanks&nbsp; to recent developments a new addition in the support repository has been made which enables us to show FAB in Android. It is called Android Design Support Library. But since this addition came in almost a year late, many third party libraries are also available, through which a FAB can be implemented. In this tutorial, we will talk about some of the leading Android FAB libraries:</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android Floating Action Buttons Tutorial</h2>



<p class="wp-block-paragraph">I will use a RecyclerView, on top of which I will be displaying an Android FAB button. Further in this tutorial I will use that FAB button to add an item in my RecyclerView. To ease the development I will refer to my <a title="Android RecyclerView Tutorial" href="http://www.truiton.com/2015/02/android-recyclerview-tutorial/" target="_blank" rel="noopener noreferrer">RecyclerView tutorial</a>, which already has a defined method for adding an item in the RecyclerView.</p>



<h3 class="wp-block-heading">1) <a href="http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html" target="_blank" rel="noopener noreferrer">FloatingActionButton</a> by Official Android <a href="http://developer.android.com/tools/support-library/features.html#design" target="_blank" rel="noopener noreferrer">Design Support Library</a></h3>



<p class="wp-block-paragraph">As I mentioned recently in the start of June 15, a new library was added in the Android support repository, called the Design Support library. This library has many new UI widgets, but one of the most important addition is the <code><code></code></code><a href="http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html">FloatingActionButton</a>. Now android supports the Floating action button from API 7 and above through the official support library. To start please include the new Android design support library in your project&#8217;s build.gradle file:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">compile 'com.android.support:design:22.2.0'</pre>



<p class="wp-block-paragraph">Next to display the Android Floating Action Button, please use the <code>android.support.design.widget.FloatingActionButton</code> xml tag to include it in your layout:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FABActivity">

    &lt;RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        &lt;android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"/>

    &lt;/RelativeLayout>

    &lt;android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:src="@mipmap/ic_add"
        app:fabSize="normal"
        app:layout_anchor="@id/coordinatorLayout"
        app:layout_anchorGravity="bottom|right|end"
        app:rippleColor="#F06292"/>
&lt;/android.support.design.widget.CoordinatorLayout>
</pre>



<p class="wp-block-paragraph">You may observe that I am also using a <a href="http://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html" target="_blank" rel="noopener noreferrer">CoordinatorLayout</a> with this floating action button. Coordinator Layout helps us in building interactions between child views. Although in this example there is no use for a coordinator layout, but it can help if a snackbar is used. To read more about it please refer to this <a href="http://www.truiton.com/2015/06/android-snackbar-example/" target="_blank" rel="noopener noreferrer">Android Snackbar Tutorial</a>.</p>



<p class="wp-block-paragraph">To detect clicks from an Android Floating Action Button please use this piece of code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Write here anything that you wish to do on click of FAB
        // Code to Add an item with default animation
        int index = mAdapter.getItemCount();
        DataObject obj = new DataObject("Some Primary Text " + index,
                "Secondary " + index);
        ((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);
        //Ends Here
    }
});</pre>



<h3 class="wp-block-heading">2) <a href="https://github.com/makovkastar/FloatingActionButton" target="_blank" rel="noopener noreferrer">FloatingActionButton</a> by <a href="https://github.com/makovkastar" target="_blank" rel="noopener noreferrer">makovkastar</a></h3>



<p class="wp-block-paragraph">This library is one of the most popular floating action button libraries available. Most of the apps are using this library. Its quite simple to use as well. It offers a great feature of showing and hiding the FAB with animation, which can give a wow factor to your app. It works for Android API 7 and above, and has an Apache 2.0 license. To start off, please include it in the dependencies section of your gradle file:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">compile 'com.melnykov:floatingactionbutton:1.3.0'</pre>



<p class="wp-block-paragraph">Further, please add&nbsp;<code>com.melnykov.fab.FloatingActionButton</code> view to your layout for displaying an Android FAB with RecyclerView:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:fab="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".FABActivity">

    &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

    &lt;com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@mipmap/ic_add"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        fab:fab_colorNormal="#FF4081"
        fab:fab_colorPressed="#C2185B"
        fab:fab_colorRipple="#F06292"/>

&lt;/RelativeLayout>
</pre>



<p class="wp-block-paragraph">In the above layout I used a mipmap(drawable) image to display the plus sign:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="72" height="72" data-attachment-id="1051" data-permalink="https://www.truiton.com/2015/04/floating-action-buttons-android/ic_add/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/ic_add.png?fit=72%2C72&amp;ssl=1" data-orig-size="72,72" data-comments-opened="1" data-image-title="ic_add" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/ic_add.png?fit=72%2C72&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/ic_add.png?resize=72%2C72" alt="Android FAB add image" class="wp-image-1051"/></figure></div>



<p class="wp-block-paragraph">Next please add the following lines to detect the click events of Android floating action button:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.attachToRecyclerView(mRecyclerView);

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Write here anything that you wish to do on click of FAB
                
        // Code to Add an item with default animation
        int index = mAdapter.getItemCount();
        DataObject obj = new DataObject("Some Primary Text " + index,
                "Secondary " + index);
        ((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);
        //Ends Here
    }
});</pre>



<p class="wp-block-paragraph">This would result in a floating action button like this:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="805" data-attachment-id="1053" data-permalink="https://www.truiton.com/2015/04/floating-action-buttons-android/android-fab-button/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-FAB-Button.png?fit=500%2C805&amp;ssl=1" data-orig-size="500,805" data-comments-opened="1" data-image-title="Android FAB Button" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-FAB-Button.png?fit=500%2C805&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-FAB-Button.png?resize=500%2C805" alt="Android FAB Button" class="wp-image-1053" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-FAB-Button.png?w=500&amp;ssl=1 500w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-FAB-Button.png?resize=186%2C300&amp;ssl=1 186w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/04/Android-FAB-Button.png?resize=300%2C483&amp;ssl=1 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></figure></div>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">3) <a href="https://github.com/futuresimple/android-floating-action-button" target="_blank" rel="noopener noreferrer">android-floating-action-button</a> by <a href="https://github.com/futuresimple" target="_blank" rel="noopener noreferrer">futuresimple</a></h3>



<p class="wp-block-paragraph">This library is a fork of FloatingActionButton by <a href="https://github.com/makovkastar" target="_blank" rel="noopener noreferrer">makovkastar</a>. This library offers a <code>FloatingActionsMenu</code>, its a unique menu of floating action buttons which pop up when the main floating button is clicked. This menu of floating action buttons also offer labels, just like normal menu items. This library is also very popular among android apps. It works for Android API 14 and above, has Apache 2.0 license.</p>



<h3 class="wp-block-heading">4) <a href="https://github.com/FaizMalkani/FloatingActionButton" target="_blank" rel="noopener noreferrer">FloatingActionButton</a> by <a href="https://github.com/FaizMalkani" target="_blank" rel="noopener noreferrer">FaizMalkani</a></h3>



<p class="wp-block-paragraph">This library offers a clean and simple to use implementation of floating action buttons in Android.&nbsp;This library can be used to speed up the development pace as it does not offer any useless features. A simple floating button nothing else. It works for Android API 7 and above, has MIT license.</p>



<h3 class="wp-block-heading">5) <a href="https://github.com/shell-software/fab" target="_blank" rel="noopener noreferrer">fab</a> by <a href="https://github.com/shell-software" target="_blank" rel="noopener noreferrer">shell-software</a></h3>



<p class="wp-block-paragraph">This library is highly customizable and offers a lot of features. By using this library you can add different types of animations to the floating action button. Fab can also be used to define the shadow offset, stroke color, and stroke width of the button. Its a great library under Apache 2.0 license with support from Android API 9 and above.</p>



<h3 class="wp-block-heading">6) <a href="https://github.com/Clans/FloatingActionButton" target="_blank" rel="noopener noreferrer">FloatingActionButton</a> by <a href="https://github.com/Clans" target="_blank" rel="noopener noreferrer">Clans</a></h3>



<p class="wp-block-paragraph">This library is also highly customizable offers a great range of features like shadow colors and offsets. A great feature about this library is that it offers support for custom animations. Also this library can be used to display menu items with labels. This library requires min Android API 15 to run, and has an Apache 2.0 license.</p>



<h3 class="wp-block-heading">7) <a href="https://github.com/sephiroth74/android-floating-action-menu" target="_blank" rel="noopener noreferrer">android-floating-action-menu</a> by <a href="https://github.com/sephiroth74" target="_blank" rel="noopener noreferrer">sephiroth74</a></h3>



<p class="wp-block-paragraph">Another floating action library which is inspired from Google+ floating menu. This library offers a builder pattern to build a menu of floating action buttons of same size. This is something new because with this library you can create an animation like the one in camera app of android. This library can run on Android API 9 and above, has an Apache 2.0 license.</p>



<h3 class="wp-block-heading">8) <a href="https://github.com/oguzbilgener/CircularFloatingActionMenu" target="_blank" rel="noopener noreferrer">CircularFloatingActionMenu</a> by <a href="https://github.com/oguzbilgener" target="_blank" rel="noopener noreferrer">oguzbilgener</a></h3>



<p class="wp-block-paragraph">If you wish to go for something new, you can try this library. This library offers a new feature of circular floating action buttons. Although this sort of guideline is not described in material design, but I believe its something new and innovative. This library is under MIT license and requires Android API 15 and above.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">Although I have tried to list down all the libraries for Android floating action buttons, in case I have missed out any one, please notify me. Hope this article helped you to find the best floating action button library for your project. Connect with us on Facebook, Google+, and Twitter for more updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2015/04/floating-action-buttons-android/">Floating Action Buttons In Android</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2015/04/floating-action-buttons-android/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1044</post-id>	</item>
		<item>
		<title>Android CardView Example</title>
		<link>https://www.truiton.com/2015/03/android-cardview-example/</link>
					<comments>https://www.truiton.com/2015/03/android-cardview-example/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Thu, 19 Mar 2015 16:15:03 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Material Design]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=946</guid>

					<description><![CDATA[<p>Now in android creating card views has gotten even more simpler. As with material design a new view was introduced through the support&#160;v7 library, called CardView. It can be used in many ways to display cards in android. For example, it can be used like an independent view to show floating search bar on top&#8230;&#160;<a href="https://www.truiton.com/2015/03/android-cardview-example/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android CardView Example</span></a></p>
<p>The post <a href="https://www.truiton.com/2015/03/android-cardview-example/">Android CardView Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="640" height="427" data-attachment-id="947" data-permalink="https://www.truiton.com/2015/03/android-cardview-example/android-cardview-example-featured/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example-Featured.png?fit=640%2C427&amp;ssl=1" data-orig-size="640,427" data-comments-opened="1" data-image-title="Android CardView Example &amp;#8211; Featured" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example-Featured.png?fit=640%2C427&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example-Featured.png?resize=640%2C427" alt="Android CardView Example" class="wp-image-947" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example-Featured.png?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example-Featured.png?resize=300%2C200&amp;ssl=1 300w" sizes="auto, (max-width: 640px) 100vw, 640px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Now in android creating card views has gotten even more simpler. As with material design a new view was introduced through the support&nbsp;v7 library, called <a title="Android CardView" href="https://developer.android.com/reference/android/support/v7/widget/CardView.html" target="_blank" rel="noopener noreferrer">CardView</a>. It can be used in many ways to display cards in android. For example, it can be used like an independent view to show floating search bar on top of the screen also it can be used to display cards in a list. In this Android CardView example we will discuss how this view can be implemented in a <a title="Android RecyclerView Tutorial" href="http://www.truiton.com/2015/02/android-recyclerview-tutorial/" target="_blank" rel="noopener noreferrer">RecyclerView list</a>.</p>



<p class="wp-block-paragraph">Recently 5.1 version of android was released, with an update for support&nbsp;v7 library. This is important to know because it contained a minor update for RecyclerView package. In this tutorial of Android CardView we will also discuss how this can effect us.</p>



<h2 class="wp-block-heading">Android CardView Tutorial</h2>



<p class="wp-block-paragraph">To start off lets understand what exactly Android CardView is? Its a new widget for Android, which can be used to display a card sort of a layout in android. As you may know Android material design is inspired from paper and ink concept. Mostly it displays views on top of each other, with shadows. In simple terms, Android CardView is such a view which has all material design properties, most importantly showing shadows according the elevation. The best part about this view is that it extends <a title="Android FrameLayout" href="https://developer.android.com/reference/android/widget/FrameLayout.html" target="_blank" rel="noopener noreferrer">FrameLayout</a> and it can be displayed on all the platforms of android since it&#8217;s available through the Support v7 library. Lets have a look at some of its properties:</p>



<h3 class="wp-block-heading">1) CardView overlapping corners:</h3>



<p class="wp-block-paragraph"><strong>Java:</strong> <a rel="noopener noreferrer" href="https://developer.android.com/reference/android/support/v7/widget/CardView.html#setPreventCornerOverlap%28boolean%29" target="_blank"><code>setPreventCornerOverlap(boolean)</code></a></p>



<p class="wp-block-paragraph"><strong>XML:</strong> cardView:cardPreventCornerOverlap</p>



<p class="wp-block-paragraph">A unique property of Android CardView is that, it has rounded corners. The interesting part about this property is that, it clips the corners of content automatically. Unfortunately this property, as of now is not supported on devices running API 20 or low. Instead when an image covering the full CardView is displayed on these devices, a padding is added to the image to prevent the overlapping corners. To make the edges overlap this property can be used. In this case you may need to use an image with round corners. Also keep in mind this does not has an effect on API 21 and above as the clipping is automatically done.</p>



<h3 class="wp-block-heading">2) CardView Content Padding:</h3>



<p class="wp-block-paragraph"><strong>Java:</strong> <a href="https://developer.android.com/reference/android/support/v7/widget/CardView.html#setContentPadding%28int,%20int,%20int,%20int%29"><code>setContentPadding(int, int, int, int)</code></a></p>



<p class="wp-block-paragraph"><strong>XML:</strong> cardView:contentPadding</p>



<p class="wp-block-paragraph">Since when a card is displayed on screen, usually multiple views are added inside it. Fortunately Android CardView supports this unique property, through which the padding for CardView content can be set once and for all. Advantage of this property is that we don&#8217;t need to set margin or padding for each view inside the CardView individually to make it look good.</p>



<h3 class="wp-block-heading">3) CardView Elevation:</h3>



<p class="wp-block-paragraph"><strong>Java:</strong> <a href="https://developer.android.com/reference/android/support/v7/widget/CardView.html#setCardElevation%28float%29"><code>setCardElevation(float)</code></a></p>



<p class="wp-block-paragraph"><strong>XML:</strong> cardView:cardElevation</p>



<p class="wp-block-paragraph">This property defines the elevation of CardView. As you may know this one of the core concepts of Android material design. According to the elevation defined, Android system dynamically shows the shadow of any material on screen.</p>



<h2 class="wp-block-heading">Android CardView Example</h2>



<p class="wp-block-paragraph">In this tutorial we would use a <a title="Android RecyclerView" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html" target="_blank" rel="noopener noreferrer">RecyclerView</a> to show a list of CardViews. Since this tutorial is not about the working of Android RecyclerView, we would not be discussing the detailed working of RecyclerView. If you wish to learn in detail about it, please refer to this <a title="Android RecyclerView Tutorial" href="http://www.truiton.com/2015/02/android-recyclerview-tutorial/" target="_blank" rel="noopener noreferrer">RecyclerView tutorial</a>.</p>



<p class="wp-block-paragraph">Lets have a look at the CardView xml, which I will be using in this tutorial.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    &lt;android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp">

        &lt;RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            &lt;TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:textStyle="bold"/>

            &lt;TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:layout_marginTop="10dp"/>
        &lt;/RelativeLayout>
    &lt;/android.support.v7.widget.CardView>


&lt;/LinearLayout></pre>



<p class="wp-block-paragraph">Before starting off don&#8217;t forget to include these dependencies in your app gradle.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dependencies {
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:cardview-v7:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'
}</pre>



<p class="wp-block-paragraph">Next please a look at activity layout, which shows the RecyclerView:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".CardViewActivity">

    &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

&lt;/RelativeLayout>
</pre>



<p class="wp-block-paragraph">In this Android CardView Example, we will use this object for supplying the data to adapter:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.cardviewexample;

public class DataObject {
    private String mText1;
    private String mText2;

    DataObject (String text1, String text2){
        mText1 = text1;
        mText2 = text2;
    }

    public String getmText1() {
        return mText1;
    }

    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }

    public String getmText2() {
        return mText2;
    }

    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
}
</pre>



<p class="wp-block-paragraph">The activity below shows the CardView list:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.cardviewexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;


public class CardViewActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "CardViewActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_view);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);

        // Code to Add an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);

        // Code to remove an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).deleteItem(index);
    }


    @Override
    protected void onResume() {
        super.onResume();
        ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter
                .MyClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                Log.i(LOG_TAG, " Clicked on Item " + position);
            }
        });
    }

    private ArrayList&lt;DataObject&gt; getDataSet() {
        ArrayList results = new ArrayList&lt;DataObject&gt;();
        for (int index = 0; index &lt; 20; index++) {
            DataObject obj = new DataObject("Some Primary Text " + index,
                    "Secondary " + index);
            results.add(index, obj);
        }
        return results;
    }
}
</pre>



<p class="wp-block-paragraph">Next lets have a look at the main class where RecyclerView adapter is defined. This class shows how to include an Android CardView widget in a RecyclerView:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.cardviewexample;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter&lt;MyRecyclerViewAdapter
        .DataObjectHolder&gt; {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList&lt;DataObject&gt; mDataset;
    private static MyClickListener myClickListener;

    public static class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            myClickListener.onItemClick(getAdapterPosition(), v);
        }
    }

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(ArrayList&lt;DataObject&gt; myDataset) {
        mDataset = myDataset;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view_row, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());
    }

    public void addItem(DataObject dataObj, int index) {
        mDataset.add(index, dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}</pre>



<p class="wp-block-paragraph">This would give an output screen with CardViews like these:</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="805" data-attachment-id="950" data-permalink="https://www.truiton.com/2015/03/android-cardview-example/android-cardview-example-2/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example.png?fit=500%2C805&amp;ssl=1" data-orig-size="500,805" data-comments-opened="1" data-image-title="Android CardView Example" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example.png?fit=500%2C805&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example.png?resize=500%2C805" alt="Android CardView Example" class="wp-image-950" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example.png?w=500&amp;ssl=1 500w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-CardView-Example.png?resize=186%2C300&amp;ssl=1 186w" sizes="auto, (max-width: 500px) 100vw, 500px" /></figure></div>



<p class="wp-block-paragraph">As mentioned in the opening lines of this tutorial, recently an update to support v7 library was released. This update deprecated an important method called <code>getPosition()</code>. This method was used to return the position of view holder item. But now two new methods, <a rel="noopener noreferrer" href="http://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getLayoutPosition%28%29" target="_blank"><code>getlayoutPosition()</code></a> and <a rel="noopener noreferrer" href="http://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getAdapterPosition%28%29" target="_blank"><code>getadapterPosition()</code></a> are introduced instead of it. These methods return the position of layout in view holder and adapter position, respectively. The later one gives the position basis on the items currently in the adapter. For better understanding, please have a look at the full source code below:</p>



<p class="wp-block-paragraph" style="text-align:center"><a class="fasc-button fasc-size-large fasc-type-flat fasc-rounded-medium ico-fa fasc-ico-before fa-github" style="background-color: #0364af; color: #ffffff;" target="_blank" rel="noopener noreferrer" href="https://github.com/Truiton/CardViewExample">Source Code</a></p>



<p class="wp-block-paragraph">Also in the above adapter class you may see that in onCreateViewHolder method, the <em>card_view_row</em> is used to inflate the CardView for a RecyclerView list. Also you may observe that in this example of Android CardView, a custom on item click listener is used. This listener is used to capture the onClick event of the CardView. It is used in such a way to simplify the code. But this can be used to detect clicks of the specific views inside CardView. Just like the Google app, we can define buttons inside the card, and detect it clicks. Hope this <a title="Android CardView Example" href="http://www.truiton.com/2015/03/android-cardview-example/">Android CardView Tutorial</a> helped you understand the working of CardView and RecyclerView better. Connect with us through Google+, Facebook, and Twitter for more updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2015/03/android-cardview-example/">Android CardView Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2015/03/android-cardview-example/feed/</wfw:commentRss>
			<slash:comments>34</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">946</post-id>	</item>
		<item>
		<title>Android RecyclerView vs ListView &#124; Comparison</title>
		<link>https://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/</link>
					<comments>https://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Sun, 01 Mar 2015 18:33:46 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Material Design]]></category>
		<category><![CDATA[Material Design Backward Compatibility]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=869</guid>

					<description><![CDATA[<p>Android is an evolving platform. Recently 5.0 (Lollipop) version of Android was released, along with the official&#160;RecyclerView support v7 library. As per the official documentation RecyclerView is a major enhancement over ListView. It contains many new features like ViewHolder, ItemDecorator, LayoutManager, and SmoothScroller. But one thing that certainly gives it an edge over the ListView&#8230;&#160;<a href="https://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android RecyclerView vs ListView &#124; Comparison</span></a></p>
<p>The post <a href="https://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/">Android RecyclerView vs ListView | Comparison</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><a href="https://www.flickr.com/photos/thebusybrain/2492945625" target="_blank" rel="noopener noreferrer"><img data-recalc-dims="1" loading="lazy" decoding="async" width="640" height="439" data-attachment-id="873" data-permalink="https://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/android-recyclerview-vs-listview-featured/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-RecyclerView-vs-Listview-Featured.jpg?fit=640%2C439&amp;ssl=1" data-orig-size="640,439" data-comments-opened="1" data-image-title="Android RecyclerView vs Listview &amp;#8211; Featured" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-RecyclerView-vs-Listview-Featured.jpg?fit=640%2C439&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-RecyclerView-vs-Listview-Featured.jpg?resize=640%2C439" alt="Photo Credit: TheBusyBrain - Android RecyclerView vs Listview" class="wp-image-873" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-RecyclerView-vs-Listview-Featured.jpg?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/03/Android-RecyclerView-vs-Listview-Featured.jpg?resize=300%2C206&amp;ssl=1 300w" sizes="auto, (max-width: 640px) 100vw, 640px" /></a></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Android is an evolving platform. Recently 5.0 (Lollipop) version of Android was released, along with the official&nbsp;RecyclerView support v7 library. As per the official documentation <a title="Android RecyclerView" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html" target="_blank" rel="noopener noreferrer">RecyclerView</a> is a major enhancement over <a title="Android ListView" href="http://developer.android.com/guide/topics/ui/layout/listview.html" target="_blank" rel="noopener noreferrer">ListView</a>. It contains many new features like ViewHolder, ItemDecorator, LayoutManager, and SmoothScroller. But one thing that certainly gives it an edge over the ListView is; the ability to have animations while adding or removing an item. Here in&nbsp;Android RecyclerView vs ListView sum up I would compare the various features of RecyclerView and ListView and form an opinion why it should be used in new implementations.</p>



<p class="wp-block-paragraph">In this article I will not discuss on how to implement a RecyclerView. Although If you wish to know about the implementation of it, please refer to <a title="Android RecyclerView Tutorial" href="http://www.truiton.com/2015/02/android-recyclerview-tutorial/" target="_blank" rel="noopener noreferrer">Android RecyclerView Example</a>.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Differences Between RecyclerView and ListView</h2>



<h3 class="wp-block-heading">1. View Holders</h3>



<p class="wp-block-paragraph">In ListView, defining view holders was a suggested approach for keeping references for views. But it was not a compulsion. Although by not doing so, ListView used show stale data. Another major drawback of not using view holders could lead to a heavy operation of finding views by ids every time. Which resulted in laggy ListViews.</p>



<p class="wp-block-paragraph">This problem is solved in RecylerView by the use of <a title="Android RecyclerView ViewHolder" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html" target="_blank" rel="noopener noreferrer">RecyclerView.ViewHolder</a> class. This is one of the major differences in RecyclerView and ListView. When implementing a RecyclerView this class is used to define a ViewHolder object which is used by the adapter to bind ViewHolder with a position. Another point to be noted here, is that while implementing the adapter for RecyclerView, providing a ViewHolder is compulsory. This makes the implementation a little complex, but solves the issues faced in ListView.</p>



<h3 class="wp-block-heading">2. Layout Manager</h3>



<p class="wp-block-paragraph">When speaking of ListViews, only one type of ListView is available i.e. the vertical ListView. You cannot implement a ListView with horizontal scroll. I know there are ways to implement a horizontal scroll, but believe me it was not designed to work that way.</p>



<p class="wp-block-paragraph">But now when we look at Android RecyclerView vs ListView, we have support for horizontal collections as well. In-fact it supports multiple types of lists. To support multiple types of lists it uses RecyclerView.LayoutManager class. This is something new that ListView does not have. RecyclerView supports three types of predefined Layout Managers:</p>



<ul class="wp-block-list"><li><a href="https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html"><code>LinearLayoutManager</code></a> &#8211; This is the most commonly used layout manager in case of RecyclerView. Through this, we can create both horizontal and vertical scroll lists.</li><li><a href="https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html"><code>StaggeredGridLayoutManager</code></a> &#8211; Through this layout manager, we can create staggered lists. Just like the Pinterest screen.</li><li><a href="https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html"><code>GridLayoutManager</code></a>&#8211; This layout manager can be used to display grids, like any picture gallery.</li></ul>



<h3 class="wp-block-heading">3. Item Animator</h3>



<p class="wp-block-paragraph">Animations in a list is a whole new dimension, which has endless possibilities. In a ListView, as such there are no special provisions through which one can animate, addition or deletion of items. Instead later on as android evolved <a title="Android ViewPropertyAnimator" href="http://developer.android.com/reference/android/view/ViewPropertyAnimator.html" target="_blank" rel="noopener noreferrer">ViewPropertyAnimator</a> was suggested by Google&#8217;s Chet Haase in this <a title="ViewPropertyAnimator" href="https://www.youtube.com/watch?v=8MIfSxgsHIs" target="_blank" rel="noopener noreferrer">video tutorial</a> for animations in ListView.</p>



<p class="wp-block-paragraph">On the other hand comparing Android RecyclerView vs ListView, it has <a title="Android RecyclerView Item Animator" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemAnimator.html" target="_blank" rel="noopener noreferrer">RecyclerView.ItemAnimator</a> class for handling animations. Through this class custom animations can be defined for item addition, deletion and move events. Also it provides a <a title="Android RecyclerView DefaultItemAnimator" href="https://developer.android.com/reference/android/support/v7/widget/DefaultItemAnimator.html" target="_blank" rel="noopener noreferrer">DefaultItemAnimator</a>, in case you don&#8217;t need any customizations.</p>



<h3 class="wp-block-heading">4. Adapter</h3>



<p class="wp-block-paragraph">ListView adapters were simple to implement. They had a main method <a href="http://developer.android.com/reference/android/widget/Adapter.html#getView%28int,%20android.view.View,%20android.view.ViewGroup%29">getView</a> where all the magic used to happen. Where the views were bound to a position. Also they used to have an interesting method&nbsp;<span class="sympad"><a href="http://developer.android.com/reference/android/widget/Adapter.html#registerDataSetObserver%28android.database.DataSetObserver%29">registerDataSetObserver</a></span> where one can set an observer right in the adapter. This feature is also present in RecyclerView, but <a title="Android RecyclerView.AdapterDataObserver" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.AdapterDataObserver.html" target="_blank" rel="noopener noreferrer">RecyclerView.AdapterDataObserver</a> class is used for it. But the point in favor of ListView is that it supports three default implementations of adapters:</p>



<ul class="nolist wp-block-list"><li><a href="http://developer.android.com/reference/android/widget/ArrayAdapter.html"><code>ArrayAdapter</code></a></li><li><a href="http://developer.android.com/reference/android/widget/CursorAdapter.html"><code>CursorAdapter</code></a></li><li><a href="http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html"><code>SimpleCursorAdapter</code></a></li></ul>



<p class="wp-block-paragraph">Whereas RecyclerView adapter, has all the functionality that ListView adapters had except the built in support for DB cursors and ArrayLists. In <a title="Android RecyclerView Adapter" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html" target="_blank" rel="noopener noreferrer">RecyclerView.Adapter</a> as of now we have to make a custom implementation to supply data to the adapter. Just like a <a title="Android BaseAdapter" href="http://developer.android.com/reference/android/widget/BaseAdapter.html" target="_blank" rel="noopener noreferrer">BaseAdapter</a> does for ListViews. Although if you wish to know more about RecyclerView adapter implementation, please refer to <a title="Android RecyclerView Tutorial" href="http://www.truiton.com/2015/02/android-recyclerview-tutorial/" target="_blank" rel="noopener noreferrer">Android RecyclerView Example</a>.</p>



<h3 class="wp-block-heading">5. Notifying Change in Data</h3>



<p class="wp-block-paragraph">When working with a ListView, if the data set is changed you have to call the <code><code></code></code><a href="https://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()" target="_blank" rel="noopener noreferrer">notifyDataSetChanged</a> method of the underlying adapter to refresh data. Or set the <code><code></code></code><a href="https://developer.android.com/reference/android/widget/ArrayAdapter.html#setNotifyOnChange(boolean)" target="_blank" rel="noopener noreferrer">setNotifyOnChange</a> method to true incase you wish to call the&nbsp;<code><code></code></code><a href="https://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()" target="_blank" rel="noopener noreferrer">notifyDataSetChanged</a> method automatically. But in both cases the out come is very heavy on the list. Basically it refreshes the views of list.</p>



<p class="wp-block-paragraph">But on the contrary in a RecyclerView adapter, if a single item or a range of items have changed, there are methods to notify the change accordingly. Those are <code><code></code></code><a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#notifyItemChanged(int)" target="_blank" rel="noopener noreferrer">notifyItemChanged</a> and <code><code></code></code><a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#notifyItemRangeChanged(int,%20int)" target="_blank" rel="noopener noreferrer">notifyItemRangeChanged</a> respectively and many more like:</p>



<ul class="wp-block-list"><li><a rel="noopener noreferrer" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#notifyItemInserted(int)" target="_blank"><code>notifyItemInsterted</code></a></li><li><a rel="noopener noreferrer" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#notifyItemMoved(int,%20int)" target="_blank"><code>notifyItemMoved</code></a></li><li><a rel="noopener noreferrer" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#notifyItemRangeInserted(int,%20int)" target="_blank"><code>notifyItemRangeInsterted</code></a></li><li><a rel="noopener noreferrer" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#notifyItemRangeRemoved(int,%20int)" target="_blank"><code>notifyItemRangeRemoved</code></a></li></ul>



<p class="wp-block-paragraph">And of course it has the original method to refresh the whole list i.e. <code><code></code></code><a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged()" target="_blank" rel="noopener noreferrer">notifyDataSetChanged</a> which notifies the adapted the whole data set has changed.</p>



<h3 class="wp-block-heading">6. Item Decoration</h3>



<p class="wp-block-paragraph">To display custom dividers in a ListView, one could have easily added these parameters in the ListView XML:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">android:divider="@android:color/transparent"
android:dividerHeight="5dp"</pre>



<p class="wp-block-paragraph">The interesting part about Android RecyclerView is that, as of now it does not show a divider between items by default. Although the guys at Google must have left this out for customization, intentionally. But this greatly increases the effort for a developer. If you wish to add a divider between items, you may need to do a custom implementation by using <a title="Android RecyclerView.ItemDecoration" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html" target="_blank" rel="noopener noreferrer">RecyclerView.ItemDecoration</a> class.</p>



<p class="wp-block-paragraph">Or you can apply a hack by using this file from official samples: <a title="DividerItemDecoration.java" href="https://chromium.googlesource.com/android_tools/+/18728e9dd5dd66d4f5edf1b792e77e2b544a1cb0/sdk/extras/android/support/samples/Support7Demos/src/com/example/android/supportv7/widget/decorator/DividerItemDecoration.java" target="_blank" rel="noopener noreferrer">DividerItemDecoration.java</a></p>



<h3 class="wp-block-heading">7. OnItemTouchListener</h3>



<p class="wp-block-paragraph">Listviews used to have a simple implementation for detection of clicks, i.e. by the use of <a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html">AdapterView.OnItemClickListener</a> interface.</p>



<p class="wp-block-paragraph">But on the other hand <a title="Android RecyclerView.OnItemTouchListener" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnItemTouchListener.html" target="_blank" rel="noopener noreferrer">RecyclerView.OnItemTouchListener</a> interface is used to detect touch events in Android RecyclerView. It complicates the implementation a little, but it gives a greater control to the developer for intercepting touch events. The official documentation states, it can be useful for&nbsp;gestural manipulations as it intercepts a touch event before it is delivered to RecyclerView.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">Conclusion</h3>



<p class="wp-block-paragraph">I would like to conclude this <a title="Android RecyclerView vs ListView | Comparison" href="http://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/">Android RecyclerView vs ListView</a> comparison by saying RecyclerView is filled with a lot customisable functionality. It can be very useful for implementing complex lists or grids. But also its a little difficult to implement as compared to ListView as it contains multiple classes where implementation is required. Hope this helped you decide. Please connect with us through Facebook, Google+ or Twitter for more updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/">Android RecyclerView vs ListView | Comparison</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2015/03/android-recyclerview-vs-listview-comparison/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">869</post-id>	</item>
		<item>
		<title>Android RecyclerView Tutorial</title>
		<link>https://www.truiton.com/2015/02/android-recyclerview-tutorial/</link>
					<comments>https://www.truiton.com/2015/02/android-recyclerview-tutorial/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Fri, 27 Feb 2015 07:09:43 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Material Design]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=852</guid>

					<description><![CDATA[<p>One of the latest widgets introduced in Android is RecyclerView. Many say its a replacement of existing ListView widget, also termed as ListView 2.0. I browsed though the entire world wide web, in search of a working Android RecyclerView example with explanation. Gotta say couldn&#8217;t find one. After a lot of struggle I finally made&#8230;&#160;<a href="https://www.truiton.com/2015/02/android-recyclerview-tutorial/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android RecyclerView Tutorial</span></a></p>
<p>The post <a href="https://www.truiton.com/2015/02/android-recyclerview-tutorial/">Android RecyclerView Tutorial</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><a href="https://www.flickr.com/photos/10037058@N08/3696670712" target="_blank" rel="noopener noreferrer"><img data-recalc-dims="1" loading="lazy" decoding="async" width="640" height="426" data-attachment-id="857" data-permalink="https://www.truiton.com/2015/02/android-recyclerview-tutorial/android-recyclerview-tutorial-featured/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-Featured.jpg?fit=640%2C426&amp;ssl=1" data-orig-size="640,426" data-comments-opened="1" data-image-title="Android RecyclerView Tutorial Featured" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-Featured.jpg?fit=640%2C426&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-Featured.jpg?resize=640%2C426" alt="Photo Credit: James Wang - Android RecyclerView Tutorial" class="wp-image-857" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-Featured.jpg?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-Featured.jpg?resize=300%2C200&amp;ssl=1 300w" sizes="auto, (max-width: 640px) 100vw, 640px" /></a></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">One of the latest widgets introduced in Android is RecyclerView. Many say its a replacement of existing ListView widget, also termed as ListView 2.0. I browsed though the entire world wide web, in search of a working Android RecyclerView example with explanation. Gotta say couldn&#8217;t find one. After a lot of struggle I finally made one and decided to write a tutorial for it my self. So here it goes, in this Android RecyclerView Tutorial I would demonstrate a working example of RecyclerView, with some basic functionality. Where I would show you how to include a RecyclerView into an Android app project with an onItemClick event.</p>



<p class="wp-block-paragraph">Android RecyclerView falls under the material design of android. Hence to maintain backward compatibility, first I would suggest you to go through my article on <a title="Android Material Design With Backward Compatibility" href="http://www.truiton.com/2015/02/android-material-design-backward-compatibility/" target="_blank" rel="noopener noreferrer">maintaining backward compatibility of material design</a>. I will try to keep this tutorial on Android RecyclerView short and simple as it consists a huge number of classes and each one of them is designed to be customized.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android RecyclerView: A basic Introduction</h2>



<p class="wp-block-paragraph">Just like ListView, <a title="Android RecyclerView" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html" target="_blank" rel="noopener noreferrer">RecyclerView</a> is used to display a large amount of similar items on screen. But since the Android team was building an enhancement, they added a bunch of new features to RecyclerView. Each one of these new features, give a platform to developers for implementing a highly custom made RecyclerView. One of the custom implementations of RecyclerView is the new Gmail app on Android. I took inspiration from it and tried to make a subset of it, in this Android RecyclerView Tutorial. Below I will discuss the 4 key classes of RecyclerView but first lets add its dependency in build.gradle(:app):-</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
}</pre>



<h3 class="wp-block-heading">RecyclerView with different layouts</h3>



<p class="wp-block-paragraph">One of the new concepts introduced in RecyclerView is the Layout managers. This class basically defines the type of layout which&nbsp;RecyclerView should use. In RecyclerView you can define three types of <a title="Android RecyclerView LayoutManager" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager.html" target="_blank" rel="noopener noreferrer">LayoutManager</a> (s).</p>



<ol class="wp-block-list"><li><a href="https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html"><code>LinearLayoutManager</code></a> &#8211; This LayoutManager can be used to display linear lists, they could be vertical or horizontal.</li><li><a href="https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html"><code>GridLayoutManager</code></a> &#8211; Earlier in android GridView was the only widget to display grids, but now with RecyclerView, GridLayoutManager can be used to display grids.</li><li><a href="https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html"><code>StaggeredGridLayoutManager</code></a> &#8211; Another great customization is StaggeredGridLayoutManager, this is used to display a staggered grid.</li></ol>



<p class="wp-block-paragraph">In this Android RecyclerView Tutorial for simplicity I will be using a LinearLayoutManager for displaying a vertical list of items.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">Android RecyclerView ViewHolder</h3>



<p class="wp-block-paragraph">The concept of&nbsp;RecyclerView.ViewHolder is same as it was in the ListView. Simply said: when a view goes out of visible area it is kept for recycling. Earlier this was done through the custom view holder pattern. But now with RecyclerView a <a title="Android RecyclerView ViewHolder" href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html" target="_blank" rel="noopener noreferrer">ViewHolder</a> class is included in the adapter by default. Therefore now its a compulsion for everyone to implement this class. There are many advantages to this approach, like it keeps references to the views in your items, which in turn decreases the overhead of creating new references every time an item is displayed.</p>



<p class="wp-block-paragraph">Have a look at the object, that I will be using to supply data to ViewHolder:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.recyclerviewexample;

public class DataObject {
    private String mText1;
    private String mText2;

    DataObject (String text1, String text2){
        mText1 = text1;
        mText2 = text2;
    }

    public String getmText1() {
        return mText1;
    }

    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }

    public String getmText2() {
        return mText2;
    }

    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
}
</pre>



<p class="wp-block-paragraph">Also have please have look at the layout where RecyclerView needs to be added:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RecyclerViewActivity">

    &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

&lt;/RelativeLayout>
</pre>



<h3 class="wp-block-heading">Android RecyclerView Adapter</h3>



<p class="wp-block-paragraph">In general ListView implementations, the adapters were used to bind views with positions, usually in the <code>getView()</code> method. As developers we used to bind data into it. Here in RecyclerView the concept is same. But now the difference is, that new view holders are placed inside the adapter and the adapter now binds with ViewHolder, instead of views, as in previous adapters. In a way a direct binding between ViewHolder and position is made. To understand the concept mode deeply have a look at my custom implementation of <code>RecyclerView.Adapter</code> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.recyclerviewexample;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter&lt;MyRecyclerViewAdapter
        .DataObjectHolder&gt; {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList&lt;DataObject&gt; mDataset;
    private static MyClickListener myClickListener;

    public static class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            myClickListener.onItemClick(getAdapterPosition(), v);
        }
    }

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(ArrayList&lt;DataObject&gt; myDataset) {
        mDataset = myDataset;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_item, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());
    }

    public void addItem(DataObject dataObj, int index) {
        mDataset.add(dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}</pre>



<p class="wp-block-paragraph"></p>



<h4 class="wp-block-heading">Android RecyclerView onItemClick Event</h4>



<p class="wp-block-paragraph">Sadly the standard implementation of RecyclerView does not have an onItemClick implementation. Instead they support touch events by adding an <code>OnItemTouchListener</code> through the <code>addOnItemTouchListener</code> method of <code>RecyclerView</code> class.</p>



<p class="wp-block-paragraph">But I believe the suggested implementation is a little lengthy to implement. Also you may need to detect click events on the views in a RecyclerView item. Which may be a little difficult to implement through the standard <code>OnItemTouchListener.</code> Therefore I have implemented a custom recyclerview onitemclick event.</p>



<p class="wp-block-paragraph">Here in the above code sample, I defined a custom interface <code>MyClickListener</code> with a method <code>onItemClick</code>, which is used for detecting clicks on the activity. The only drawback of this approach is, that I had to make a static reference to <code>MyClickListener</code>, inside the adapter. Although it may not create any problems, but still to avoid them I suggest you to initialize this interface in <code>onResume()</code> method of your activity.</p>



<p class="wp-block-paragraph">The above code implementation will detect clicks on the item. If you need to detect clicks on particular views. It can be done simply by modifying the code in <code>DataObjectHolder</code> constructor. The layout for RecyclerView item:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="?android:attr/selectableItemBackground"
              android:orientation="vertical">

    &lt;TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="5dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    &lt;TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"/>
&lt;/LinearLayout></pre>



<p class="wp-block-paragraph">To display visual responses like ripples on screen when a click event is detected add&nbsp;<code>selectableItemBackground</code> resource in the layout (highlighted above). Next have a look at the main activity where all of this code would come into play:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.recyclerviewexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;


public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "RecyclerViewActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);
        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);

        // Code to Add an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);

        // Code to remove an item with default animation
        //((MyAdapter) mAdapter).deleteItem(index);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(
                new MyRecyclerViewAdapter.MyClickListener() {
                    @Override
                    public void onItemClick(int position, View v) {
                        Log.i(LOG_TAG, " Clicked on Item " + position);
                    }
                });
    }

    private ArrayList&lt;DataObject&gt; getDataSet() {
        ArrayList results = new ArrayList&lt;DataObject&gt;();
        for (int index = 0; index &lt; 20; index++) {
            DataObject obj = new DataObject("Some Primary Text " + index,
                    "Secondary " + index);
            results.add(index, obj);
        }
        return results;
    }
}
</pre>



<p class="wp-block-paragraph">In the above code sample I have defined a basic Android RecyclerView. Where I have also shown, how to add and remove items at run time from the adapter. The good part here is, you need not to worry about animation. They are added and removed with default animation. But the most annoying thing is how to add dividers and spaces between items in RecyclerView?</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">Android RecyclerView Spacing Between Items</h3>



<p class="wp-block-paragraph">One of the biggest problems you might face while implementing Android RecyclerView is the spacing between items. Yes its true RecyclerView by default does not give any support for displaying dividers between items. Although you can add dividers by using an object of&nbsp;<code>RecyclerView.ItemDecoration</code> class in <code>mRecyclerView.addItemDecoration(itemDecoration)</code> method. Earlier this item decoration had to be implemented by the user itself but now it has been included in the Support package itself by the name of&nbsp;<code>DividerItemDecoration</code> class. To have a look at the full working sample of Android RecyclerView, please checkout the link below:</p>



<p class="wp-block-paragraph" style="text-align:center"><a class="fasc-button fasc-size-large fasc-type-flat fasc-rounded-medium ico-fa fasc-ico-before fa-github" style="background-color: #0364af; color: #ffffff;" target="_blank" rel="noopener noreferrer" href="https://github.com/Truiton/RecyclerViewExample">Full Sample</a></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">After including divider decoration file and using it through the&nbsp;<code>addItemDecoration</code> method, your app would look something like this:</p>



<figure class="wp-block-video aligncenter"><video height="480" style="aspect-ratio: 320 / 480;" width="320" autoplay controls loop poster="http://www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-1.png" src="https://www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-Video.mp4"></video></figure>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In the above tutorial, I tried to make a RecyclerView inspired from the Gmail app. Although its not a complete implementation, but it gives a basic idea on how to use RecyclerView in Android. Hope this <a title="Android RecyclerView Tutorial" href="http://www.truiton.com/2015/02/android-recyclerview-tutorial/">Android RecyclerView Tutorial</a> helped you. To stay updated with latest Android tutorials and news connect with us on Facebook, Twitter and Google+.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2015/02/android-recyclerview-tutorial/">Android RecyclerView Tutorial</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2015/02/android-recyclerview-tutorial/feed/</wfw:commentRss>
			<slash:comments>17</slash:comments>
		
		<enclosure url="https://www.truiton.com/wp-content/uploads/2015/02/Android-RecyclerView-Tutorial-Video.mp4" length="306403" type="video/mp4" />

		<post-id xmlns="com-wordpress:feed-additions:1">852</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.truiton.com @ 2026-08-05 03:28:47 by W3 Total Cache
-->