<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xml" href="https://mayojava.github.io/feed.xslt.xml"?><feed xmlns="http://www.w3.org/2005/Atom"><generator uri="http://jekyllrb.com" version="3.3.1">Jekyll</generator><link href="https://mayojava.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://mayojava.github.io/" rel="alternate" type="text/html" /><updated>2016-12-03T12:24:39+01:00</updated><id>https://mayojava.github.io//</id><title type="html">Mayowa Adegeye</title><subtitle>Programming adventures, Android, PHP, NodeJS, JavaScript, Python. Coding Tips and best practices.</subtitle><entry><title type="html">AnimatedVectorDrawable XML Bundles</title><link href="https://mayojava.github.io/android/animated-vector-drawable-xml-bundle/" rel="alternate" type="text/html" title="AnimatedVectorDrawable XML Bundles" /><published>2016-12-01T00:00:00+01:00</published><updated>2016-12-01T00:00:00+01:00</updated><id>https://mayojava.github.io/android/animated-vector-drawable-xml-bundle</id><content type="html" xml:base="https://mayojava.github.io/android/animated-vector-drawable-xml-bundle/">&lt;p&gt;Creating AnimatedVectorDrawables on Android requires you to create three xml resources that represents each of the following
&lt;code class=&quot;highlighter-rouge&quot;&gt;animated-vector&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;objectAnimator&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;vector&lt;/code&gt;. The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;vector /&amp;gt;&lt;/code&gt; is the actual file that represents the vector drawable.
The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;objectAnimator /&amp;gt;&lt;/code&gt; defines the animation to  apply to different parts of the vector and the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animated-vector /&amp;gt;&lt;/code&gt;
is used to link the vector drawable and the animation together.&lt;/p&gt;

&lt;p&gt;As an example, to create an animated vector drawable that transforms a play icon to stop icon, the following xml files will be created. The Vector drawable:&lt;/p&gt;

&lt;!-- more --&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- res/drawable/ic_play.xml --&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;vector&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:android=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48dp&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48dp&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:viewportWidth=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:viewportHeight=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

        &lt;span class=&quot;nt&quot;&gt;&amp;lt;path&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;play&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:pathData=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;M 20 15 L 32 24 L 20 33 Z&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:fillColor=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@android:color/white&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/vector&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then an animator that defines the transformation on the &lt;code class=&quot;highlighter-rouge&quot;&gt;pathData&lt;/code&gt; property of the vector and morphs it into the stop icon.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- res/animator/animates_play_to_stop.xml --&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:android=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;objectAnimator&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:duration=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;300&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:interpolator=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@android:anim/accelerate_decelerate_interpolator&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:propertyName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pathData&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:valueFrom=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;M 20 15 L 32 24 L 32 24 L 20 33 Z&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:valueTo=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;M 16 16 L 32 16 L 32 32 L 16 32 Z&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:valueType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pathType&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/set&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Finally, we create the AnimatedVectorDrawable xml file links the animation to the vector:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- res/drawable/animated_drawable_play_to_stop.xml --&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;animated-vector&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:android=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:drawable=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@drawable/ic_play&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

        &lt;span class=&quot;nt&quot;&gt;&amp;lt;target&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:animation=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@animator/animate_play_to_stop&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;play&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/animated-vector&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;At IO 2016, Google announced a new xml bundle format that enables creating AnimatedVectorDrawables using a single xml file.
The XML bundle format requires build tools version 24 and above to work, and also requires adding the &lt;code class=&quot;highlighter-rouge&quot;&gt;aapt&lt;/code&gt; namespace to your xml declaration.
For example, the three xml files for the AnimatedVectorDrawable above can be written in a single xml file as shown below:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- res/drawable/animated_drawable_play_to_stop.xml --&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;animated-vector&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:android=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;xmlns:tools=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/tools&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;xmlns:appt=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/aapt&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;tools:ignore=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;MissingPrefix&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

        &lt;span class=&quot;nt&quot;&gt;&amp;lt;appt:attr&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name =&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;android:drawable&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;vector&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:android=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;android:width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48dp&quot;&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;android:height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48dp&quot;&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;android:viewportWidth=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48&quot;&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;android:viewportHeight=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;48&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

                &lt;span class=&quot;nt&quot;&gt;&amp;lt;path&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;play&quot;&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:pathData=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;M 20 15 L 32 24 L 20 33 Z&quot;&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:fillColor=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@android:color/white&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/vector&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/appt:attr&amp;gt;&lt;/span&gt;

        &lt;span class=&quot;nt&quot;&gt;&amp;lt;target&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;android:name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;play&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;appt:attr&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;android:animation&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;objectAnimator&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:duration=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;300&quot;&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:interpolator=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@android:anim/accelerate_decelerate_interpolator&quot;&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:propertyName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pathData&quot;&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:valueFrom=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;M 20 15 L 32 24 L 32 24 L 20 33 Z&quot;&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:valueTo=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;M 16 16 L 32 16 L 32 32 L 16 32 Z&quot;&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;android:valueType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pathType&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/appt:attr&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/target&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;/animated-vector&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The keys things to note in this new format are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Inclusion of the &lt;code class=&quot;highlighter-rouge&quot;&gt;appt&lt;/code&gt; namespace inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animated-vector ..&lt;/code&gt; opening xml tag:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;        &lt;span class=&quot;nt&quot;&gt;&amp;lt;animated-vector&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:android=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;xmlns:appt=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/aapt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;android:drawable&lt;/code&gt; atttibute of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animated-vector&lt;/code&gt; has been removed and now you put the vector xml directly within this tag:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;        &lt;span class=&quot;nt&quot;&gt;&amp;lt;appt:attr&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name =&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;android:drawable&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
             &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- vector graphics xml --&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/appt:attr&amp;gt;&lt;/span&gt;
    &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;Finally, within each of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;tagret..&lt;/code&gt; tags, you can either directly specify the &lt;code class=&quot;highlighter-rouge&quot;&gt;objectAnimator&lt;/code&gt; wihtin an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;appt:attr name=&quot;android:animation&amp;gt;&lt;/code&gt;, or
reference an external animation located in &lt;code class=&quot;highlighter-rouge&quot;&gt;res/animator/..&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- directly specify the animation --&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;tagret&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;android:name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;play&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;appt:attr&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;android:animation&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;objectAnimator&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;.......&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/appt:attr&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;/target&amp;gt;&lt;/span&gt;

   &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- referenceing an external object animator --&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;tagret&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;android:name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;play&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:animation=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@animator/animate_play_to_stop&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You can add the &lt;code class=&quot;highlighter-rouge&quot;&gt;tools:ignore=&quot;MissingPrefix&quot;&lt;/code&gt; to suppress lint warnings because the bundle format is not yet recognized by lint.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://plus.google.com/+NickButcher/posts/A8KKxnJdg4r&quot;&gt;Nick Butcher Google Plus Post on New XML Bundle format&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.stylingandroid.com/animatedvectordrawable-bundles/&quot;&gt;AnimatedVectorDrawables Bundle&lt;/a&gt;&lt;/p&gt;</content><category term="Android" /><category term="AnimatedVectorDrawable" /><summary type="html">Creating AnimatedVectorDrawables on Android requires you to create three xml resources that represents each of the following
