Snackbar in Android
Snackbars are ( Android Material Design) just like a Toast . Snackbar provide action for interact . Snackbar will be display at the bottom of the screen and can be dispersed by swiping . Follow the bellow steps for create Snackbar in Android Material Design .
Steps 1: Create Project
a) Open Android Studio
b) Go to File >New> New Project > Project Name > Next > Next > Next > Finish
Steps 2: Add Design Library
Open build.gradle > add this line com.android.support:design:23.0.1
> Sync the gradle file .
Steps 3 : Layout Design
open activity_main.xml file and add the bellow code
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/llMain"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/holo_blue_dark"
tools:context="singhji.snackbar.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@android:color/holo_blue_bright"
android:text="Snackbar Example in Android " />
<Button
android:id="@+id/btnNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="Simple Snackbar"/>
<Button
android:id="@+id/btnAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="Snackbar With Action"/>
<Button
android:id="@+id/btnCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="Custom Snackbar"/>
</LinearLayout>
Step 4: Code
Open MainActivity class file and the bellow code .
package singhji.snackbar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.support.design.widget.Snackbar;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
Context mContext ;
Button btnNormal,btnAction,btnCustom;
Snackbar mSnackbar;
LinearLayout llMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
llMain=(LinearLayout)findViewById(R.id.llMain);
btnNormal=(Button)findViewById(R.id.btnNormal);
btnAction=(Button)findViewById(R.id.btnAction);
btnCustom=(Button)findViewById(R.id.btnCustom);
btnNormal.setOnClickListener(this);
btnAction.setOnClickListener(this);
btnCustom.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnNormal:
mSnackbar = Snackbar.make(llMain,"Welcome to Apna Android",Snackbar.LENGTH_LONG);
mSnackbar.show();
break;
case R.id.btnAction:
Snackbar snackba = Snackbar
.make(llMain, "Message is deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar1 = Snackbar.make(llMain, "Message is restored!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackba.show();
break;
case R.id.btnCustom:
Snackbar snackbar = Snackbar
.make(llMain, "No internet connection!", Snackbar.LENGTH_LONG)
.setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
// Changing message text color
snackbar.setActionTextColor(Color.RED);
// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
break;
}
}
}
Find Us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Comments
Post a Comment