in Android Code Tips

Customized Toasts

I love Android Toasts :)

Let me show you a way to construct Toast with customized content:

Toast customizedToast = Toast.makeText(this, "Customized toast test, Toast.LENGTH_LONG);
customizedToast.setGravity(Gravity.TOP, 0, 0);
LinearLayout mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
TextView mTextView = new TextView(this);
mTextView.setText("Title here");
ImageView mImageView = new ImageView(this);
mImageView.setImageDrawable(-a drawable here-);
mLayout.addView(mTextView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mLayout.addView(mImageView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
customizedToast.setView(mLayout);
customizedToast.show();
Share