animated-vector , objectAnimator and vector. The &amp;lt;vector /&amp;gt; is the actual file that represents the vector drawable.
The &amp;lt;objectAnimator /&amp;gt; defines the animation to  apply to different parts of the vector and the &amp;lt;animated-vector /&amp;gt;
is used to link the vector drawable and the animation together.</summary></entry><entry><title type="html">Android support Annotations</title><link href="https://mayojava.github.io/android/android-support-annotations/" rel="alternate" type="text/html" title="Android support Annotations" /><published>2016-08-22T00:00:00+01:00</published><updated>2016-08-22T00:00:00+01:00</updated><id>https://mayojava.github.io/android/android-support-annotations</id><content type="html" xml:base="https://mayojava.github.io/android/android-support-annotations/">&lt;p&gt;In this blog post, we are going to be talking about the Android Support
Annotations Library and why we should care about it.&lt;/p&gt;

&lt;p&gt;Version 19.1 of the support library included a new annotations package which
contains a number of useful annotations that can be added to your code, to help
catch bugs. These annotations will help Android studio to check your code for
possible errors and report them to you. As of version 22.2 of the support
library, this annotations package has been enriched with an additional 13
annotations.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;adding-the-library-to-your-project&quot;&gt;Adding the Library to your project&lt;/h2&gt;
&lt;p&gt;The annotations library are not included in an Android project by default, as they
are shipped as a separate library. If you’re using &lt;code class=&quot;highlighter-rouge&quot;&gt;appcompat library&lt;/code&gt; however,
you already have access to the annotations, because appcompat itself depends on it.&lt;/p&gt;

&lt;p&gt;In any case, to have access to the support library annotations in your android
project, add this line to your module level &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;compile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support:support-annotations:&amp;lt;latest-library-version&amp;gt;'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;These annotations are grouped together based on similarities in their usage and
functionality and based on these grouping, we have the following classes of
annotations in the support annotations library:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Nullness Annotations&lt;/li&gt;
  &lt;li&gt;Resource Annotations&lt;/li&gt;
  &lt;li&gt;Thread Annotations&lt;/li&gt;
  &lt;li&gt;Value Constraints Annotations&lt;/li&gt;
  &lt;li&gt;Others are: Permissions Annotations, CheckResults Annotations and CallSuper
Annotations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;nullness-annotations&quot;&gt;Nullness Annotations&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@Nullable&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;@NonNull&lt;/code&gt; annotations can be used to check the nullness of a
variable, parameter or method return value. &lt;code class=&quot;highlighter-rouge&quot;&gt;@NonNull&lt;/code&gt; annotation means
that the value of the variable, parameter or method return value cannot be null.
Android studio generates a warning if this is violated. For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;//pass a null argument to a method annotated as @NonNull&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;doubleNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;doubleNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;@Nullable means a variable is allowed to take a null value and if used with a
method, it means the method can return a null value. Null check should be done
on variables or return values whenever &lt;code class=&quot;highlighter-rouge&quot;&gt;@Nullable&lt;/code&gt; annotation is used.&lt;/p&gt;

&lt;h3 id=&quot;resource-annotations&quot;&gt;Resource Annotations&lt;/h3&gt;
&lt;p&gt;Since resources are passed around in Android as integers, a code expecting a
string resource id can be passed an int pointing to a drawable and the compiler
will accept it. Resource type annotations allows type checking to be done in
situations like this. By annotating an int parameter with &lt;code class=&quot;highlighter-rouge&quot;&gt;@StringRes&lt;/code&gt; for
examples, ensures that passing anything other than a &lt;code class=&quot;highlighter-rouge&quot;&gt;R.string&lt;/code&gt; ref will be
flagged by the IDE.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setButtonText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@StringRes&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
         &lt;span class=&quot;c1&quot;&gt;//set text on some button&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//this will be flagged by the IDE&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;setButtonText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;drawable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Each of the android resource type has a corresponding resource type annotation.
Some other examples are &lt;code class=&quot;highlighter-rouge&quot;&gt;@DrawableRes&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;@ColorRes&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;@InterpolatorRes&lt;/code&gt; and so
on. The general rule is, if there is a resource of type &lt;strong&gt;“Foo”&lt;/strong&gt;, the corresponding
resource type annotation is &lt;strong&gt;“FooRes”&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Lastly, there is a special resource annotation called &lt;code class=&quot;highlighter-rouge&quot;&gt;@AnyRes&lt;/code&gt; which is used
when the specific type of resource is not known but it must be a resource type.&lt;/p&gt;

&lt;h3 id=&quot;thread-annotations&quot;&gt;Thread Annotations&lt;/h3&gt;
&lt;p&gt;Thread annotations checks if a method is called from a specific type of thread.
Supported thread annotations are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;@UiThread&lt;/li&gt;
  &lt;li&gt;@MainThread&lt;/li&gt;
  &lt;li&gt;@WorkerThread&lt;/li&gt;
  &lt;li&gt;@BinderThread&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@MainThread&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;@UiThread&lt;/code&gt; annotations are interchangeable and method calls
from either thread type are allowed for these annotations.&lt;/p&gt;

&lt;p&gt;A class can be annotated with a thread annotation if all methods in that class
are called from the same thread. A good example of the usage of thread
annotations is in &lt;code class=&quot;highlighter-rouge&quot;&gt;AsyncTask&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;    &lt;span class=&quot;nd&quot;&gt;@WorkerThread&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Result&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;doInBackground&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@MainThread&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onProgressUpdate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Progress&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;An error will be flagged if the &lt;code class=&quot;highlighter-rouge&quot;&gt;onProgressUpdate&lt;/code&gt; method is called from a
thread other than the main thread.&lt;/p&gt;

