Search code examples
androidtextviewandroid-custom-view

Customizing a textview


I want to create a label like this:

enter image description here

Requirements are:

  • The rectangle should grow with the text
  • the length of the red line is dependent of a property

What I tried was to create a 9ng image for the background of the text and use the padding property for the length of the red line. The red line is drawn by overriding the onDraw method.

The result is that the red line is correct but the background is expanded including the padding like this:

enter image description here

What's the best way to do this? Building a custom view with a label included? a custom view drawn 100% programmatically (in this case, what's the method to resize a 9ng image?)?

Thanks Julien


Solution

  • try this one

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="The text"
                android:textColor="#000000"
                android:background="@drawable/<your_drawable>"
                android:textSize="16sp" />
    
                <View 
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:background="#f00"
                android:layout_weight="1" />
    
        </LinearLayout>
    

    Hope it will help you