TextViews with dividers in between them
Asked Answered
R

5

7

I am trying to recrate following layout in android studio:

Preview image

Because i am preety new at android stuff, i first tryed with LinearLayout, and figured thath this probably wont be possible with it. Now i am trying with RelativeLayout i already created this block with color:

        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="80dp"
                android:background="@color/red_top">
        </RelativeLayout>

Now i want to ask how can i divide it like it is shown at the image 1 bar at top and 2 at the bottom in the same layout, and how can i put same borders?

Rabbit answered 26/8, 2013 at 13:7 Comment(4)
He said he specifically needs them in the same Layout; the TableLayout that bakriOnFire suggested is also great.Bifilar
Its in the same parent layout(Linear), and inside it,the layout nesting is done.Salman
Ah, yes you are right. My apologies.Bifilar
its all right...!!! no need to apologise.Salman
S
20

You can do this by using the following xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/custom_toast_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CD96CD"
        android:text="TEXT" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#CD96CD"
            android:text="TEXT" />

        <View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#CD96CD"
            android:text="TEXT" />
    </LinearLayout>

</LinearLayout>
Salman answered 26/8, 2013 at 13:17 Comment(3)
How can i add another text view with the bottom line so i will have like Text1 Text2 | Text3 Text4 If i try to add just another textview it expand the right over the screen.Rabbit
At the bottom line now i have 2 text views. How can i add 2 more thath i will have like on the left side: Wins: 0 (2 text views), on the right Lost: 0 (2 text views)?Rabbit
see either you can set text through settext like textview.settext("Wins: "+ no.of wins) ,this will save an additional textview else you can do it by the code I'm posting below.Salman
B
1
<?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" >

<LinearLayout 
    android:id="@+id/FirstLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/FirstTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_ONE" />
</LinearLayout>
<LinearLayout
    android:id="@+id/SecondLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/FirstLinearLayout"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/SecondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_TWO" />

    <TextView
        android:id="@+id/ThirdTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_Three" />
</LinearLayout>

First you will need a container to hold all of your components inside; in my example the container I am talking about is the RelativeLayout. Relative Layouts allow their children to be placed in their position using the corresponding IDs. Take a look at how I positioned the other two LinearLayouts using android:layout_below="@+id/FirstLinearLayout".

If you really insist on having them in the same Layout, use a relative layout and position the TextViews as follows:

<?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" >


    <TextView
        android:id="@+id/FirstTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_ONE" />


    <TextView
        android:id="@+id/SecondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/FirstTextView"
        android:text="TEXT_TWO" />

    <TextView
        android:id="@+id/ThirdTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/FirstTextView"
        android:layout_toRightOf="@+id/SecondTextView"
        android:text="TEXT_Three" />


</RelativeLayout>

I hope this helps you out.

Bifilar answered 26/8, 2013 at 13:18 Comment(0)
M
1

A TableLayout is the best option, to accomplish your need.

<TableLayout>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="Text" />

    </TableRow>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text" />

    </TableRow>
</TableLayout>

Note the layout_span attribute used in the first TextView to span tow columns.

Mcfall answered 26/8, 2013 at 13:18 Comment(0)
S
1

You can try this to add additional TextViews

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#CD96CD"
    android:text="TEXT" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />
     <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#000000" />
     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />
     <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#000000" />
     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />
</LinearLayout>

Salman answered 27/8, 2013 at 3:51 Comment(0)
S
0

Adding this view; draw you a seperator between your textviews

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />
Subtype answered 19/12, 2016 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.