&lt;h3 id=&quot;value-constraints-annotations&quot;&gt;Value Constraints Annotations&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@IntRange&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;@FloatRange&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;@Size&lt;/code&gt; annotations are used to validate the values
of passed parameters.
&lt;code class=&quot;highlighter-rouge&quot;&gt;@IntRange&lt;/code&gt; annotation validates that the parameter is within the specified
range of integer values. The &lt;code class=&quot;highlighter-rouge&quot;&gt;setAlpha&lt;/code&gt; method below for example, ensures that
the alpha parameter value is between 0 and 255 inclusive.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setAlpha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@IntRange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//set alpha&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@FloatRange&lt;/code&gt; likewise checks that a parameter value is within a specified
range of floating point values. The &lt;code class=&quot;highlighter-rouge&quot;&gt;@Size&lt;/code&gt; annotation on the other hand, is
used to check the size of a collection or array, as well as the length of a
string. &lt;code class=&quot;highlighter-rouge&quot;&gt;@Size(min=1)&lt;/code&gt; annotation can be used to check that a collection is not
empty and &lt;code class=&quot;highlighter-rouge&quot;&gt;@Size(2)&lt;/code&gt; annotation checks that an array contains exactly two
values.&lt;/p&gt;

&lt;h3 id=&quot;checkresult-annotations&quot;&gt;CheckResult Annotations&lt;/h3&gt;
&lt;p&gt;This is used to ensure that the result of a method or its return value is
actually used. It’s main purpose is to help with cases where users of the API
may be confused and may think the method has a side effect. As explained in the
support annotations documentation, a good example of this is the &lt;code class=&quot;highlighter-rouge&quot;&gt;String.trim&lt;/code&gt;
method, which may be confusing to new Java developers, thinking that the method
call actually changes the string by removing whitespace. If the &lt;code class=&quot;highlighter-rouge&quot;&gt;String.trim&lt;/code&gt;
method is annotated with &lt;code class=&quot;highlighter-rouge&quot;&gt;@CheckResult&lt;/code&gt;, the IDE will flag uses of &lt;code class=&quot;highlighter-rouge&quot;&gt;String.trim&lt;/code&gt;
where the caller does not do something with the result.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;    &lt;span class=&quot;nd&quot;&gt;@CheckResult&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//remove whitespace from string&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hello world &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//this will make the IDE flag an error since the result from the @CheckResult&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//annotated method is not used&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Other support annotations worth looking at are &lt;code class=&quot;highlighter-rouge&quot;&gt;@CallSuper&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;@Keep&lt;/code&gt; and
&lt;code class=&quot;highlighter-rouge&quot;&gt;@RequiresPermission&lt;/code&gt;. For a complete list check the android developer reference
for &lt;a href=&quot;https://developer.android.com/reference/android/support/annotation/package-summary.html&quot;&gt;support annotations&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Using support annotation makes helps to make obvious how the code works. It makes
the code predictable and also makes it easy for other developers integrating your
code or when you have to make changes to the code some times in the future.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://developer.android.com/studio/write/annotations.html#adding-nullness&quot;&gt;Improve code Inspection with Annotations - Android developer doc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://tools.android.com/tech-docs/support-annotations&quot;&gt;Support Annotation documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://michaelevans.org/blog/2015/07/14/improving-your-code-with-android-support-annotations/&quot;&gt;http://michaelevans.org/blog/2015/07/14/improving-your-code-with-android-support-annotations/&lt;/a&gt;&lt;/p&gt;</content><category term="Android" /><category term="Android Support Annotations" /><summary type="html">In this blog post, we are going to be talking about the Android Support
Annotations Library and why we should care about it.</summary></entry><entry><title type="html">Bottom Sheets in Android</title><link href="https://mayojava.github.io/android/bottom-sheets-android/" rel="alternate" type="text/html" title="Bottom Sheets in Android" /><published>2016-06-29T00:00:00+01:00</published><updated>2016-06-29T00:00:00+01:00</updated><id>https://mayojava.github.io/android/bottom-sheets-android</id><content type="html" xml:base="https://mayojava.github.io/android/bottom-sheets-android/">&lt;p&gt;Bottom Sheet is a material design component, that was added to design support
library in version 23.2. Bottom sheet is a very simple window that displays from the
bottom of the screen, and can be used to reveal more content to the user.
Examples of bottom sheet can be seen in some of Google’s apps and widgets,
such as the Place Picker from the Places API.&lt;/p&gt;

&lt;p&gt;In this post, I will be showing how you can use bottom sheets in your apps.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;Bottom sheet can be a structural part of a UI or a temporary modal. To achieve
these, the design support library provides:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior&lt;/code&gt; to be used with &lt;code class=&quot;highlighter-rouge&quot;&gt;CoordinatorLayout&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetDialog&lt;/code&gt; which is a dialog with a bottom sheet behavior&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetDialogFragment&lt;/code&gt; which is an extension of &lt;code class=&quot;highlighter-rouge&quot;&gt;DialogFragment&lt;/code&gt;, that
creates a &lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetDialog&lt;/code&gt; instead of a standard dialog.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To get started with &lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheet&lt;/code&gt; in your apps, add the support library
dependecy to your &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;    &lt;span class=&quot;n&quot;&gt;compile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support:design:&amp;lt;latest-library-version&amp;gt;'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;creating-a-persistent-bottom-sheet&quot;&gt;Creating a Persistent Bottom Sheet&lt;/h2&gt;
&lt;p&gt;To create a bottom sheet as part of your interface layout,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Create a layout with &lt;code class=&quot;highlighter-rouge&quot;&gt;CoordinatorLayout&lt;/code&gt; as the root, then add the
the &lt;code class=&quot;highlighter-rouge&quot;&gt;view&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;layout&lt;/code&gt; for the bottom sheet, as a &lt;strong&gt;direct&lt;/strong&gt; child of the
&lt;code class=&quot;highlighter-rouge&quot;&gt;CoordinatorLayout&lt;/code&gt;. The bottom sheet layout must have &lt;code class=&quot;highlighter-rouge&quot;&gt;app:layout_behavior&lt;/code&gt;
attribute, with value &lt;code class=&quot;highlighter-rouge&quot;&gt;android.support.design.widget.BottomSheetBehavior&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xml&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1.0&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;support&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;design&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CoordinatorLayout&lt;/span&gt;
   &lt;span class=&quot;nl&quot;&gt;xmlns:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;
   &lt;span class=&quot;nl&quot;&gt;xmlns:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tools&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/tools&quot;&lt;/span&gt;
   &lt;span class=&quot;nl&quot;&gt;xmlns:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res-auto&quot;&lt;/span&gt;
   &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
   &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
   &lt;span class=&quot;nl&quot;&gt;tools:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;com.mayojava.sample.bottomsheetdemo.MainActivity&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

   &lt;span class=&quot;o&quot;&gt;&amp;lt;!--&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RelativeLayout&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddingBottom&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddingLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_horizontal_margin&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddingRight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_horizontal_margin&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddingTop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;......&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RelativeLayout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

   &lt;span class=&quot;o&quot;&gt;&amp;lt;!--&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RelativeLayout&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/linear_layout_bottom_sheet&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;220dp&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;app:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;behavior_peekHeight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;80dp&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;app:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;behavior_hideable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;app:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_behavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@string/string_bottom_sheet_behavior&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;elevation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/z_bottom_sheet&quot;&lt;/span&gt;
       &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@color/color_bottom_sheet&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TextView&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/text_view_sheet_title&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@string/text_pull_to_show_more&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;textSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/text_size_medium&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&amp;gt;&lt;/span&gt;

       &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TextView&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/text_view_more_content&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@string/text_more_contet_to_user&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;textSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/text_size_big&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;textStyle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bold&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_below&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/text_view_sheet_title&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddingLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddingRight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&amp;gt;&lt;/span&gt;

       &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@string/text_click_me&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_alignParentLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_marginLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_marginRight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;

           &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_marginTop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;android:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout_below&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/text_view_more_content&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&amp;gt;&lt;/span&gt;

   &lt;span class=&quot;o&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RelativeLayout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;support&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;design&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CoordinatorLayout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;For the bottom sheet to be displayable, we need a to create a
&lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior&lt;/code&gt;. This can be done by calling &lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior.from()&lt;/code&gt;,
passing a reference to the view with the bottom behavior attribute to the call.
This should be done from the &lt;code class=&quot;highlighter-rouge&quot;&gt;Activity&lt;/code&gt; that uses the bottom sheet.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;LinearLayout&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bottomSheetLayout&lt;/span&gt;  
    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LinearLayout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;findViewById&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;layout_bottom_sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//get bottom sheet behavior from bottom sheet view&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;BottomSheetBehavior&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bottomSheetBehavior&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BottomSheetBehavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mLayoutBottomSheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With a reference to the &lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior&lt;/code&gt; object, we can call the &lt;code class=&quot;highlighter-rouge&quot;&gt;setState&lt;/code&gt; method, passing different behavior constants, that toggles the state of the bottom sheet.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior.STATE_EXPANDED&lt;/code&gt;: Completely expands the bottom sheet.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior.STATE_HIDE&lt;/code&gt;: Completely hides the bottom sheet.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior.STATE_COLLAPSED&lt;/code&gt;: Sets the bottom sheet height with the value set on the &lt;code class=&quot;highlighter-rouge&quot;&gt;peekHeight&lt;/code&gt; attribute.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;    
&lt;span class=&quot;c1&quot;&gt;//to expand the bottom sheet&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mbottomSheetBehavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BottomSheetBehavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;STATE_EXPANDED&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//to collapse the bottom sheet&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mbottomSheetBehavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BottomSheetBehavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;STATE_COLLAPSED&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;peekHeight&lt;/code&gt; can be used to show a preview of the bottom sheet when the screen is displayed. It can be set in xml using the &lt;code class=&quot;highlighter-rouge&quot;&gt;app:behavior_peekHeight&lt;/code&gt; attribute, or in code, by calling &lt;code class=&quot;highlighter-rouge&quot;&gt;setPeekHeight&lt;/code&gt; on the &lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetBehavior&lt;/code&gt; object.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;mbottomSheetBehavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setPeekHeight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;320&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;creating-a-modal-bottom-sheet&quot;&gt;Creating a Modal Bottom Sheet&lt;/h2&gt;
&lt;p&gt;A modal bottom sheet is a dialog with a bottom sheet behavior, and is not part of your view hierarchy. It can be used as alternative to menus, content choosers or simple dialogs in your application. It can also present deep-linked content from other apps.&lt;/p&gt;

&lt;p&gt;To do this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Create a class that extends &lt;code class=&quot;highlighter-rouge&quot;&gt;BottomSheetDialogFragment&lt;/code&gt;, inflated with the layout that will be used as the content of the modal dialog.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;layout_custom_bottom_sheet.xml&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;RelativeLayout&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:android=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;android:layout_width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;android:layout_height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;android:paddingLeft=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;android:paddingRight=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;android:paddingBottom=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;android:paddingTop=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;ImageView&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/ic_image&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@mipmap/ic_launcher&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_marginRight=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/margin_normal&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_marginTop=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;TextView&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:text=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello bottom sheet&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:textSize=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/text_size_medium&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_marginTop=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_toRightOf=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/ic_image&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_alignTop=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/ic_image&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;TextView&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_marginTop=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/activity_vertical_margin&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_below=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/ic_image&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:text=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@string/text_lorem_ipsum&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:lineSpacingExtra=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/margin_small&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_marginBottom=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@dimen/margin_normal&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;


&lt;span class=&quot;nt&quot;&gt;&amp;lt;/RelativeLayout&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;Create an instance of the modal bottom sheet and show it with the &lt;code class=&quot;highlighter-rouge&quot;&gt;show&lt;/code&gt; method, passing the &lt;code class=&quot;highlighter-rouge&quot;&gt;FragmentManager&lt;/code&gt; and a string &lt;code class=&quot;highlighter-rouge&quot;&gt;tag&lt;/code&gt; as parameters.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;CustomBottomSheetDialog&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bottomSheetDialog&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CustomBottomSheetDialog&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInstance&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bottomSheetDialog&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getSupportFragmentManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Custom Bottom Sheet&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;img src=&quot;/images/result_bottom_sheet_2.png&quot; alt=&quot;Alt bottom sheet screen 1&quot; /&gt;&lt;/td&gt;
      &lt;td&gt;&lt;img src=&quot;/images/result_bottom_sheet.png&quot; alt=&quot;Alt bottom sheet screen 2&quot; /&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The complete code for this tutorial can be found in this &lt;a href=&quot;https://github.com/mayojava/BottomSheetDemo&quot;&gt;github repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Bottom sheets can be used to display unobtrusive dialogs, as well as provide users with context menus or pickers in a very subtle way.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;http://www.hidroh.com/2016/06/17/bottom-sheet-everything/?utm_source=androiddevdigest&quot;&gt;http://www.hidroh.com/2016/06/17/bottom-sheet-everything/?utm_source=androiddevdigest&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;http://www.materialdoc.com/bottom-sheets/&quot;&gt;http://www.materialdoc.com/bottom-sheets/&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://material.google.com/components/bottom-sheets.html&quot;&gt;https://material.google.com/components/bottom-sheets.html&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><category term="Android" /><category term="Support Library" /><category term="Bottom Sheets" /><summary type="html">Bottom Sheet is a material design component, that was added to design support
library in version 23.2. Bottom sheet is a very simple window that displays from the
bottom of the screen, and can be used to reveal more content to the user.
Examples of bottom sheet can be seen in some of Google’s apps and widgets,
such as the Place Picker from the Places API.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://mayojava.github.io/%7B%22feature%22=%3E%22bottom-sheet.png%22,%20%22credit%22=%3E%22gunhansancar%22,%20%22creditlink%22=%3E%22http://gunhansancar.com/bottom-sheets-in-android/%22%7D" /></entry><entry><title type="html">Creating and sharing your own Android Library</title><link href="https://mayojava.github.io/android/library/creating-and-distributing-your-own-android-library/" rel="alternate" type="text/html" title="Creating and sharing your own Android Library" /><published>2016-06-16T00:00:00+01:00</published><updated>2016-06-16T00:00:00+01:00</updated><id>https://mayojava.github.io/android/library/creating-and-distributing-your-own-android-library</id><content type="html" xml:base="https://mayojava.github.io/android/library/creating-and-distributing-your-own-android-library/">&lt;p&gt;If you have ever thought of writing your own custom &lt;code class=&quot;highlighter-rouge&quot;&gt;Android library&lt;/code&gt; for others
to use or extend, and wonder how you could achieve that, then you can join me on
this ride. In this post, I will be showing a step by step guide on how to create
an &lt;code class=&quot;highlighter-rouge&quot;&gt;Android library&lt;/code&gt;. I will also be showing you how to distribute your library
through &lt;a href=&quot;https://jitpack.io/&quot;&gt;JitPack&lt;/a&gt;.&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;We will be creating a simple statistics library, that can be used to compute basic
&lt;code class=&quot;highlighter-rouge&quot;&gt;Arithmetic Mean&lt;/code&gt;,  &lt;code class=&quot;highlighter-rouge&quot;&gt;Median&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Mode&lt;/code&gt;. To achieve this, we follow the steps
below:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Create a new Android app that showcases the library&lt;/li&gt;
  &lt;li&gt;Create a new library module within the Android app&lt;/li&gt;
  &lt;li&gt;Write the code for your custom library&lt;/li&gt;
  &lt;li&gt;Add the library as a dependency to the project&lt;/li&gt;
  &lt;li&gt;Publish your library to &lt;a href=&quot;https://github.com/&quot;&gt;Github&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Setup to share your library with any other android project using &lt;code class=&quot;highlighter-rouge&quot;&gt;Jitpack&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;create-a-new-android-project&quot;&gt;Create a new Android project&lt;/h3&gt;
&lt;p&gt;Create a new Android project using the create new project wizard on Android studio.
This project will be used to test your library and also for later showcasing it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/new_project_wizard.png&quot; alt=&quot;Alt create new android project&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;create-a-new-library-module&quot;&gt;Create a new library module&lt;/h3&gt;
&lt;p&gt;Next is to create a new &lt;code class=&quot;highlighter-rouge&quot;&gt;Android libraray&lt;/code&gt; module within the created Android app above.&lt;/p&gt;

&lt;p&gt;Right-click the project &lt;strong&gt;new -&amp;gt; module&lt;/strong&gt; then select &lt;code class=&quot;highlighter-rouge&quot;&gt;Android Library&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/new_android_module.png&quot; alt=&quot;Alt create new android project&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Give your library a name on the next screen and click finish.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/library_name_wizard.png&quot; alt=&quot;Alt create new android project&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once the project is done building, we should see our newly created module in the project.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/module.png&quot; alt=&quot;Alt create new android project&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;write-library-code&quot;&gt;Write library code&lt;/h3&gt;
&lt;p&gt;Our library for now will have just one class. &lt;code class=&quot;highlighter-rouge&quot;&gt;CentralTendency.java&lt;/code&gt;. Create this
file in the &lt;code class=&quot;highlighter-rouge&quot;&gt;java&lt;/code&gt; folder under the library module.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// contains functions to computer Arithmetic mean, median and mode&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CentralTendency&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;/**
     * Computes the arithmetic mean of a set of numbers
     * @param numbers
     * @return
     */&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;arithmeticMean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collection&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//check type of values&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;firstNumberInCollection&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;firstNumberInCollection&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;computeMeanInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;firstNumberInCollection&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;computeMeanLong&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;computeMeanDouble&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;o&quot;&gt;...............&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;...............&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/**
     * computes arithmetic mean for a set of Integer values
     *
     * @param numbers
     * @return
     */&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;computeMeanInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collection&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;hasNext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;intValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check this &lt;a href=&quot;https://github.com/mayojava/statlib&quot;&gt;github repo&lt;/a&gt; for the complete code.&lt;/p&gt;

&lt;h3 id=&quot;add-the-library-module-as-a-dependency-of-the-android-project&quot;&gt;Add the library module as a dependency of the android project&lt;/h3&gt;
&lt;p&gt;Go to &lt;strong&gt;File -&amp;gt; Project Structure&lt;/strong&gt;, click on the app module and select the &lt;strong&gt;Dependencies&lt;/strong&gt; tab. Use the (&lt;strong&gt;+&lt;/strong&gt;) icon at the bottom of the window to add a new &lt;code class=&quot;highlighter-rouge&quot;&gt;module dependency&lt;/code&gt;. Select the newly created library.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/add_module_dependecy.png&quot; alt=&quot;Alt create new android project&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Click the &lt;code class=&quot;highlighter-rouge&quot;&gt;ok&lt;/code&gt; button when done. The dependency should be automatically added to the &lt;code class=&quot;highlighter-rouge&quot;&gt;app&lt;/code&gt; module &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;compile&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;project&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;':statlib'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In the &lt;code class=&quot;highlighter-rouge&quot;&gt;onCreate&lt;/code&gt; method of our &lt;code class=&quot;highlighter-rouge&quot;&gt;MainActivity&lt;/code&gt;, we add the code to showcase this library.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;nextInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CentralTendency&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;arithmeticMean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;doubleValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CentralTendency&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CentralTendency&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Numbers:\n\n%s\nMean: %.1f\nMedian: %.1f\nMode: %s\n&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;textView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;publish-your-library-to-github&quot;&gt;Publish your library to Github&lt;/h3&gt;
&lt;p&gt;Now that our app is ready, we need to upload to &lt;code class=&quot;highlighter-rouge&quot;&gt;Github&lt;/code&gt;. Init a git repository in the project root, create a new remote git repository on &lt;a href=&quot;htpps://github.com&quot;&gt;Github&lt;/a&gt; and push all committed codes to the remote repo. If you are not familiar with git, you can learn about git &lt;a href=&quot;https://try.github.io/levels/1/challenges/1&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;setup-and-share-your-library-through-jitpack&quot;&gt;Setup and share your library through Jitpack&lt;/h3&gt;
&lt;p&gt;Jitpack is an easy to use package repository for Git. It allows you share your library with anyone by just adding the dependency of your library to their projects. We need to setup our library for &lt;code class=&quot;highlighter-rouge&quot;&gt;Jitpack&lt;/code&gt; by adding some few lines to our gradle files.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Add the &lt;a href=&quot;https://github.com/dcendents/android-maven-gradle-plugin&quot;&gt;android-maven&lt;/a&gt; plugin. In the root &lt;code class=&quot;highlighter-rouge&quot;&gt;build.grade&lt;/code&gt; file, add:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;buildscript&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;dependencies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;classpath&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.github.dcendents:android-maven-gradle-plugin:1.3'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;In the library module &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file, add the following below the first apply plugin line:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;apply&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;plugin:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.github.dcendents.android-maven'&lt;/span&gt;  

