stackoverflowuser
stackoverflowuser

Reputation: 33

How to create layout_weight for vertical and horizontal at the same time in a RecyclerView item

I want a layout like below image for the item of a RecyclerView: UI Layout

Below is my XML Item script:

  <?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:orientation="horizontal" android:layout_width="match_parent"
       android:weightSum="1"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/t"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="20sp"
         />

    <TextView
        android:id="@+id/d"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="20sp"
         />

</LinearLayout>

What changes to be needed so that vertical and horizontal weight will be applied on layout at same time using Linear Layout?

Upvotes: 2

Views: 248

Answers (3)

stackoverflowuser
stackoverflowuser

Reputation: 33

Thank you so much for your helps! And this is my exact answer from your helps:

<?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:orientation="horizontal" android:layout_width="match_parent"
   android:weightSum="1"
   android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="0.4">
<TextView
    android:id="@+id/t"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:gravity="center"
    android:textColor="#000000"
    android:textSize="20sp" />

<TextView
    android:id="@+id/d"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:gravity="center"
    android:textColor="#000000"
    android:textSize="20sp" />
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="0.6">
<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.4"/>
<Switch
    android:id="@+id/switch1"
    android:layout_width="0dp"
    android:layout_height="100dp"
    android:layout_weight="0.2"
    android:checked="true" />
</LinearLayout>

Upvotes: 0

Ashish
Ashish

Reputation: 6919

Put it in Linear Layout and provide weight to your each layouts

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

        <LinearLayout
            android:layout_weight="0.5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:id="@+id/myText"
                android:text="Hello"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:id="@+id/newText"
                android:text="NEW Text"/>

        </LinearLayout>

        <TextView
            android:layout_weight="0.5"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:id="@+id/Newtext"/>

    </LinearLayout>

Upvotes: 2

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

If you want to use LinearLayout You can use something like this:

[LinearLayout ]   [LinearLayout ]
[child 1]         [child 1](takes full screen height)
[child 2]

Here is the XML for it:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>



<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Button" />
    </LinearLayout>
</LinearLayout>

It will look like this:

enter image description here

Upvotes: 2

Related Questions