Reputation: 320
I am trying to providing click listener to toast message.Any one tell me is it possible to provide click listener to Toast in android?
I am using custom view for toast and I am apply onclick listener to my view it's not working.I triade this
LayoutInflater inflater = (LayoutInflater) ConnectToXMPP.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.chat_message_alert_dialog,null);
TextView text = (TextView) layout.findViewById(R.id.chat_popup_message_textview);
text.setText("From : " + fromName+ "\n" + fromName);
LinearLayout chatMessageLayout = (LinearLayout)
layout.findViewById(R.id.chat_popup_message_layout);
Toast toast = new Toast(ConnectToXMPP.mContext);
toast.setView(layout);
toast.setGravity(Gravity.CENTER_VERTICAL, 0,0);
toast.setDuration(60000);
toast.getView().setClickable(true);
toast.getView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ConnectToXMPP.mContext,"toast touched",Toast.LENGTH_SHORT).show();
}
});
toast.show();
Upvotes: 2
Views: 4956
Reputation: 21
You can provide the ID to root view of the XML layout file. And find it from the layout by using FindviewbyId and then you can configure the listener for it
Upvotes: 1
Reputation: 399
Use snack bar for this purpose.
Snackbar.make(findViewById(android.R.id.content), "Hi I'm snack bar", Snackbar.LENGTH_LONG)
.setTextColor(Color.parseColor("#FFFFFF"))
.setDuration(30000)
.setBackgroundTint(Color.parseColor("#716E7C"))
.setActionTextColor(Color.parseColor("#49FF00"))
.setAction("Refresh") {
// Do whatever you want
}.show()
Upvotes: 5
Reputation: 4323
Have a look at John Persanos SuperToast library. It includes clickable toasts. Github repo
Upvotes: 1
Reputation: 597
Janusz's Answer
A toast can not be clicked. It is not possible to capture a click inside a toast message. You will need to build a dialog for that. Look at Creating Dialogs for more info.
The API on the Toast class state that a toast will never receive the focus and because a toast is not a view there is no onClick message. I would assume that therefore childs of a Toast can not be clicked as well.
Upvotes: 1