Learn how to implement Snackbar in your android application. Snackbar is very easy to implement in your application it is very easy if you are using
appcompat-v7:23.0.1
Step 1. Create new project in android studio.
Step 2. Now open your build.gradle and check your Appcompat your appcompat version must be greater than 21 to implement SnackBar.
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.snackbarandroid" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1' }
Step 3. Now open your activity and add CoordinateLayout in it
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> </android.support.design.widget.CoordinatorLayout>
Step 4. Now add below code to in your Main_Activity.java file
public class MainActivity extends AppCompatActivity { CoordinatorLayout coordinatorLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout); ShowSnack(); } public void ShowSnack() { Snackbar snackbar = Snackbar.make(coordinatorLayout, "Snackbar Label", Snackbar.LENGTH_LONG); snackbar.setAction("Action", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Action", Toast.LENGTH_LONG).show(); } }); snackbar.setActionTextColor(Color.RED); View snackbarView = snackbar.getView(); snackbarView.setBackgroundColor(Color.DKGRAY); TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text); textView.setTextColor(Color.YELLOW); snackbar.show(); } }
In above code is very easy to understand, i create a separate method in which i create a snacbar and also i define color for my action and label in it.
🙂