0

i'm trying to design my android application so i need to create a separator line with text in the middle. i used this code XML to create the separator line:

   <View
    style="?android:attr/listSeparatorTextViewStyle"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/login_button"
    android:layout_marginTop="20dp"
    android:background="@android:color/white"
/>

but i didn't figure out how to put the text in the middle.

like this : ------- Text ------- but a coherent line

2 Answers2

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:text="-----------------------------"
    android:textColor="#ffff00" />

<TextView
    android:id="@+id/but_book_now"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:background="@android:color/transparent"
    android:text="This is button"
    android:textColor="#ffff00" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:text="-----------------------------"
    android:textColor="#ffff00" />

enter image description here

Yogendra
  • 4,817
  • 1
  • 28
  • 21
0

For future references, here is a simple solution that resizes properly to the parent's width, and shows a continuous divider line.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="40dp">

    <View android:layout_width="match_parent"
          android:layout_height="1dp"
          android:layout_centerVertical="true"
          android:layout_marginStart="40dp"
          android:layout_marginEnd="8dp"
          android:layout_toStartOf="@id/sep_text"
          android:background="@android:color/black"/>

    <TextView android:id="@+id/sep_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:text="misc"/>

    <View android:layout_width="match_parent"
          android:layout_height="1dp"
          android:layout_centerVertical="true"
          android:layout_marginStart="8dp"
          android:layout_marginEnd="40dp"
          android:layout_toEndOf="@id/sep_text"
          android:background="@android:color/black"/>

</RelativeLayout>

The text can be accessed by the sep_text id. The height has been fixed to 40dp but could be changed to wrap_content.

RedGlyph
  • 11,309
  • 6
  • 37
  • 49