0

I want to have my text stroke out. I use a custom strike line image. So far I did this:

<RelativeLayout
    android:id="@+id/title_container"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFF0000">

    <TextView
        android:id="@+id/title"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:singleLine="true"
        android:ellipsize="end"
        android:textColor="@color/title_text"
        android:textSize="18dp"
        android:background="#FF00FF00"
         />

    <ImageView
        android:id="@+id/title_strike_through"
        android:contentDescription="@null"
        android:layout_centerVertical="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:duplicateParentState="true"
        android:src="@drawable/title_strike"/>

</RelativeLayout>

The problem now is that title_container layout stretches across whole width of the window, and as a result title_strike_through stretches too. It should not! I only need title_strike_through stretching over the text. What should I do?

SMGhost
  • 3,867
  • 6
  • 38
  • 68
  • 1
    If you're just trying to set a strikethrough of the title text, you can set strike through in code. [This answer covers it](http://stackoverflow.com/a/4301555/700292) – tim Dec 13 '12 at 04:15
  • I don't see where that method takes custom image into account. My image is not one line, and I already knew about this method and background 9 patch one. None of them apply in this situation. – SMGhost Dec 13 '12 at 04:26

5 Answers5

2

add the below properties in your imageview to make it equal to the width of textview:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/title"
android:layout_alignRight="@id/title"
Permita
  • 5,503
  • 1
  • 16
  • 21
1

Programmatically set the width of the ImageView to the width of the TextView. Although I don't understand why the layout spans the entire width if it's set to wrap content...

gsgx
  • 12,020
  • 25
  • 98
  • 149
  • I would rather not do that because in my getView method getWidth() and getHeight() return zero, so I need to add additional code to be able to correctly handle dimensions. I'll use this method only as a last resort if no one offers more elegant solution. – SMGhost Dec 13 '12 at 04:49
1

If you want it to take up a specific amount of space, then you should set the layout_width property. If you want it to take up a percentage of the screen, then you should set the layout_weight property.

aLearner
  • 1,051
  • 14
  • 30
salty
  • 594
  • 4
  • 6
  • 18
  • Text is of variable size, so I need process to be dynamic. – SMGhost Dec 13 '12 at 04:42
  • if you need it to resize dynamically, then you can use the setWidth() method on the textview in your java.... Edit: here, this post has a pretty good solution to what you're trying to do, even though it focuses on a linearlayout and not a relative, the information should work the same: http://stackoverflow.com/questions/2676190/setting-width-of-textview-in-a-linearlayout – salty Dec 13 '12 at 04:44
  • It's not the TextView that should be limited in width, but the line. – SMGhost Dec 13 '12 at 04:46
  • I'm not sure strikethroughs work that way, I thought they worked by text length including spaces. To achieve what you're trying to do, you could probably take a 1px image and make an imageview that over laps the textview and then stretch the 1px programmatically as needed. – salty Dec 13 '12 at 04:52
1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/title_container"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFF0000">

    <TextView
        android:id="@+id/title"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:singleLine="true"
        android:ellipsize="end"
        android:textColor="@color/titleText"
        android:textSize="18dp"
        android:background="#FF00FF00"
         />

    <ImageView
        android:id="@+id/title_strike_through"
        android:contentDescription="@null"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:src="@drawable/ic_launcher"/>

</RelativeLayout>

is this what you want please let me know..

Abhijit Chakra
  • 3,201
  • 37
  • 66
0

try this..

<RelativeLayout
    android:id="@+id/title_container"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFF0000">
<LinearLayout
 android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
android:duplicateParentState="true">

<TextView
        android:id="@+id/title"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:singleLine="true"
        android:ellipsize="end"
        android:textColor="@color/title_text"
        android:textSize="18dp"
        android:background="#FF00FF00"
         />

    <ImageView
        android:id="@+id/title_strike_through"
        android:contentDescription="@null"

        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:src="@drawable/title_strike"/>
</LinearLayout>

</RelativeLayout>
Charan Pai
  • 2,288
  • 5
  • 32
  • 44
  • This does manage to keep Relative layout width same as text, but I can't have ImageView over TextView now and when I add another RelativeLayout inside Linear, the problem returns. – SMGhost Dec 13 '12 at 05:18