I have a textview which holds an image and some text.
programmatically i have added the drawable image to the left side of text view
txtIDFav.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fav_enabled, 0, 0, 0);
i have aligned the text view to center using
txtIDFav.setGravity(Gravity.CENTER);
Now the text in the textview is aligned to center. but the image is on the left side.
eg: [image]_____________________ mytext
used the _ to give space as its not allowed.
I want to align the image to center along with the text.
eg: _____________________[image]mytext
Please advice.
Thanks and appreciate.
You can try this, it should work:
Spannable span = new SpannableString(" " + txtIDFav.getText()); // or set your text manually
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = getResources().getDrawable(R.drawable.fav_enabled, getContext().getTheme());
}
else {
drawable = getResources().getDrawable(R.drawable.fav_enabled);
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(drawable);
span.setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
txtIDFav.setText(span);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With