SMGhost
SMGhost

Reputation: 4037

Android making imageview as long as textview

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?

Upvotes: 0

Views: 493

Answers (5)

Permita
Permita

Reputation: 5493

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"

Upvotes: 2

Abhijit Chakra
Abhijit Chakra

Reputation: 3236

<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..

Upvotes: 1

salty
salty

Reputation: 614

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.

Upvotes: 1

Charan Pai
Charan Pai

Reputation: 2318

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>

Upvotes: 0

gsgx
gsgx

Reputation: 12249

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...

Upvotes: 1

Related Questions