&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'com.github.YourUsername'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;Create a Github release or add a &lt;a href=&quot;https://git-scm.com/book/en/v2/Git-Basics-Tagging&quot;&gt;TAG&lt;/a&gt;
In the project root, run the command below to create v1.0 tag for newly created library:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;git tag -a v1.0 -m “statlib version 1.0”
git push origin v1.0&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;Ensure that you have &lt;code class=&quot;highlighter-rouge&quot;&gt;Gradle Wrapper&lt;/code&gt; in your repository. If you don’t then create it using the command &lt;code class=&quot;highlighter-rouge&quot;&gt;gradle wrapper&lt;/code&gt; and commit it. When this is done, run this command in the project root:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;./gradlew install&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This will install this library in the local maven repository &lt;code class=&quot;highlighter-rouge&quot;&gt;($HOME/.m2/repository)&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If the build is successful and the project has also been uploded to &lt;a href=&quot;https://github.com&quot;&gt;Github&lt;/a&gt;, go to this &lt;code class=&quot;highlighter-rouge&quot;&gt;jitpack&lt;/code&gt; url:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;//Replace User with your Github username, and Repo with your library repo name

https://jitpack.io/#User/Repo&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;On the website, select the release you would like to share by clicking the &lt;code class=&quot;highlighter-rouge&quot;&gt;Get It&lt;/code&gt; button next to the list of releases of your repository.&lt;/p&gt;

&lt;h3 id=&quot;add-a-readme-file&quot;&gt;Add a README file&lt;/h3&gt;
&lt;p&gt;Automatically, Github looks for a file names &lt;code class=&quot;highlighter-rouge&quot;&gt;README.md&lt;/code&gt; in the root of your project and displays it below. At minimum, the README file should contain the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A badge with the status of your library repository.&lt;/li&gt;
  &lt;li&gt;A line describing how your library can be added to an android project.&lt;/li&gt;
  &lt;li&gt;Sample code showing how to use the library.&lt;/li&gt;
  &lt;li&gt;Finally, the library license usage. Check out this &lt;a href=&quot;https://help.github.com/articles/open-source-licensing/&quot;&gt;link&lt;/a&gt; for sample open source licensing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://jitpack.io/docs/&quot;&gt;https://jitpack.io/docs/&lt;/a&gt;&lt;/p&gt;</content><category term="Android" /><category term="Android Library" /><summary type="html">If you have ever thought of writing your own custom Android library for others
to use or extend, and wonder how you could achieve that, then you can join me on
this ride. In this post, I will be showing a step by step guide on how to create
an Android library. I will also be showing you how to distribute your library
through JitPack.</summary></entry><entry><title type="html">Using Java 8 Lambda expressions in Android</title><link href="https://mayojava.github.io/android/java/using-java8-lambda-expressions-in-android/" rel="alternate" type="text/html" title="Using Java 8 Lambda expressions in Android" /><published>2016-06-06T00:00:00+01:00</published><updated>2016-06-06T00:00:00+01:00</updated><id>https://mayojava.github.io/android/java/using-java8-lambda-expressions-in-android</id><content type="html" xml:base="https://mayojava.github.io/android/java/using-java8-lambda-expressions-in-android/">&lt;h2 id=&quot;what-are-lambda-expressions&quot;&gt;What are Lambda expressions&lt;/h2&gt;

&lt;p&gt;Lambda Expressions are one of the most important features added to Java 8. Prior
to Lambda Expressions, implementing functional interfaces i.e interfaces with
only one abstract method has been done using syntax that may seem cumbersome and
unclear. In cases like this, what we are trying to do is pass a functionality
as an argument to a method, such as what happens when a button is clicked. Lambda
expressions enables you to do just that, in a way that is much more compact and
clear. In android for example, lambdas can replace anonymous inner
class when providing event listeners.&lt;/p&gt;

&lt;h2 id=&quot;syntax-of-lambda-expressions&quot;&gt;Syntax of Lambda Expressions&lt;/h2&gt;

&lt;p&gt;A lambda expression consist of the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A comma separated list of formal parameters enclosed in parentheses. The data
types of the parameters in a lambda expression can be omitted. Also the
parenthesis can be omitted if there is only one parameter. For example:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;TextView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;textView&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TextView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;findViewById&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;text_view&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;textView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setOnLongClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Long Click&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;The arrow token &lt;code class=&quot;highlighter-rouge&quot;&gt;-&amp;gt;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;A body which contains a single expression or a statement block. If a single
expression is specified, the java runtime evaluates the expression and then return
its value. To specify a statement block, enclose statements in curly braces &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;{}&quot;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;lambda-expressions-in-android&quot;&gt;Lambda Expressions in Android&lt;/h2&gt;
&lt;p&gt;To use Lambda Expressions and other Java 8 features in Android, you need to use
the new Jack toolchain. Open your module level &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file and add the
following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;android&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;defaultConfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;jackOptions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;compileOptions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sourceCompatibility&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JavaVersion&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;VERSION_1_8&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;targetCompatibility&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JavaVersion&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;VERSION_1_8&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;sync your &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file and if you are having any issue with build tools,
you may need to update &lt;code class=&quot;highlighter-rouge&quot;&gt;buildToolsVersion&lt;/code&gt; in your &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file to &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;24rc4&quot;&lt;/code&gt;
or just download the latest Android SDK Build-tools from the SDK Manager, under
the Tools (Preview channel).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/sdk_manager_build_tool.png&quot; alt=&quot;Alt sdk manager build tools&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;example&quot;&gt;Example&lt;/h2&gt;
&lt;p&gt;Adding a click listener to a button&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;without lambda expression&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;button&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;findViewById&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setOnClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;OnClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Toast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;makeText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Button clicked&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Toast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;LENGTH_LONG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;with lambda expressions&lt;/strong&gt;
It is as simple as:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;button&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;findViewById&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setOnClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Toast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;makeText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Button clicked&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Toast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;LENGTH_LONG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As we can see above, using lambda expressions makes implementing a functional
interface clearer and compact. Standard functional interfaces can be found in
the &lt;code class=&quot;highlighter-rouge&quot;&gt;java.util.function&lt;/code&gt; package [included in Java 8]. These interfaces can be
used as target types for lambda expressions and method references.&lt;/p&gt;

