Can We Perform 2 Different Actions In Snack Bar At A Time In Android?


Answer :

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.


As @Elias N answer's each Snackbar may contain a single action. If you want to set more then action in Snackbar then you need to create your own layout. Please try this i hope this will help you.

Create one xml file my_snackbar.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="horizontal"               android:layout_width="match_parent"               android:layout_height="50dp"               android:background="#000000">     <TextView         android:layout_width="0dp"         android:layout_height="50dp"         android:layout_weight=".7"         android:gravity="center_vertical"         android:text="Please select any one"         android:textColor="@color/white"/>      <TextView         android:id="@+id/txtOne"         android:layout_width="0dp"         android:layout_height="50dp"         android:layout_weight=".1"         android:gravity="center"         android:text="ONE"         android:textColor="@color/red"/>     <TextView         android:id="@+id/txtTwo"         android:layout_width="0dp"         android:layout_height="50dp"         android:layout_weight=".1"         android:gravity="center"         android:text="TWO"         android:textColor="@color/red"/> </LinearLayout> 

Now in your activity file do the following code.

public void myCustomSnackbar() {     // Create the Snackbar     LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);     Snackbar snackbar = Snackbar.make(llShow, "", Snackbar.LENGTH_LONG);     // Get the Snackbar's layout view     Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();     layout.setPadding(0,0,0,0);     // Hide the text     TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);     textView.setVisibility(View.INVISIBLE);      LayoutInflater mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);     // Inflate our custom view     View snackView = getLayoutInflater().inflate(R.layout.my_snackbar, null);     // Configure the view     TextView textViewOne = (TextView) snackView.findViewById(R.id.txtOne);      textViewOne.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             Log.i("One", "First one is clicked");         }     });      TextView textViewTwo = (TextView) snackView.findViewById(R.id.txtTwo);     textViewTwo.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {         Log.i("Two", "Second one is clicked");         }     });      // Add the view to the Snackbar's layout     layout.addView(snackView, objLayoutParams);     // Show the Snackbar     snackbar.show(); } 

For more detail please read this documentation and here.


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; }  

Comments

Popular posts from this blog

Chemistry - Bond Angles In NH3 And NCl3

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?