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.setCompound
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);
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
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="20dp"
android:background="@drawable/next_btn_bg_rounded_corners">
<TextView
android:id="@+id/btn_restore_purchases"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:drawableLeft="@mipmap/restore"
android:drawablePadding="10dp"
android:drawableStart="@mipmap/restore"
android:text="@string/restore_purchases"
android:textAllCaps="false"
android:textColor="@color/white" />
</FrameLayout>
Embed the drawable in your text by using a SpannableString.
SpannableStringBuilder textspan = new SpannableStringBuilder(" " + yourText);
Drawable icon = getResources().getDrawable(R.drawable.fav_enabled);
// jump drawable to the text view's state (if it is a state drawable)
icon.setState(textView.getBackground().getState()); // match the background state
textspan.setSpan(new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(textspan);
You have to write code like this:-
txtIDFav.setGravity(Gravity.CENTER | Gravity.RIGHT);
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:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Test"
android:textSize="28sp" />
</FrameLayout>
Activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewTest=(TextView)findViewById(R.id.textViewTest);
textViewTest.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_launcher, 0, 0, 0);
}
}
Result
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:background="@drawable/ic_launcher"
android:layout_gravity="center"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout>