Android Login Based on Hashmap Store & Retrieve

Android Hashmap Shared Preferences :

Every app which requires users to login will have to make sure they make the UI much flexible to the user so that they can have a hassle free login experience so that it is a add on to the quality of the app.

So lets begin this Android Login based on Hashmap Store & Retrieve, Shared Preferences tutorial.

You can refer android studio login example

1.Facebook login android

2. android login example with mysql database, php

3. android studio login and register 

http://www.androidcoding.in/2016/04/22/android-tutorial-login/

 

We have seen few apps giving a pin to login instead of username and password mainly in banking sector, and few more because it makes user login much simpler.In our next android studio login activity tutorial  we will be dealing with pin based login but for this tutorial  we will be dealing with autocomplete user selection.

What ever the style of login we need to take care of user authentication, i,e, the most important factor for an app.

 

If you are new for android autocompletetextview  visit

http://www.androidcoding.in/autocomplete/

 

LoginPojo

Create a pojo class for components

private String Username;
private String Password;

 

And generate getter and setter

 

public class LoginPojo

{

    private String Username;
    private String Password;

    public String getUsername() {
        return Username;
    }

    public void setUsername(String username) {
        Username = username;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }
}

Activity_login.Xml

Design a login panel by adding a

1)  android autocompletetextview –> for username

<AutoCompleteTextView
 android:id="@+id/username"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="Username"
 android:padding="10dp"
 android:textColor="@android:color/black"
 android:textColorHint="@android:color/darker_gray" />

 

2) android edittext –> for password

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:hint="Password"
    android:inputType="textPassword"
    android:padding="10dp"
    android:textColor="@android:color/black"
    android:textColorHint="@android:color/darker_gray" />

3) android checkbox –> for remember me functionality

<CheckBox
    android:id="@+id/rememberCheck"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Remember Me" />

 

4) Android Buttons for sign-in and sign-up

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="30dp"
    android:background="#fc668e"
    tools:context="com.androidcoding.storearraylist.LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@android:color/white"
        android:padding="30dp">

    <AutoCompleteTextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:padding="10dp"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/darker_gray" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Password"
        android:inputType="textPassword"
        android:padding="10dp"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/darker_gray" />

    <CheckBox
        android:id="@+id/rememberCheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Remember Me" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="signin"
            android:text="Sign-in" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="signup"
            android:text="Sign-up" />


    </LinearLayout>

    </LinearLayout>

</LinearLayout>

 

LoginActivity

Now lets start coding for the view

we are using android shared preferences(save data) and the showing them as suggestion in autocomplete textview.

 

Initialize all the components which we have specified in design.

Then in sign-up we will be checking if Remember me is checked or not

if checked

if (rememberCheck.isChecked()) {

    sharedPreferences = getSharedPreferences(key, MODE_PRIVATE);
    edit = sharedPreferences.edit();
    edit.putString(username, password);
    edit.commit();
}

 

Then as the user tries to sign-in load user data if exists using shared preferences as,

private void loaduserdata() {

    sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
    edit = sharedPreferences.edit();
    Map map = sharedPreferences.getAll();

    Set keySet = map.keySet();
    Iterator iterator = keySet.iterator();

    while (iterator.hasNext()) {
        LoginPojo credlist = new LoginPojo();
        String key = (String) iterator.next();
        String value = (String) map.get(key);

        credlist.setUsername(key);
        credlist.setPassword(value);

        this.credlist.add(credlist);
    }
    for (int i = 0; i < credlist.size(); i++) {
        LoginPojo bean = credlist.get(i);
    }

    List userList = getusers(credlist);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
    username.setAdapter(adapter);
    username.setThreshold(1);
    username.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> view, View arg1, int position, long arg3) {
            password.setText(credlist.get(position).getPassword());
        }
    });
}

private List getusers(ArrayList<LoginPojo> users) {
    List list = new ArrayList();
    for (LoginPojo c : users) {
        list.add(c.getUsername());
    }
    return list;

}

 

And now if user credentials is not remembered according to user wish we need to check whether user is logged in or not.

If user is registered move to next screen as a result of successful login.

 

for (LoginPojo d : credlist) {

    if (d.getUsername() != null && d.getUsername().contains(username.getText().toString())) {

        Intent i = new Intent(LoginActivity.this, HomeActivity.class);
        startActivity(i);

    }

 

 

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LoginActivity extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor edit;
    private AutoCompleteTextView username;
    EditText password;
    CheckBox rememberCheck;
    ArrayList<LoginPojo> credlist = new ArrayList<LoginPojo>();
    private ArrayAdapter<String> adapter;
    public static String key = "cred";


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

        username = (AutoCompleteTextView) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        rememberCheck = (CheckBox) findViewById(R.id.rememberCheck);

        loaduserdata();


    }

    public void signin(View view) {

        for (LoginPojo d : credlist) {

            if (d.getUsername() != null && d.getUsername().contains(username.getText().toString())) {

                Intent i = new Intent(LoginActivity.this, HomeActivity.class);
                startActivity(i);

            } 
        }


    }

    public void signup(View view) {

        String username = this.username.getText().toString();
        String password = this.password.getText().toString();

        if (rememberCheck.isChecked()) {

            sharedPreferences = getSharedPreferences(key, MODE_PRIVATE);
            edit = sharedPreferences.edit();
            edit.putString(username, password);
            edit.commit();
        }

        Toast.makeText(this, "Signed-Up Successfully", Toast.LENGTH_SHORT).show();

    }


    private void loaduserdata() {

        sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
        edit = sharedPreferences.edit();
        Map map = sharedPreferences.getAll();

        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();

        while (iterator.hasNext()) {
            LoginPojo credlist = new LoginPojo();
            String key = (String) iterator.next();
            String value = (String) map.get(key);

            credlist.setUsername(key);
            credlist.setPassword(value);

            this.credlist.add(credlist);
        }
        for (int i = 0; i < credlist.size(); i++) {
            LoginPojo bean = credlist.get(i);
        }

        List userList = getusers(credlist);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
        username.setAdapter(adapter);
        username.setThreshold(1);
        username.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> view, View arg1, int position, long arg3) {
                password.setText(credlist.get(position).getPassword());
            }
        });
    }

    private List getusers(ArrayList<LoginPojo> users) {
        List list = new ArrayList();
        for (LoginPojo c : users) {
            list.add(c.getUsername());
        }
        return list;

    }


}

 

Activity_home.Xml

once the user logged in successfully we are showing a list of users logged in you make change functionality of this page accordingly.

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/signedusers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Signed Up Users"
        android:padding="10dp"
        android:textSize="18dp"
        android:gravity="center"
        android:layout_marginTop="1dp"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/signedusers"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:text="Logout"/>


</RelativeLayout>

 

HomeActivity

Initialize listview and shared preferences and fetch the list of users registered.

sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
edit = sharedPreferences.edit();
Map map = sharedPreferences.getAll();

Set keySet = map.keySet();
Iterator iterator = keySet.iterator();

while (iterator.hasNext())
{
    LoginPojo credlist =new LoginPojo();
    String key = (String) iterator.next();
    String value = (String)map.get(key);

    credlist.setUsername(key);
    credlist.setPassword(value);

    this.credlist.add(credlist);
}
for (int i = 0; i < credlist.size(); i++)
{
    LoginPojo bean= credlist.get(i);
}

List userList = getusers(credlist);

 

Set the listview to adapter

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, userList);
listView.setAdapter(adapter);

 

 

 

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Abhishek on 10/15/2017.
 */

public class HomeActivity extends AppCompatActivity{

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor edit;
    public static String key = "cred";
    ListView listView;
    private ArrayAdapter<String> adapter;
    ArrayList<LoginPojo> credlist =new ArrayList<LoginPojo>();

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

        listView = (ListView) findViewById(R.id.listView);

        sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
        edit = sharedPreferences.edit();
        Map map = sharedPreferences.getAll();

        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();

        while (iterator.hasNext())
        {
            LoginPojo credlist =new LoginPojo();
            String key = (String) iterator.next();
            String value = (String)map.get(key);

            credlist.setUsername(key);
            credlist.setPassword(value);

            this.credlist.add(credlist);
        }
        for (int i = 0; i < credlist.size(); i++)
        {
            LoginPojo bean= credlist.get(i);
        }

        List userList = getusers(credlist);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, userList);
        listView.setAdapter(adapter);

    }

    private List getusers(ArrayList<LoginPojo> users) {
        List list = new ArrayList();
        for (LoginPojo c : users) {
            list.add(c.getUsername());
        }
        return list;

    }

 

Output :

Android Google Firebase Analytics

 

Google analytics android :

