Nick Borisenko Nick Borisenko -3 years ago 115
Android Question

How to position Drawable over TextView in Android XML

I'm trying to create strike-through text in my XML. I made a drawable (just a line), and draw it behind the text if a condition is met (using data binding). The problem is that my text and strikethrough are different colors, and when the drawable is added, it is drawn behind the text, which looks weird. I need it to appear over the text, not behind it. Currently, I have it using the

background
Android attribute, so it makes sense why it would be rendering behind the text, however I couldn't find any other options. The only related help I found online was to use
drawableTop
, but then the drawable wouldn't appear at all. Also, I can't do this programmatically, I want to do it in the xml so that I don't need to reference my views elsewhere. Any help would be appreciated. Here is some of my code:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/spacing_xs"
android:layout_marginLeft="@dimen/spacing_xs"
android:text="@{@string/text(viewModel.insertThisIntoString)}"
android:background="@{viewModel.displayStrikeThrough.value ? @drawable/strikethrough_text : null}"/>


and my drawable:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<item>
<shape android:shape="line">
<stroke android:width="2dp"
android:color="@color/app_grey_300"/>
</shape>
</item>
</layer-list>


Code I used when trying to display ImageView over TextView:

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/spacing_xs"
android:layout_marginLeft="@dimen/spacing_xs"
android:text="@{@string/text(viewModel.insertThisIntoString)}"/>

<ImageView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/strikethrough_text"/>
</RelativeLayout>


Ideally, I would want the layout_width of the ImageView to span the width of the TextView it's covering. What I tried was setting the width of the RelativeLayout to
wrap_content
(so that its width would match the width of the TextView), and set the width of the ImageView to
match_parent
, so that it would fill up the width of the RelativeLayout, and therefore the TextView, however that didn't work - the width would span the entire screen. I currently have it at 40dp to make sure it would appear.

Also, it should be noted that I tried setting the drawable as both the background and the src of the ImageView, neither of which worked.

Answer Source

Since you are using android:layout_height="wrap_content" in the ImageView, it will take as much height as it needs. But your Drawable is not a bitmap, and also has no size itself. So it will take exactly 0px height, as it needs 0px while not having size.
You should set some concrete value to the height, or make your ImageView match size of TextView depending on how you want it to look like.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download