How to Customise Toast in Android?

前端 未结 6 487
礼貌的吻别
礼貌的吻别 2021-01-05 05:06

Is it possible to make Customize Toast in Android. like if can we place in it image icon and place button.

相关标签:
6条回答
  • 2021-01-05 05:23

    You can put any view in a Toast using setView. However, I'm not quite sure why you would want to place a button in it, as a Toast will rapidly disappear. Taken from the officiel developer site :

    When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see.

    So the toast should only be used to display information. For more complex interactions, you can use a Dialog.

    0 讨论(0)
  • 2021-01-05 05:25

    It is obviously possible to create custom toast in android. Just check my blog.http://androiddesk.wordpress.com/2012/01/28/custom-notification-in-android-with-an-example/ I've explained about it in detail.

    0 讨论(0)
  • 2021-01-05 05:28

    Toast is non focus able.Adding button did not make sense. However you can display information.You can also control its visibility means u can hide and show by making few changes in Toast class.

    0 讨论(0)
  • 2021-01-05 05:32

    XML FILE

    enter code here`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="8dp"
              android:background="#DAAA"
              >
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
    

    '

    JAVA CODE

     LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast,
                                   (ViewGroup) findViewById(R.id.toast_layout_root));
    
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("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();
    
    0 讨论(0)
  • 2021-01-05 05:34

    You can also use the regular makeText() and handle the getView() to set an image next to see the next.

    Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
    if (null!=tv) {
        tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
        tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));
    
    0 讨论(0)
  • 2021-01-05 05:41

    toast can be customized to show at different places like top, bottom, center, left, right

     toast.setGravity(Gravity.TOP , 0, 0); // for example setting gravity to top
    

    for more details [here](http://androidcoding.in/2016/05/31/android-tutorial-custom-toast/"android custom toast")

    0 讨论(0)
提交回复
热议问题