Hello all
We all have seen toasts right…ok then you have already customized your toasts right.
Here I will show you a method to create toasts which will last the time you set and the gravity.
You can download the code by clicking on the download links.
This is the class that creates the toast notification.
MyMsgBox.java
package com.example.messageboxtest; import android.app.Activity; import android.content.Context; import android.os.Handler; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.LinearLayout; import android.widget.TextView; public class MyMsgBox { private static final int ANIMATION_DURATION = 600; private int HIDE_DELAY = 5000; private View mContainer; private int gravity = Gravity.CENTER; private TextView mTextView; private Handler mHandler; private AlphaAnimation mFadeInAnimation; private AlphaAnimation mFadeOutAnimation; public MyMsgBox(Context context, int HIDE_DELAY, int gravity) { ViewGroup container = (ViewGroup) ((Activity) context) .findViewById(android.R.id.content); View v = ((Activity) context).getLayoutInflater().inflate( R.layout.newmb__messagebar, container); this.HIDE_DELAY = HIDE_DELAY; this.gravity = gravity; init(v); } private void init(View v) { mContainer = v.findViewById(R.id.mbContainer); mContainer.setVisibility(View.GONE); mTextView = (TextView) v.findViewById(R.id.mbMessage); mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f); mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f); mFadeOutAnimation.setDuration(ANIMATION_DURATION); mFadeOutAnimation .setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mContainer.setVisibility(View.GONE); } @Override public void onAnimationRepeat(Animation animation) { } }); mHandler = new Handler(); } public void show(String message) { mContainer.setVisibility(View.VISIBLE); ((LinearLayout) mContainer).setGravity(gravity | Gravity.CENTER_VERTICAL); mTextView.setText(message); mFadeInAnimation.setDuration(ANIMATION_DURATION); mContainer.startAnimation(mFadeInAnimation); mHandler.postDelayed(mHideRunnable, HIDE_DELAY); } private final Runnable mHideRunnable = new Runnable() { @Override public void run() { mContainer.startAnimation(mFadeOutAnimation); } }; }
Now the layout for this toast.. Here I am making a toast with only one textview in it.
This is the layout.
newmb__messagebar.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mbContainer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="10dp" android:gravity="bottom" android:orientation="vertical" > <LinearLayout style="@style/bgTheme" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="bottom" android:orientation="vertical" > <TextView android:id="@+id/mbMessage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Test" android:textColor="@drawable/white" /> </LinearLayout> </LinearLayout>
This will create some errors.
inside your drawable folder create an xml named “bg_gradiant.xml” and copy this code into it.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item><shape> <solid android:color="@drawable/dark_grey" /> <stroke android:width="0dp" android:color="#000000" /> <corners android:radius="5dp" /> </shape></item> </selector>
And at last in your values/styles.xml add this style
<style name="bgTheme"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_gravity">center_horizontal|center</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">@drawable/white</item> <item name="android:textStyle">bold</item> <item name="android:layout_margin">3dp</item> <item name="android:background">@drawable/bg_gradiant</item> <item name="android:textSize">14sp</item> <item name="android:typeface">normal</item> <item name="android:text">05</item> <item name="android:padding">10dp</item> </style>
You can add these colors to your strings.xml
<drawable name="white">#ffffff</drawable> <drawable name="black">#000000</drawable> <drawable name="blue">#2554C7</drawable> <drawable name="green">#347C2C</drawable> <drawable name="orange">#ff9900</drawable> <drawable name="pink">#FF00FF</drawable> <drawable name="violet">#a020f0</drawable> <drawable name="grey">#778899</drawable> <drawable name="red">#C11B17</drawable> <drawable name="yellow">#FFFF8C</drawable> <drawable name="PowderBlue">#b0e0e6</drawable> <drawable name="brown">#2F1700</drawable> <drawable name="Hotpink">#7D2252</drawable> <drawable name="dark_grey">#424242</drawable>
Now in your activity call the class like this.
// time in ms MyMsgBox m = new MyMsgBox(arg0.getContext(), 5000,Gravity.BOTTOM); m.show("Hello this is a custom toast");
Our custom toast is complete, now run the project and customize it according to your need.
You can download the complete android source code for this project from here.