Button in Custom Android Toast

Button in custom Android Toast?

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.

How can I include a button in a Toast notification?

The Gmail undo bar is't a toast, here is how Google did it

http://code.google.com/p/romannurik-code/source/browse/misc/undobar/src/com/example/android/undobar/UndoBarController.java

I guess this answers your question.

Android Toast with Button

No its not Possible use a Dialog Box Insted.

Toast with button in Android

You can use custom toast messages like this. This is a dialog will dismiss after 2.5 seconds just like a toast.

public class CustomToast extends Dialog {  
public CustomToast(Context context, String text) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(android.content.Context.
LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.toast, null);
TextView tv = (TextView) layout.findViewById(R.id.toastText);
tv.setText(text);
setContentView(layout);
show();
setCanceledOnTouchOutside(true);
setCancelable(true);
Window window = getWindow();
window.setGravity(Gravity.BOTTOM);
new Handler().postDelayed(new Runnable() {

public void run() {
dismiss();
}
}, 2500);
}
}

Android: Display a toast message after a custom time when a button is clicked

Something like that:

Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "Hello!", Toast.LENGTH_LONG).show();
}
}, 30000);

}
});

Custom toast on Android: a simple example

Use the below code of a custom Toast. It may help you.

toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#DAAA" >

<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp" />

<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFF" />

</LinearLayout>

MainActivity.java

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

And check out the below links also for a custom Toast.

Custom Toast with Analog Clock

YouTube: Creating Custom Toast With Button in Android Studio

How to show a Toast exactly on a button

rather play around giving int values.... like toast.setGravity(5,6,5); and also try other values..

try to display button with toast function in my ListView

Add this method

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

//You can find your button here
if(view instanceof Button)
{
if(view.getId() == R.id.getenter)
{
Toast.makeText(getApplicationContext(),"toast_msg",Toast.LENGTH_LONG).show();
}
}

}
});

after adapter code.

lv.setadapter(adpater);


Related Topics



Leave a reply



Submit