Here is simple way to implement snackbar in android
Step 1. Add support library 23 and compile your project with
compile 'com.android.support:appcompat-v7:23.0.1'
Step 2. Add coordinate layout in your activity file
<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 3. Now add following code in your MainActivity.java to implement snackbar
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();
}
}
Hope this will work for you.
For more android tutorial please follow this blog: Trinity Tuts
Snackbar
does not mean that Android 5.0 (or whatever it is numbered) will not haveSnackbar
. – Semibreve