&lt;p&gt;Another way to have Java 8 features in your Android app is using the
RetroLambda plugin. You can check out the RetroLambda github repo &lt;a href=&quot;https://github.com/evant/gradle-retrolambda&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://developer.android.com/preview/j8-jack.html#configuration&quot;&gt;https://developer.android.com/preview/j8-jack.html#configuration&lt;/a&gt;
&lt;a href=&quot;https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html&quot;&gt;https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html&lt;/a&gt;&lt;/p&gt;</content><category term="Java 8" /><category term="Lambda Expressions" /><summary type="html">What are Lambda expressions</summary></entry><entry><title type="html">Android UI Instrumentation test with Espresso [Tutorial]</title><link href="https://mayojava.github.io/android/android-ui-instrumentation-test-with-espresso/" rel="alternate" type="text/html" title="Android UI Instrumentation test with Espresso [Tutorial]" /><published>2016-06-03T00:00:00+01:00</published><updated>2016-06-03T00:00:00+01:00</updated><id>https://mayojava.github.io/android/android-ui-instrumentation-test-with-espresso</id><content type="html" xml:base="https://mayojava.github.io/android/android-ui-instrumentation-test-with-espresso/">&lt;p&gt;This tutorial describes how to write instrumentation tests for your Android
application. It covers the usage of Espresso to write UI tests and how to run
these tests via Android studio.&lt;/p&gt;

&lt;h3 id=&quot;why-testing&quot;&gt;Why testing?&lt;/h3&gt;
&lt;p&gt;Writing and maintaining a good test suite containing different types of tests requires
a good amount of time and discipline on the part of the developer. However, the benefits
that comes with doing this includes the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Helps to ensure the correctness of the app features.&lt;/li&gt;
  &lt;li&gt;Helps to implement new features with confidence that the new changes will not break
existing working code.&lt;/li&gt;
  &lt;li&gt;Facilitates building of app features in modules and iterations especially when
tests are first written [TDD], then codes are written to validate the test.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;categories-of-android-unit-tests&quot;&gt;Categories of Android unit tests&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Local unit tests&lt;/strong&gt;: These are tests that runs on the JVM. They do not require any native
Android library to run. They are found in &lt;code class=&quot;highlighter-rouge&quot;&gt;app/src/test/java&lt;/code&gt; folder in the
&lt;strong&gt;Project perspective&lt;/strong&gt;. Unit test should amount to about 60-70% of the test code base.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Integration test&lt;/strong&gt;: Tests more than one individual module working together that
implements a complete functional part of an app. Integration test are located in
the &lt;code class=&quot;highlighter-rouge&quot;&gt;app/src/androidTest/java&lt;/code&gt; folder and requires the Android system to run. Should amount to
about 20% of the test code base.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Espresso is a testing framework contained in the Android Testing Support Library.
It provides APIs to simulate user interactions and write functional UI tests.&lt;/p&gt;

&lt;p&gt;Espresso tests are written based on what user might do while interacting with your app.
Basically, you:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Locate the desired UI element&lt;/li&gt;
  &lt;li&gt;Interact with the UI element or check its state.&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- more --&gt;

&lt;p&gt;Android Instrumentation tests are located in the &lt;code class=&quot;highlighter-rouge&quot;&gt;app/src/androidTest/java&lt;/code&gt; folder
and this is the location where you will be writing your tests.&lt;/p&gt;

&lt;p&gt;Espresso tests are composed of three major components which are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;ViewMatchers: These are collection of objects used to find the desired view in
the current view hierarchy. They are passed to the &lt;code class=&quot;highlighter-rouge&quot;&gt;onView&lt;/code&gt; method to locate and return
the desired UI element.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;ViewActions: They are used to perform actions such &lt;code class=&quot;highlighter-rouge&quot;&gt;click&lt;/code&gt; on views. They are
passed to the &lt;code class=&quot;highlighter-rouge&quot;&gt;ViewInteraction.perform()&lt;/code&gt; method.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;ViewAssertions: They are used to assert the state of the currently selected view.
They can be passed to the &lt;code class=&quot;highlighter-rouge&quot;&gt;ViewInteraction.check()&lt;/code&gt; method.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;my_view&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;// withId(R.id.my_view) - ViewMatcher&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;                  &lt;span class=&quot;c1&quot;&gt;// click() - ViewAction&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isDisplayed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()));&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;//matches(isDisplayed()) - ViewAssertion&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;setting-up-espresso-in-your-project&quot;&gt;Setting up Espresso in your project&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Download the latest Android Support Repository via the SDK Manager or Android Studio&lt;/li&gt;
  &lt;li&gt;Open your app’s build.gradle file, the one located in &lt;code class=&quot;highlighter-rouge&quot;&gt;app/build.gradle&lt;/code&gt; and add
the following lines inside dependencies:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Android JUnit Runner&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;androidTestCompile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support.test:runner:0.5'&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// JUnit4 Rules&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;androidTestCompile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support.test:rules:0.5'&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Espresso core&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;androidTestCompile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support.test.espresso:espresso-core:2.2.2'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;To that same &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; file, under &lt;code class=&quot;highlighter-rouge&quot;&gt;android.defaultConfig&lt;/code&gt;, add the following:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;testInstrumentationRunner&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;android.support.test.runner.AndroidJUnitRunner&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;sample-buildgradle-file&quot;&gt;Sample build.gradle file&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;apply&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;plugin:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.application'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;android&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;compileSdkVersion&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;buildToolsVersion&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;23.0.3&quot;&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;defaultConfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;applicationId&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;com.android.sample.espressouitestsample&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;minSdkVersion&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;targetSdkVersion&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;versionCode&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;versionName&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;1.0&quot;&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;testInstrumentationRunner&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;android.support.test.runner.AndroidJUnitRunner&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;dependencies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// App's dependencies, including test&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;compile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support:support-annotations:22.2.0'&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;androidTestCompile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support.test:runner:0.5'&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;androidTestCompile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support.test:rules:0.5'&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//Espresso core&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;androidTestCompile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'com.android.support.test.espresso:espresso-core:2.2.2'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;set-up-your-device-for-espresso-tests&quot;&gt;Set up your device for Espresso tests&lt;/h2&gt;
&lt;p&gt;To ensure that espresso works as expected on your test device or emulator, turn
off animations on your device. Navigate to &lt;em&gt;Settings -&amp;gt; Developer Options&lt;/em&gt; and turn
all the following off under &lt;strong&gt;Drawing&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Window animation scale&lt;/li&gt;
  &lt;li&gt;Transition animation scale&lt;/li&gt;
  &lt;li&gt;Animator duration scale&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A sample app showing a SignUp and Login screen will be used to demonstrate the
