Pupkin
Pupkin

Reputation: 1111

TextView between two elements

I have an Android app and I created a view. This view contains RelativeLayout and it contains 3 child elements: CheckBox, TextView and Button. CheckBox is placed at the left parent's side, Button is placed at the right side and TextView between those elements. TextView can contain a text with arbitrary length and if the text is very long, TextView must not overlap neither ChecBox nor Button. Now it looks like this:

enter image description here

But I want this:

enter image description here

Is it possible to place elements properly?

Upvotes: 0

Views: 255

Answers (8)

Arbaz.in
Arbaz.in

Reputation: 1558

No need to put Textview between checkBox and Button, You can do with 2 elements (CheckBox and Button). what you have to do just set the text to checkbox then set button.

For better understanding read below code:

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

    <CheckBox
        android:text="Some bla blablablablablablabla bla bla bla bla bla bla "
        android:layout_width="0dp"
        android:layout_weight="0.7"

        android:layout_height="wrap_content" />
    <Button
        android:text="Button"
        android:layout_width="0dp"
        android:layout_weight="0.3"
        android:layout_height="wrap_content" />
</LinearLayout>

Upvotes: 0

InsaneCat
InsaneCat

Reputation: 2161

Try this XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Some Long text Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button2"
            app:layout_constraintStart_toEndOf="@+id/checkBox"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent" />    
    </android.support.constraint.ConstraintLayout>
</LinearLayout>

enter image description here

Hope this may help you.

Upvotes: 0

Vinay Rathod
Vinay Rathod

Reputation: 1332

You can simply add weight to textview it will adjust automatically

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

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="large text view that not overlay neither checkbox nor button" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

Upvotes: 0

Nouman Ch
Nouman Ch

Reputation: 4121

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_toRightOf="@id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim"
        android:layout_toLeftOf="@id/btn" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="button"
        android:layout_alignParentRight="true" />

</RelativeLayout>

Upvotes: 0

Juanjo Berenguer
Juanjo Berenguer

Reputation: 789

Using ConstraintLayout you can get this:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="this is a long text bla bla bla la this is a long text bla bla bla la"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/button2"
                app:layout_constraintStart_toEndOf="@+id/checkBox"
                app:layout_constraintTop_toTopOf="parent" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:text="Button"
                app:layout_constraintEnd_toEndOf="parent" />


        </android.support.constraint.ConstraintLayout>
    </LinearLayout>

Result: enter image description here

Upvotes: 0

Hamid Reza
Hamid Reza

Reputation: 664

You can use this

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="button" />

    <TextView
        android:text="this is my test this is my test this is my test this is my test this is my test "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/checkBox"
        android:layout_toLeftOf="@id/button"
        android:layout_toRightOf="@id/checkBox"
        android:layout_toStartOf="@id/button" />

</RelativeLayout>

enter image description here

Upvotes: 1

Angel Koh
Angel Koh

Reputation: 13525

a linearLayout (instead of relativelayout) wrapping the 3 views should do the trick. just remember to set the textview's weight to 1 and width to 0

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

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

<TextView 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:weight="1"  />

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

</LinearLayout>

Upvotes: 1

Prashanth Verma
Prashanth Verma

Reputation: 588

You can do it like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/textView" />

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Helovusndijfsdufsdjfkjdsbfkjsdkjfbskjdbfkjsbdfkjbskdjfbskjbdfkjbfsdf"
    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.005"
    app:layout_constraintStart_toEndOf="@+id/checkBox"
    app:layout_constraintTop_toTopOf="@+id/button" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

This is how it would look:

enter image description here

Upvotes: 0

Related Questions