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.
it is not possible to set image in background of text view instead of this you can set your drawable on textview like this
android:drawableRight="@drawable/ic_launcher"
"or"
android:drawableleft="@drawable/ic_launcher"
"or"
android:drawableBottom="@drawable/ic_launcher"
only for your problem's solution you can use texview inside the framelayout in this way
You have to write code like this:-
Embed the drawable in your text by using a SpannableString.
I assume it is happened because the width of textView is set to match_parent or fill_parent. Try setting textView width to wrap_content and center the whole textView not only text.
Layout:
Activity:
Result
I was also facing the same issue, But I don't want to use Button and TextView separately and also I don't want to make it using java code. So I found this. I know I am giving solution late. But it helps you
Try this, it should work: