Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Custom Toast in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 446
    Comment on it

    Android give facility to change the view of a Toast. Sometimes we need to show message on a better way to user .

    Here I am writing code to implement custom Toast. I make a separate layout for Toast . Here I am showing a ImageView and TextView on a toast and Inflate it on toast . We can set gravity and Orientation here.

    custom_toast_layout.xml

    1. ?<xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:layout_margin="20dp"
    6. android:orientation="vertical" >
    7. <ImageView
    8. android:id="@+id/custom_image"
    9. android:layout_height="wrap_content"
    10. android:layout_width="wrap_content"
    11. android:background="@drawable/user_icon"
    12. />
    13. <textview android:id="@+id/custom_text"
    14. android:layout_height="wrap_content"
    15. android:layout_width="wrap_content"
    16. android:text="Hello......Got it."
    17. android:layout_margintop="20dp"
    18. android:layout_marginleft="10dp"
    19. android:layout_torightof="@+id/custom_image">
    20. /RelativeLayout>
    21.  
    22.  
    23.  
    24. View toastView=getLayoutInflater().inflate(R.layout.custom_toast_layout, null);
    25.  
    26. Toast toastView=new Toast(this);
    27. customtoast.setView(toastView);
    28. customtoast.setGravity(Gravity.CENTER_HORIZONTAL);
    29. customtoast.setDuration(Toast.LENGTH_SHORT);
    30. customtoast.show();

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: