Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we perform 2 different actions in Snack bar at a time in android?

I am creating an android application in which i want to use Snack Bar, In a that snack bar i want 2 different words on which we have to perform 2 different actions.

enter image description here

like image 482
Anjali Patel Avatar asked Dec 19 '16 13:12

Anjali Patel


People also ask

How do you stack snack bars?

Consecutive snackbars should appear above persistent bottom navigation. Don'tAvoid stacking snackbars on top of one another. Don'tDon't animate other components along with snackbar animations, such as the floating action button.

How do you show multiple snackbar in flutter?

First, add the material_snackbar package to your pubspec dependencies. For displaying a material snackbar you need to obtain the [MaterialSnackbarMessengerState] of the current [BuildContext] by using [MaterialSnackBarMessenger.

How do you use a snack bar?

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.


2 Answers

From the Google design specifications:

Each snackbar may contain a single action, neither of which may be “Dismiss” or “Cancel.”

For multiple actions, use a dialog.

like image 145
Elias N Avatar answered Sep 28 '22 01:09

Elias N


Thanks Shailesh, I had to modify the code in order to make it work for me.

my_snackbar.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal"     android:id="@+id/my_snackbar_layout"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@color/dark_grey"     android:padding="15dp">      <TextView         android:id="@+id/message_text_view"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight=".6"         android:gravity="center_vertical"         android:text="Two button snackbar"         android:textColor="@color/white"/>      <TextView         android:id="@+id/first_text_view"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight=".2"         android:gravity="center"         android:text="ONE"         android:textColor="#FFDEAD"/>      <TextView         android:id="@+id/second_text_view"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight=".2"         android:gravity="center"         android:text="TWO"         android:textColor="#FFDEAD"/>  </LinearLayout>  

In your activity call this method whenever you want to show the snackbar:

 private void showTwoButtonSnackbar() {      // Create the Snackbar     LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);     snackbar = Snackbar.make(this.findViewById(android.R.id.content), message, Snackbar.LENGTH_INDEFINITE);      // Get the Snackbar layout view     Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();      // Set snackbar layout params     int navbarHeight = getNavBarHeight(this);     FrameLayout.LayoutParams parentParams = (FrameLayout.LayoutParams) layout.getLayoutParams();     parentParams.setMargins(0, 0, 0, 0 - navbarHeight + 50);     layout.setLayoutParams(parentParams);     layout.setPadding(0, 0, 0, 0);     layout.setLayoutParams(parentParams);      // Inflate our custom view     View snackView = getLayoutInflater().inflate(R.layout.my_snackbar, null);      // Configure our custom view     TextView messageTextView = (TextView) snackView.findViewById(R.id.message_text_view);     messageTextView.setText(message);      TextView textViewOne = (TextView) snackView.findViewById(R.id.first_text_view);     textViewOne.setText("ALLOW");     textViewOne.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             Log.d("Allow", "showTwoButtonSnackbar() : allow clicked");             snackbar.dismiss();         }     });      TextView textViewTwo = (TextView) snackView.findViewById(R.id.second_text_view);     textViewTwo.setText("DENY");     textViewTwo.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             Log.d("Deny", "showTwoButtonSnackbar() : deny clicked");             snackbar.dismiss();         }     });      // Add our custom view to the Snackbar's layout     layout.addView(snackView, objLayoutParams);      // Show the Snackbar     snackbar.show(); } 

To get nav bar height:

public static int getNavBarHeight(Context context) {     int result = 0;     int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");     if (resourceId > 0) {         result = context.getResources().getDimensionPixelSize(resourceId);     }     return result; }  
like image 21
Ashwin Avatar answered Sep 28 '22 01:09

Ashwin