I have the following in an xml layout :
Notice how the hexagon # 4 is NOT center aligned with the text. How can i do this: here is what i have tried so far:
To actually get the view with the # inside of it i inflate a view that looks like this:
//my_hexagon_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
tools:ignore="MissingPrefix">
<Button
android:id="@+id/tv_icon"
fontPath="proxima_nova_semi_bold.otf"
android:layout_width="16dp"
android:layout_height="17.5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:alpha=".25"
android:background="@drawable/hexagon"
android:clickable="true"
android:contentDescription="@string/content_description"
android:focusable="false"
android:padding="0dp"
android:text="4"
android:textColor="@color/white"
android:textSize="8dp"
/>
</LinearLayout>
After inflating the view i take a copy of its drawing cache and i use that in the ImageSpan. here is how i get the copy of the drawing cache:
public Bitmap getIconBitmap() {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(getActivity());
// inflate and measure the button then grab its image from the view cache
ViewGroup parent = (ViewGroup) inflater.inflate(R.layout.my_hexagon_button, myRoot);
TextView tv = (TextView) parent.findViewById(R.id.tv_icon);
parent.setDrawingCacheEnabled(true);
parent.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
parent.layout(0, 0, parent.getMeasuredWidth(), parent.getMeasuredHeight());
parent.buildDrawingCache(true);
// if you need bounds on the view, swap bitmap for a drawable and call setbounds, im not using bounds
Bitmap b = Bitmap.createBitmap(parent.getDrawingCache());
parent.setDrawingCacheEnabled(false); // clear drawing cache
return b;
}
So now i have a bitmap that looks like the hexagon #4 image in the drawing i attached. Now lets use that in the ImageSpan:
public Spannable createImageSpan(TextView tv,Bitmap bitmapIcon) {
Spannable span = new SpannableString(tv.getText());
int start = span.length() - 1;
int end = span.length();
ImageSpan image = new ImageSpan(new BitmapDrawable(getResources(), bitmapIcon),ImageSpan.ALIGN_BASELINE);
span.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return span;
}
then later on i simply set that span on my textview. also dont forget to set the bounds on the drawable or it wont display, and it wroks but the image is not aligned center in the text. Notice how it drops to the bottom. How can i resolve this cleanly ?