Download the New LearnVern Android App and enter Lucky Draw to win Free Certificate
Now we will learn how we can create Custom Toast in Android.
1. So to create a custom Toast, we will first design the layout, for that we have to create two types of layout file -
2. When we create any project inside Android, we have two types of content, one is 'Static' and the other is 'Dynamic'.
3. Static content is that which is auto-generated such as the 'activity_main.xml' file of our layout. Dynamic content is the one whose run time changes, meaning that it actually changes when our application is executed.
4. Now we will see how we design 'customtoast.xml'. Here inside the customtoast.xml file, first of all we have to design its layout.
5. So for that we will first use an image view, so for that we have describe the basic properties of image view such as width and height, and along with that we have provided its description as well,
6. After that we have to provide its image resource,
7. After that we will take a text view, which we have given the id and with it, width, height, description, specification have been specified. Meaning that we have created the customtoast.xml file with an image view and text view.
8. Now we will have the java file inside our 'java' folder, open it, and now we have to generate a customtoast, for that we will now use our customtoast.xml file, but to use this thing we have to learn a concept called 'Layout Inflater'.
9. Layout Inflater is a class that is used to create an instance of any xml file of layout. Inshort, it will input the xml file, and build it as a view, meaning that we use Layout Inflater when we want to design a custom view.
10. After that we will create an instance of view,
11. So this is how we can use Layout inflator and use the layout XML file and can display its view.
12. After that we have to set the properties of Toast here,
13. After that we will set all the properties of toast, such as Gravity, Duration, View and show them all in the last,
14. After this, we will execute our code, as soon as we execute our toast notification will come and our code runs.
Have a friend to whom you would want to share this course?