Hi, everyone we go through a lot of apps and websites these days, they may be related to different domains, may be no way related to each other but every website or app deals with a important topic called as “analytics”.

Today we are dealing with google analytics android tutorial, Yes this may not be a new word for few but in this google analytics android tutorial i am targeting who are new to this terminology. 

Analytics

Analytics is the phenomena where the statistics of the app or website is closely observed so that the audience, their interest can be noticed by respective admins.

So here lies the point that analytics not only helps to know the audience count but also improves the overall performance of app/website in terms  of tracking bugs in time as well getting notified with reviews, and few more which may help development.

Much More Simple 

So now have you ever noticed popular website like Flipkart, Amazon, snapdeal, and many more shopping portals and their apps starts showing you products which you have stared at, purchased, or looking for  lot once you visited them.

Now, i think you understood now that by this simple example its a part of analytics and also its not that simple as it appears but lets see it in our coming tutorial.

Is This For Everyone ?

When you are trying to build an app/website, ok think its done and now you want to know  who visited them and what have they been going through, how much time they are spending on your site exactly on which page this is what we are going to deal with.

Also from where i.e, country, device, service provider, gender, number of visits per hour,  day, week, monthly like what not you can know all these scenarios and much more google analytics android tutorial.

 

Let’s Get started :

Are you planning to start developing a new app or already having an app so this android app development tutorial is very much useful for you, lets get started today we will deal with a simple example by which we can trace out analytics of our app.

We will be using Google Firebase for tracing out app’s analytics.


Visit https://firebase.google.com    to get started

Then login in with your credentials 

Then Add a project

Select Android Project 

Add Firebase to your android app by mentioning package name, app name, and SHA-1.

The process of retrieving SHA-1 key is shown below 

Download the json file and add it to your android studio project.

Then add dependencies

 

build.gradle (Project: Firebase Tutorial)

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
    classpath 'com.google.gms:google-services:3.0.0'
}

 

build.gradle (Module: app)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.google.android.gms:play-services:10.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
    testCompile 'junit:junit:4.12'
    apply plugin: 'com.google.gms.google-services'
}

 

In MainActivity add FirebaseAnalytics as

private FirebaseAnalytics firebaseAnalytics;

 

Then initialize

firebaseAnalytics = FirebaseAnalytics.getInstance(this);

 

Add a log event

firebaseAnalytics.logEvent("MainActivity", new Bundle());

 

Don’t forget to add internet permission to your manifest file

<uses-permission android:name="android.permission.INTERNET" />

  

And that’s it now after 24 hours you will be able to track your audience

 

Android Marshmallow Permissions | Requesting Run-Time Permissions

 

Introduction

Now a days you can notice permissions popping up in android marshmallow phones (ver 6.0 android) when you deny permission then you might have notice a error when you try to make a call, read a message, access your contacts, camera, and few more services for your app in Android Marshmallow 6.0 (API level 23).

Want to know the reason behind it then this android marshmallow development tutorial is for you.

Android Marshmallow has got some advanced security features so as to make sure that user know what type of android marshmallow permission a app require while user try to use them i.e., when ever you try to open a app for first time it will ask you few permissions depending upon the app functionality which we will cover in this  android marshmallow tutorial.

Earlier versions of android before to lollipop might have not requested you for permission but are included in android marshmallow features yes its a safety feature for controlling user data from being taken away without any user notice as in earlier cases but now are being controlled in android 6.0 phones. 

By now you might got  an idea what would it look like so you need to few more things as all permissions are not asked in this manner only few are listed out and which are termed to be dangerous permissions and which are to be notified to user and after getting permission will move further.

 

Want to know more information regarding this topic

Requesting Permissions at Run Time

 

 

I will show you how to make a call using a permission window in android marshmallow in this tutorial.

 

MainActivity.java :

 

Here in this tutorial we need not required to design a screen as we are not performing any user clickable functionality as the app starts up it will ask a permission for making a call through the device.

 

Declare a method call() in which call functionality is implemented.

 

void call() {

        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:123" ));
        startActivity(intent);

    }

 

And in oncreate we will check whether permission is granted or not if not granted we need to ask for permission.

 

void checkCallPermission() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {

            requestCallPermission();

        } else {

            call();
        }
    }

 

And permission is not granted the we need to request for permission as

 

