Tuesday, October 1, 2019

Showing A Custom Toast message in Android

Many a times we have to go beyond the built-in options and build our own custom interfaces. Here I will share with you the code to build your own custom Android Toast Messages.

Code Snippet


//Show custom Toast Message.
Toast t = new Toast(this);
View v = LayoutInflater.from(this).inflate(R.layout.no_data, null);
TextView messageTV = (TextView) v.findViewById(R.id.textViewNoDataMessage);
messageTV.setCompoundDrawablePadding(4);
messageTV.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.bullet_ball_glass_red_16), null, null, null);
messageTV.setText(R.string.device_unauthorized_message);
messageTV.setTextSize(25);
messageTV.setTypeface(Typeface.DEFAULT);
t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
t.setDuration(Toast.LENGTH_LONG);
t.setView(v);
t.show();



Here you go guys! Hope this helps you! Enjoy!