batman
batman

Reputation: 4908

How to draw a line along with some text in center in android?

I'm very new to android and I have to implement a view which looks something like:

-------------Some Text----------------------

I know that I can draw a line using View in my xml using the code like:

<View
android:layout_width="100dp"
 android:layout_height="1dp"
 android:background="#00ff00" />

this will end up in drawing the:

-----------------------

but after this I wish to place the text and again draw the other end of the line.

I tried but I get something like this:

----------------------
text

when I add some TextView after the View code.

Is that possible to do what I'm aiming for ?

Thanks in advance.

Upvotes: 2

Views: 3396

Answers (4)

SkyWalker
SkyWalker

Reputation: 855

May be this will help you to get what you want. Giving weight to view is a key..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_weight="1"
    android:background="#00ff00" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:background="#00ff00" />
</LinearLayout>

Upvotes: 3

pavko_a
pavko_a

Reputation: 507

Try using FrameLayout for this cases or RelativeLayout to prevent deep view hierarchy and improve performance

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >

<View
    android:layout_width="200dp"
    android:layout_height="1dp"
    android:layout_gravity="center"
    android:background="#555" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#fff"
    android:text="Hello world" />

</FrameLayout>

Upvotes: 2

moDev
moDev

Reputation: 5258

Try this..

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

    <View
        android:id="@+id/view1"
        android:layout_width="60dp"
        android:layout_height="4dp"
        android:layout_marginTop="5dp"
        android:background="#00ff00" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_toRightOf="@+id/view1"
        android:text="My Text" />

    <View
        android:id="@+id/view2"
        android:layout_width="60dp"
        android:layout_height="4dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/tv1"
        android:background="#00ff00" />

</RelativeLayout>

Hope it works for you..

Let me know if it works.

Upvotes: 1

Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

Well, the easiest would probably just be to have a RelativeLayout or horizontal LinearLayout, half your line's width and create it as ;

View (50dp) - TextView - View (50dp)

Upvotes: 0

Related Questions