Android Certification Training Course
- 64k Enrolled Learners
- Weekend
- Live Class
In this topic of “How to Create Android Widgets” we will learn how you can customize a Toast. In our previous posts, we covered RatingBar and SeekBar which were much similar to use.
If you are not clear with the basic concepts of Android, please attend this Android Course.
Are you bored using the same Toast all the time?
This blog will tell you how to create your own Customized Toast. Cool isn’t it. ;)
Using Customized Toast, you can truly redefine the look of the toast. You can even put images in your toast. In the above image next to the text “No Internet Connection”, Its an image placed to make this toast look great. Changing the color, size of the text is very easy. So Let’s begin!
Create an XML file and drag all the android widgets inside it that you want to display in the Toast.
Important key points :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_custom" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/custom_textview" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="4dp" android:contentDescription="@string/android" android:src="@drawable/img2" /> <TextView android:id="@+id/tvtoast" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingRight="6dp" android:paddingTop="6dp" /> </LinearLayout>
Creating another XML file that sets the appearance of the text view which will be used as background in the above layout. This file should be placed in drawable folder.
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#98E8F9" /> <stroke android:width="1dip" android:color="#4fa5d5" /> <corners android:radius="4dp" /> </shape>
The custom layout that we created is inflated inside this method and using it we can create our customized Toast.
// Inflating the layout for the toast LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_custom)); // Typecasting and finding the view in the inflated layout TextView text = (TextView) layout.findViewById(R.id.tvtoast); // Setting the text to be displayed in the Toast text.setText("No internet connection"); // Setting the color of the Text to be displayed in the toast text.setTextColor(Color.rgb(0, 132, 219)); // Creating the Toast Toast toast = new Toast(getApplicationContext()); // Setting the position of the Toast to centre toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); // Setting the duration of the Toast toast.setDuration(Toast.LENGTH_LONG); // Setting the Inflated Layout to the Toast toast.setView(layout); // Showing the Toast toast.show();
[dl url=”#” class=’eModal eModal-11′ title=”Download Code” desc=”” type=”” align=”” for=”download”]
Got a question for us? Mention them in the comments section and we will get back to you.
Related Posts:
Course Name | Date | |
---|---|---|
Android Certification Training Course | Class Starts on 4th March,2023 4th March SAT&SUN (Weekend Batch) | View Details |
Android Certification Training Course | Class Starts on 6th May,2023 6th May SAT&SUN (Weekend Batch) | View Details |
edureka.co
nice……
This is the correct blog for anybody who wants to find out about this topic. You understand so much its nearly onerous to argue with you (not that I actually would need…HaHa). You definitely put a brand new spin on a subject thats been written about for years. Nice stuff, simply nice!
I adore foregathering utile information , this post has got me even more info! .
Really coooooool :)
Good one! :))
Thanks
its really helpful..Thans edureka.
really great…