skip to Main Content

I am trying to allign my cardViews horizontally inside my recyclerView, but when i change the orientation attribute in the XML nothing changes. Does anyone know which other factors are involved? Could there be a problem in the adapter or the card_layout file?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".UI.Game.GuessWordFragment">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_marginTop="36dp"
    android:layout_marginBottom="137dp"
    android:orientation="horizontal"
    android:scrollbars="horizontal"
    app:layout_constraintBottom_toTopOf="@+id/inputField"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/CategoryText" />


<EditText
    android:id="@+id/inputField"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:ems="10"
    android:hint="Type your guess"
    android:imeOptions="actionDone"
    android:inputType="textPersonName"
    android:textAlignment="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recyclerView" />

<Button
    android:id="@+id/submitBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:text="Submit"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/inputField" />

<TextView
    android:id="@+id/CategoryText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="TextView"
    android:textSize="20sp"
    app:layout_constraintBottom_toTopOf="@+id/recyclerView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>

2

Answers


  1. When you create your layoutManager Class, use LinearLayoutManager.HORIZONTAL

    val layoutManager
        = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
    
    binding.recyclerview.setLayoutManager(layoutManager)
    
    Login or Signup to reply.
  2. If you want recyclerview allign VERTICAL

    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    

    And for HORIZONTAL

    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    

    just add this line in your code…
    Thank You…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search