private void requestCallPermission() {


        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},
                REQUEST_CALL);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CALL) {


            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                call();

            } else {

                Toast.makeText(MainActivity.this,"You need to grant call permission to make a call",Toast.LENGTH_LONG).show();
            }

        }
    }

 

Full Code

 

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    private static final int REQUEST_CALL = 0;

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

        checkCallPermission();

    }


    void call() {

        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:123" ));
        startActivity(intent);

    }



    private void requestCallPermission() {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},
                REQUEST_CALL);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CALL) {

            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                call();

            } else {

                Toast.makeText(MainActivity.this,"You need to grant call permission to make a call",Toast.LENGTH_LONG).show();
            }

        }
    }

    void checkCallPermission() {
      
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {


            requestCallPermission();


        } else {

            call();

        }
    }
}

 

And when you run this code you will be prompted to accept a android marshmallow permission regarding call and will make a call if you permitted.

AndroidManifest.xml

 

Add call permission in manifest file

 

<uses-permission android:name="android.permission.CALL_PHONE" />

 

If you have any query’s in the android marshmallow permission tutorial let us know in comment section below, like, share this tutorial for more interesting tutorials.

Android Snackbar Tutorial With Material Design

 In previous posts we have seen toast to display a message, alert box or dialog box to display a message the message may be output or may be sometimes system error depending upon requirement but now we have similar display system called as Snackbar.

Android Snackbar :

Snackbar displays a piece of message at the bottom of the screen which appears much more stylish than the normal toast so we will see it in the present tutorial.

This Snackbar is made available with latest api supporting design.This is a material design  available for providing high end design. 

Add support:design which is required for CoordinatorLayout whic is used to display a Snackbar in android add it to your gradle file as

 Dependency’s

Try to check for the latest version of dependency’s before implementing

    compile 'com.android.support:design:23.0.1'

snackbar.xml : 

Adding a CoordinatorLayout to the activity file and also a button. This CoordinatorLayout  helps in displaying a message and also a action button which will perform a desired action on user click.

CoordinatorLayout :

<android.support.design.widget.CoordinatorLayout
        android:id="@+id/CoordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.design.widget.CoordinatorLayout>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SnackBarActivity">


    <Button
        android:id="@+id/SnackBarbut"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show SnackBar" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/CoordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

 

SnackBarActivity.java :

Firstly initialize snackbarCoordinatorLayout

snackbarCoordinatorLayout = (CoordinatorLayout)findViewById(R.id.CoordinatorLayout);

And now initialize button and also set on click listener

        btnShowSnackBar = (Button)findViewById(R.id.SnackBarbut);
        btnShowSnackBar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(snackbarCoordinatorLayout, "AndroidCoding.in",
                        Snackbar.LENGTH_LONG);

                snackbar.setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SnackBarActivity.this, "Android Coding", Toast.LENGTH_LONG).show();
                    }
                });

 

As you can notice we have also given time duration as Long for the snack bar from below line. 

Snackbar snackbar = Snackbar.make(snackbarCoordinatorLayout, "Snackbar",
                        Snackbar.LENGTH_LONG);

 

And now we can set the Action for the Snackbar this is the main feature as i consider because in toast there is no button for user to accept it or perform any action but in Snackbar it is accepting a action by which you can get a piece of work done a s you can move from activity to another or sny thing which you want it be done when you click on that action button and the coding for it goes on like this

snackbar.setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SnackBarActivity.this,"snackbar OK clicked", 
                           Toast.LENGTH_LONG).show();
                    }
                });

At last don’t forget to add this line as this plays a crucial role in showing a Snackbar on user screen i.e., show functionality and its not anything new you have seen it for Toast, AlertBox and  a few more….

  snackbar.show();

 

package com.androidcoding.snackbar;

import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SnackBarActivity extends AppCompatActivity {

    Button btnShowSnackBar;
    CoordinatorLayout snackbarCoordinatorLayout;

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

        snackbarCoordinatorLayout = (CoordinatorLayout)findViewById(R.id.CoordinatorLayout);

        btnShowSnackBar = (Button)findViewById(R.id.SnackBarbut);
        btnShowSnackBar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(snackbarCoordinatorLayout, "Snackbar",
                        Snackbar.LENGTH_LONG);

                snackbar.setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(
                                SnackBarActivity.this,
                                "snackbar OK clicked",
                                Toast.LENGTH_LONG).show();
                    }
                });

                snackbar.show();
            }

        });
    }

}

Output :

The screen depicts Android Snackbar

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...