How To Show Custom Toast In Android?

When it's comes to display light message in response to options performed by app Android platform offers Toast.Toast is a light messages display near the bottom,horizontally centered to the screen.Toast can display maximum two line of message.Toast messages are not clickable and hide automatically after sometime.Toast messages are useful when you don't want to block user interface and want to display messages.
Figure 1. shows "Sending message..." text message,indicating message  has been sending.

Fegure 1. Toast Message. 

Show Toast.

To show Toast you need to first initialize Toast object with one of makeText() methods.makeText() method takes three parameters : the application Context, the message to display and duration for the Toast.this method returns fully initialized Toast object.you can show Toast with show() method.

Following code snipped shows how to show simple toast message.


How To Change Toast Default Position?

Toast by default display near the bottom of the screen,horizontally centered.You can change toast default position with setGravity(int, int, int) method,which takes three parameters: a Gravity constant , a X-position offset and Y-position offset.
Following code snippet shows how to position toast to top right corner of the screen.

If you want to change position of x axis of toast then increase or decrease value of second parameter and if you want to change position of Y-axis of toast then increase of decrease value of third parameter.


How To Display Custom Toast?

When simple toast layout can not fulfill your requirements you can create custom view for toast messages.To create custom toast layout define View layout,in XML file or in you application code and pass the root View object to setView(view) method of the Toast object to set this layout.

Following code snipped shows custom layout for the toast message.
Note : save this layout file as custom_toast.xml

You can see that ID of the LinearLayout element is "custom_toast_container". You must use this ID and ID of XML layout file to inflate the layout.

Following code snippet shows how to inflate and set custom layout for Toast.

First retrieve LayoutInflater object with getLayoutInflater() or from getSystemService()  method.then inflate your custom layout with inflate(int, ViewGroup) method, which take to parameters first is the ID of layout file you want to inflate and second is root View.You can use inflated View to find more View objects in the layout.So lets find and set content for TextView.At last create the Toast object with Toast(Context) and set properties like duration,gravity Then call setView(View) and pass it the inflated custom layout.You can now display toast by calling show() method.

How To Implement Swipe-to-Refresh In Android?

If you are updating you app content automatically and user does not need to do any thing for that.But you can also provide feature to user so that they can request for manual update.For example in video streaming app user can request manual update to get latest videos.
To provide consistence user experience for requesting updates.Android provide swipe-to-refresh design pattern,which allow user to trigger update with vertical swipe gesture.

Add Swipe-to-Refresh To App.

Swipe-to-Refresh design pattern is completely implemented with SwipeRefreshLayout widget.which detects vertical swipe gesture of user.Display distinctive progress bar and call callback method in app.You can add swipe-to-Refresh feature by adding this widget in to you layout file as a parent of RecyclerView , ListView or GridView.
Note : SwipeRefreshLayout only support signle RecyclerView , ListView or GridView as his child.

Following code snipped shows how to add SwipeRefershLayout in layout file containing RecyclerView.


 Add Refresh Action To Action Bar.

 

You should add refresh action in action bar , so that user with disability could trigger manual update with external device like keyboard and D-pads.
Following code snipped shows how to add swipe-to-refresh action to menu overflow area.


Refresh App Content With Refresh Requests.


Respond To User Gesture.

To respond to user's vertical swipe gesture you have to implement SwipeRefreshLayout.OnRefreshListener interface and it's onRefresh() method,OnRefresh() method called when user perform vertical swipe gesture.System automatically shows distinctive progress bar when user perform swipe gesture and you should update app content in onRefresh() method.

Following code snipped shows how to implement SwipeRefreshLayout.OnRefreshListener and update app content when you get refresh request from user's vertical swipe gesture.


Respond To The Action Bar Refresh Action.

When user request manual refresh request from action bar,Android OS calls the onOptionsItemSelected() method.Your app should show progress indicator and update app's data when this method calls.To get callback of menu item selection override onOptionsItemSelected() method.To display SwipeRefreshLayout progress indicator call setRefreshing() method of SwipeRefreshLayout with true value,And once update has finished,call setRefreshing(false) to remove progress indicator.

Following code snippet shows how to respond request action.


How To Get Notify When System UI Visibility Changes In Android?

When ever you hide system bars to give fullscreen experience you should also hide or show other part of you app's UI as per your requirements. i.e. play , pause buttons , game controls etc.
In this post we gonna learn how to listen for system bars hide / show event and make any desired changes accordingly.

To listen for system bars hide / show event,register View.OnSystemUiVisibilityChangeListener() to view.This could be any view you used for controlling navigation visibility.

In Following Code Snipped We Have Registered Listener in Activity's onCreate() method.


It's best precise to sync you UI with system bar visibility changes.You could use this code to hide and show video player controls in sync with status bar hide and show event.