Tuesday, May 21, 2013

Android Custom Toast

A toast is a view containing a quick little message for the user. A toast provides simple feedback about an operation in a small popup.It only fills the amount of space required for the message and the current activity remains visible and interactive.

The default implementation of a  Toast shows a simple text at the bottom of the screen.
We can change this  default implementation of Toast , we can change the size , color, style and other properties of the text being displayed in Toast.   Not only this we show image and other views in Toast.

Showing Toast for Longer Time: Customize the Display Time Of Toast

 

Toast.makeText(context,"Your Text", duration).show();


Even we can set a Layout/ View in toast and can make it more decorated.
We can also change the position of the Toast being displayed, we can display it at any place ,top, middle, left, bottom etc  of the screen.


Positioning your Toast:




A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:
 toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.





Creating Layout for Custom Toast :


my_custom_toast.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/custom_toast_layout"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="8dp"
              android:background="#DAAA"
              >
    <ImageView android:src="@drawable/myimage"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/textToShow"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>


Showing The Toast



                LayoutInflater inflater = getLayoutInflater();
                // Inflate the Layout
                View layout = inflater.inflate(R.layout.my_custom_toast,
                                               (ViewGroup) findViewById(R.id.custom_toast_layout));
     
                TextView text = (TextView) layout.findViewById(R.id.textToShow);

                // Set the Text to show in TextView
                text.setText("My Custom Toast in Center of Screen");

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

                toast.show();



With the above example we can create customized toast from an xml layout, we inflate the  xml layout in our code and do the needfull.

We can also create customized toast without creating xml layout,  we can add/select the views at run time runtime and can show in the Toast. This gives us more flexibility to select the Toast Components at runtime.




                



                 // create a LinearLayout and Views

                LinearLayout  layout=new LinearLayout(this);
                layout.setBackgroundResource(R.color.LightOrange);
              
                TextView  tv=new TextView(this);
                // set the TextView properties like color, size etc
                tv.setTextColor(Color.RED);
                tv.setTextSize(15);        

                tv.setGravity(Gravity.CENTER_VERTICAL);

                // set the text you want to show in  Toast
                tv.setText("My Custom Toast at Bottom of Screen");  

               ImageView   img=new ImageView(this);

                // give the drawble resource for the ImageView
                img.setImageResource(R.drawable.myimage);


              // add both the Views TextView and ImageView in layout
                layout.addView(img);
                layout.addView(tv);

                Toast toast=new Toast(this); //context is object of Context write "this" if you are an Activity
               // Set The layout as Toast View
                toast.setView(layout);
                     
                  // Position you toast here toast position is 50 dp from bottom you can give any integral value   
                 toast.setGravity(Gravity.BOTTOM, 0, 50);
                 toast.show();




 We can also set/customize  the time interval of the Toast to show. It has been discussed in next post.



More Android Topics 


Android : Introduction


       Eclipse Setup for Android Development

                     Configuring Eclipse for Android Development

          Begging With Android

                     Creating Your First Android Project
                     Understanding Android Manifest File of your android app


         Working With Layouts

                      Understanding Layouts in Android
                          Working with Linear Layout (With Example)
                                Nested Linear Layout (With Example)
                          Table Layout
                          Frame Layout(With Example)
                         Absolute Layout
                         Grid Layout


       Activity

                     Activity In Android
                     Activity Life Cycle
                     Starting Activity For Result
                     Sending Data from One Activity to Other in Android
                     Returning Result from Activity

     Working With Views

                     Using Buttons and EditText in Android 
                     Using CheckBoxes in Android 
                     Using AutoCompleteTextView in Android
                     Grid View

       Toast

                     Customizing Toast In Android
                     Customizing the Display Time of Toast
                     Customizing Toast At Runtime
                     Adding Image in Toast
                     Showing Toast for Longer Time

     Dialogs In Android

                     Working With Alert Dialog
                     Adding Radio Buttons In Dialog
                     Adding Check Boxes In Dialog
                     Creating Customized Dialogs in Android
                    Adding EditText in Dialog

                   Creating Dialog To Collect User Input

                 DatePicker and TimePickerDialog

                              Using TimePickerDialog and DatePickerDialog In android

    Working With SMS

                  How to Send SMS in Android
                  How To Receive SMS
                  Accessing Inbox In Android

    ListView:

               Populating ListView With DataBase

      Menus In Android

                    Creating Option Menu
                    Creating Context Menu In Android

      TelephonyManager

                    Using Telephony Manager In Android

     Working With Incoming Calls

                    How To Handle Incoming Calls in Android
                    How to Forward an Incoming Call In Android
                   CALL States In Android
 

    Miscellaneous

                   Notifications In Android
                   How To Vibrate The Android Phone
                   Sending Email In Android
                  Opening a webpage In Browser
                   How to Access PhoneBook In Android
                   Prompt User Input with an AlertDialog

   Storage:  Storing Data In Android


               Shared Prefferences  In Android

                             SharedPreferences In Android

               Files: File Handling In Android

                              Reading and Writing files to Internal Stoarage
                              Reading and Writing files to SD Card 
                           

                DataBase : Working With Database

                             Working With Database in Android
                             Creating Table In Android
                             Inserting, Deleting and Updating Records In Table in Android
                             How to Create DataBase in Android
                             Accessing Inbox In Android

     Animation In Android:

                  Animating A Button In Android










               

4 comments:

  1. How do you get the text for a toast to appear in a @drawable/shape that you've defined? Thank you. Great tutorials!

    ReplyDelete
  2. Hi, is it possible to change the time_Display to any other value different to Toast.LENGTH_SHORT and the other value Toast.LENGTH_LONG...

    ReplyDelete
  3. Very nice tutorial. kepp sharing<a href="http://www.androidmaterial.info/2016/01/android-custom-toast-example/" target="_blank" rel="dofollow”></a>

    ReplyDelete
  4. Hi,
    Thanks for sharing the information with us it was very informative. https://hangup.in

    ReplyDelete