Implementing the Snack Bar
First, we need to bring in the Design Support Library by adding the following to the dependencies in Grade Scripts > build.gradle (Module: app)
compile 'com.android.support:design:22.2.0'
Then we need to add the snack bar text to app > res > values > strings.xml:
<string name="snackbar_text">Hello</string>
Then we need to add a CoordinatorLayout (new in the Design Support Library) to app > res > layout > activity_main.xml:
<android.support.design.widget.CoordinatorLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:id="@+id/snackbarPosition"> </android.support.design.widget.CoordinatorLayout>
Finally, we implement the snack bar when clicking the "Hello" menu item in the toolbar in the MainActivity class.
if (id == R.id.action_hello) {
Snackbar
.make(findViewById(R.id.snackbarPosition), R.string.snackbar_text, Snackbar.LENGTH_LONG)
.show();
return true;
}
No comments:
Post a Comment