Chromium Chromium - 2 months ago 25
Android Question

Android compound drawable

I am using a custom toast, with a

ImageView
and a
TextView
using a background in drawable. But the Android Studio tells me to use a single
TextView
and a compound drawable instead. I have already used a drawable for background, but how can I "draw" a picture at a certain position relative to the
TextView
?

Answer

You can use a single TextView and a CompoundDrawable like this

Toast toast = Toast.makeText(this, "Custom Compount Drawable Toast", Toast.LENGTH_LONG);
View toastView = toast.getView(); //This'll return the default View of the Toast.

TextView toastMessage = (TextView) toastView.findViewById(android.R.id.textview);
toastMessage.setTextSize(25);
toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image_id, 0, 0, 0);
toastMessage.setGravity(Gravity.CENTER);
toastMessage.setCompoundDrawablePadding(16);
toastView.setBackgroundColor(Color.CYAN);
toast.show();
Comments