Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Align TextView between two dividers horizontally

Here is what I am trying to achieve:

Here is how I tried:

            <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/padd_loading_btn"
            android:layout_marginBottom="@dimen/padd_loading_btn" >

            <View
                android:layout_width="65dp"
                android:layout_height="1dp"
                android:layout_gravity="center_vertical"
                android:background="@android:color/darker_gray" />

            <TextView
                android:id="@+id/orLoginWithEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="@string/orlogin_withEmail"
                android:textSize="12sp" />

            <View
                android:layout_width="65dp"
                android:layout_height="1dp"
                android:layout_gravity="center_vertical"
                android:background="@android:color/darker_gray" />
        </LinearLayout>

This looks ok for my HD display but for others it looks awful.

How do I make the text wrap as much as it needs, and then the dividers equally align on sides?

p.s. Of course I can set a relative layout and inside to put a divider to fill in width and the textview with a white background above with padding, but i don't think this is the best solution.

like image 895
Filip Luchianenco Avatar asked May 19 '26 11:05

Filip Luchianenco


2 Answers

Try like this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padd_loading_btn"
    android:layout_marginBottom="@dimen/padd_loading_btn" >

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/orLoginWithEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center_vertical"
        android:text="@string/orlogin_withEmail"
        android:textSize="12sp" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:background="@android:color/darker_gray" />
</LinearLayout>
like image 140
Mike Avatar answered May 21 '26 00:05

Mike


You can try with below code, you can change TextViews to view.

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

    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:background="#cccccc" />

    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.8"
                android:gravity="center_horizontal"
                android:background="#ffffff"
                android:text="Hello Android" />


    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:background="#cccccc" />

</LinearLayout>

Here is the output

Image

like image 25
Aniruddha Avatar answered May 21 '26 01:05

Aniruddha