Reputation: 206
I have created my own class to mimick the Snackbar, lets call it CustomSnackbar. What I am trying to achieve is to customize the snackbar and be able to call CustomSnackbar from my main activity and for the usage to be very similar to calling the standard Snackbar. For the purpose of demonstrating my example without all the bulk code, here is my CustomSnackbar class:
package com.wizzkidd.myapp.helpers;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomSnackbar {
Context _context;
Snackbar snackbar;
public CustomSnackbar(Context context) {
this._context = context;
}
public void make(View view, CharSequence text, int duration) {
snackbar = Snackbar.make(view, "", duration);
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbar.getView();
TextView textView = (TextView) snackbarLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE); //hide the default snackbar textview
//Create my own textview instead
TextView myTextView = new TextView(_context);
myTextView.setText(text);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //Create layout params for some text
myTextView.setLayoutParams(params); //Apply the text layout params
snackbarLayout.addView(myTextView); //Add my text to the main snackbar layout. (Other widgets will also be added)
}
public void setAction(CharSequence text) {
snackbar.setAction(text, new View.OnClickListener() {
@Override
public void onClick(View view) {
//do something
Log.v("TAG", "You clicked the action");
}
});
}
public void show() {
snackbar.show();
}
}
In my MainActivity, I am using the class like this:
CustomSnackbar customSnackbar = new CustomSnackbar(activity);
customSnackbar.make(view, "This is my snackbar", Snackbar.LENGTH_INDEFINITE);
customSnackbar.setAction("HIDE");
customSnackbar.show();
You can see that I am using my .setAction method to pass a string/charsequence but I am unsure how to handle the onClickListener in the same call instead of handling the onClickListener inside the class
Please ignore the fact that the class may appear pointless (but this is because I have simplified it for the purpose of this question). I'm not sure that I am creating this class correctly, so any additional advice or suggestions would be appreciated. Thanks.
Upvotes: 2
Views: 1187
Reputation: 1356
I am exactly not clear about your question. But,if you want to make a custom Snackbar which will display all your message, and give functionality on click of it. Then you can try this code.
just call this method to make a snackbar.
//Here I am sending one as code
showMessage("Snackbar Opened","Close",1)
//send different code based on which you can set something to be done, or identify a button click
private void showMessage(String msg, String action_name, final int code) {
progress.cancel();
final Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), msg, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(action_name, new View.OnClickListener() {
@Override
public void onClick(View v) {
clearDataForListView();
if (code == 1) {
//do something if code is one/if a particular button is clicked
//you can dismiss anwhere else also.
snackbar.dismiss();
} else if (code == 2) {
//do something if another button is pressed
snackbar.dismiss();
}
}
});
snackbar.show();
}
Upvotes: 0
Reputation: 19243
declare somewhere else your OnClickListener
, e.g. in Activity
in which you are calling your methods, and pass it to your class
final View.OnClickListener ocl = new View.OnClickListener() {
@Override
public void onClick(View view) {
//do something
Log.v("TAG", "You clicked the action");
}
}
CustomSnackbar customSnackbar = new CustomSnackbar(activity);
customSnackbar.make(view, "This is my snackbar", Snackbar.LENGTH_INDEFINITE);
customSnackbar.setAction("HIDE", ocl);
customSnackbar.show();
and inside your custom class:
public void setAction(CharSequence text, final View.OnClickListener ocl) {
snackbar.setAction(text, ocl);
}
now inside OnClickListener
you may call methods from Activity
Upvotes: 2
Reputation: 62
Do a class extends Snackbar.
Then addMouseListener(classWithExtendedSnackbar)
Upvotes: 1
Reputation: 157457
you have to provide it as parameter to your setAction
method. E.g.
public void setAction(CharSequence text, final View.OnClickListener listener) {
and either pass the provided instance, or proxy the call to the other object
Upvotes: 1