-1

I want to draw a simple line on top of some text in a TextView. I have looked at various examples which seem to override the onDraw() function but my understanding is that onDraw() is called when something is drawn.

I would like a vertical line in my TextView and at this moment in time I dont really care where it is, once I have the line I am sure I will be able to manipulate it to the position I would like.

I have a TextViewWithLines class extending TextView where the code will go:

public class TextViewWithLines extends TextView {

public TextViewWithLines(Context context){
    super(context);
}

public TextViewWithLines(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TextViewWithLines(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

and I also have a fragment where I would like the drawing of the line to be done when I create the view.

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

    t1 = (TextViewWithLines)v.findViewById(R.id.display1);
            .................
}

Any help would be appreciated

Kara
  • 6,115
  • 16
  • 50
  • 57
Allan Macmillan
  • 1,481
  • 3
  • 18
  • 30

2 Answers2

0

As you describe, you simply need to do your drawing in onDraw().

Here is a (working) example of someone that wants all text in the Textview underlined;

How can I have a row separating line in TextView

Community
  • 1
  • 1
Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
  • What do I call from my fragment though? – Allan Macmillan Oct 14 '13 at 15:11
  • You do not need to call anything special in your fragment. onDraw() is called by Android, whenever the content of a view is to be displayed. I think this is what you were missing, to understand the examples. – user2808624 Oct 14 '13 at 18:34
0

One possible solution (just a simple XML layout):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/small_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="@dimen/define_your_size"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />

    <LinearLayout
        android:id="@+id/text_separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:orientation="vertical"
        android:background="@color/define_your_color"
        android:layout_alignParentTop="true"/>

</RelativeLayout>

If you want to draw a shape around a TextView (like a rectangle), you can define a drawable background and set the colors you want:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true">
        <shape>
            <stroke android:color="#FFCC00" android:width="3dp"></stroke>
            <corners android:radius="5dp"/>
            <solid android:color="#FAFFA8"/>
        </shape>
    </item>

    <item android:state_enabled="false">
        <shape>
            <stroke android:color="#FFFFFF" android:width="2dp"></stroke>
            <corners android:radius="5dp"/>
            <solid android:color="#00FFFFFF"/>
        </shape>
    </item>

    <item>
        <shape>
            <stroke android:color="#DADADA" android:width="2dp"></stroke>
            <corners android:radius="5dp"/>
            <solid android:color="#FFFFFF"/>
        </shape>
    </item>

</selector>
fasteque
  • 4,309
  • 8
  • 38
  • 50