basic concepts of Espresso. To follow through with the snippets in the following
section, please clone this git repository.
&lt;code&gt;
git clone git@github.com:mayojava/espressoUIIntegrationTest.git
&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;now-lets-dive-in&quot;&gt;Now let’s dive in&lt;/h2&gt;
&lt;p&gt;The sample code has a main screen with a Login and SignUp button. The first test we
will be looking at, is to check that the SignUp screen shows up when the SignUp button
is clicked. In the &lt;code class=&quot;highlighter-rouge&quot;&gt;androidTest&lt;/code&gt; folder, open the &lt;code class=&quot;highlighter-rouge&quot;&gt;app/src/androidTest/java/.../mainscreen/MainScreenTest.java&lt;/code&gt; file. The first
thing to notice here are the annotations:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@RunWith(AndroidJUnit4.class)&lt;/code&gt;: Tags the class as an Android JUnit4 class. Espresso
unit test should be written as a JUnit 4 test class.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@LargeTest&lt;/code&gt;: Qualifies a test to run in &amp;gt;2s execution time, makes use
of all platform resources, including external communications. Other test qualifiers
are &lt;code class=&quot;highlighter-rouge&quot;&gt;@SmallTest&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;@MediumTest&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@Rule&lt;/code&gt;: &lt;code class=&quot;highlighter-rouge&quot;&gt;ActivityTestRule&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;ServiceTestRule&lt;/code&gt; are JUnit rules part of the
Android Testing Support Library and they provide more flexibility and reduce the
boilerplate code required in tests. These rules provides functional testing of a
single activity or service respectively.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;@Test&lt;/code&gt;: Each functionality to be tested must be annotated with this. The activity
under test will be launched before each of the test annotated with &lt;code class=&quot;highlighter-rouge&quot;&gt;@Test&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To test that the Signup screen shows up when the Signup button is clicked, we do
the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Locate the Signup button using the &lt;code class=&quot;highlighter-rouge&quot;&gt;withId&lt;/code&gt; ViewMatcher.&lt;/li&gt;
  &lt;li&gt;Perform a &lt;code class=&quot;highlighter-rouge&quot;&gt;click()&lt;/code&gt; action on the button.&lt;/li&gt;
  &lt;li&gt;Assert that the Signup screen is displayed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code for the steps above is shown in the snippets below:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;    &lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;clickSignUpButton_opensSignUpScreen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//locate and click on the login button&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;button_sign_up&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;//check if the sign up screen is displayed by asserting that the first name edittext is displayed&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;edit_text_first_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;allOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isDescendantOfA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;layout_sign_up&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isDisplayed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())));&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Next open the &lt;code class=&quot;highlighter-rouge&quot;&gt;SignUpScreenTest.java&lt;/code&gt; file under &lt;code class=&quot;highlighter-rouge&quot;&gt;app/src/androidTest/java/.../signup/SignUpScreenTest.java&lt;/code&gt;. Here we want to test that
on filling the signup form and clicking the Signup button, the Success screen is displayed with the signup success text, when the signup is complete.&lt;/p&gt;

&lt;p&gt;The code for the test above, follow the following steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Locate each of the sign up form text fields using the &lt;code class=&quot;highlighter-rouge&quot;&gt;withId&lt;/code&gt; ViewMatcher.&lt;/li&gt;
  &lt;li&gt;Perform &lt;code class=&quot;highlighter-rouge&quot;&gt;typeText()&lt;/code&gt; action on each, to type in the text for that field.&lt;/li&gt;
  &lt;li&gt;Locate sign up button and perform the &lt;code class=&quot;highlighter-rouge&quot;&gt;click()&lt;/code&gt; action on the button to submit
the form.&lt;/li&gt;
  &lt;li&gt;On sign up completion, assert that the sign up success screen is displayed with
the success text.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code for the steps above is shown below:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;clickSignUpButtonAfterFillingForm_showProgressAndSuccessScreen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Firstname&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;lastname&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;emailAddress&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;firstname.lastname@g.com&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;password&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//find the firstname edit text and type in the first name&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;edit_text_first_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeSoftKeyboard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//find the lastname edit text and type in the last name&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;edit_text_last_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeSoftKeyboard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//find the email address edit text and type in the email address&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;edit_text_email&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailAddress&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeSoftKeyboard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//find the password edit text and type in the password&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;edit_text_password&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeSoftKeyboard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//click the signup button&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;button_sign_up&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//check that we can see the success screen with success message&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InstrumentationRegistry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getTargetContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;text_sign_up_successful&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;onView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;text_view_status_message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;allOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;withText&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;successString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isDisplayed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For a list of possible &lt;code class=&quot;highlighter-rouge&quot;&gt;ViewMatchers&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;ViewActions&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;ViewAssertions&lt;/code&gt; check out the
&lt;a href=&quot;https://google.github.io/android-testing-support-library/docs/espresso/cheatsheet/index.html&quot;&gt;Espresso Cheat Sheet&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;running-espresso-tests&quot;&gt;Running Espresso tests&lt;/h2&gt;
&lt;p&gt;To run a single test class, right click on the test class file and select the &lt;strong&gt;‘Run [TestFileName]’&lt;/strong&gt; option.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/espresso_run_single_test_class.png&quot; alt=&quot;Alt run single test class&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To run many different tests at the same time, you will need to create a build configuration. This can be done easily in Android Studio by right clicking on the
package name under &lt;code class=&quot;highlighter-rouge&quot;&gt;androidTest /java/..&lt;/code&gt; and select &lt;strong&gt;“Create Tests in [package name]”&lt;/strong&gt;
option.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/espresso_create_build_configuration.png&quot; alt=&quot;Alt create build configuration&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Select the new run configuration from the run configuration menu and select run.
Tests results are displayed in the run panel at the bottom of the screen.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/espresso_test_run_result.png&quot; alt=&quot;Alt Test run result&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;references-and-further-readings&quot;&gt;References and further readings:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://google.github.io/android-testing-support-library/docs/espresso/index.html&quot;&gt;Espresso Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://google.github.io/android-testing-support-library/docs/espresso/intents/index.html&quot;&gt;Espresso Intents&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.android.com/topic/libraries/testing-support-library/index.html&quot;&gt;Android Testing Support Library&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex&quot;&gt;Android Testing Codelab&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.android.com/reference/android/support/test/espresso/matcher/ViewMatchers.html&quot;&gt;ViewMatchers&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://hamcrest.org/JavaHamcrest/&quot;&gt;Hamcrest Matchers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The sample code for this post can be found in this &lt;a href=&quot;https://github.com/mayojava/espressoUIIntegrationTest&quot;&gt;github repo&lt;/a&gt;.&lt;/p&gt;</content><category term="Android" /><category term="Unit test" /><category term="Espresso" /><summary type="html">This tutorial describes how to write instrumentation tests for your Android
application. It covers the usage of Espresso to write UI tests and how to run
these tests via Android studio.</summary></entry></feed>
