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
Rendering Problems in Android Studio V 1.1/1.2
Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice
How to Import a .Aar File into Android Studio 1.1.0 and Use It in My Code
Strange Behavior of Android Videoview - "Can't Play Video"
Android/Eclipse Panic: Could Not Open
Searchview in Optionsmenu Not Full Width
Findfragmentbytag() Returns Null After Perform a Fragmenttransaction Using Replace() Method
How to Change the Group Owner in a Persistent Group in Wi-Fi Direct
How to Use Mediacodec Without Mediaextractor for H264
Custom 'Keyboard' Built in an Application on Android
How to Apply Plugin to Only One Flavor in Gradle
Setting Android Theme Background Color
Can Gradientcolor Be Used to Define a Gradient for a Fill or Stroke Entirely in Xml