How to display a recyclerview with Cardview in landscape mode

67 Views Asked by At

I have a recyclerview that I want to show in the middle of the fragment FR_RV_Level_Selection_Menu. The app should only be displayed in landscape mode. The recylcerview consists of cardviewItems rv_item_level_selection_menu. Here is my current output: enter image description here

There are 2 problems

  1. The whole recyclerview is on the button of the fragment
  2. The cardviewItems are not displayed in a correct rotation. They should be rotated 90° to the right.

Here is my xml code for "fragment_level_selection_menu.xml"

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

And here is the XML code for the cardView item "rv_item_level_selection_menu.xml"

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="150dp"
    android:layout_height="150dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="10dp"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true"
    app:contentPaddingBottom="2dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:id="@+id/imageView_levelImage"
            android:layout_width="75dp"
            android:layout_height="75dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.507"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="5dp"

            app:srcCompat="@drawable/ic_launcher_background" />

        <TextView
            android:id="@+id/textView_bestResultThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:textSize="10sp"

            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView_levelImage" />


        <TextView
            android:id="@+id/textView_CO2SavingsTotalThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="10sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_bestResultThisLevel" />


        <TextView
            android:id="@+id/textView_gasSavingsTotalThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="10sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_CO2SavingsTotalThisLevel" />



    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

And here is the Java code for the Fragment with the recyclerview "FR_RV_Level_Selection_Menu"

public class FR_RV_Level_Selection_Menu extends Fragment {

    private RecyclerView recyclerView;
    private RV_Adapter_Level_Selection adapter;
    private RecyclerView.LayoutManager layoutManager;

    private ArrayList<RV_Item_Level_Selection_Menu> levelList;

    private FragmentLevelSelectionMenuBinding binding;


    public FR_RV_Level_Selection_Menu() {
        // Required empty public constructor
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        binding = FragmentLevelSelectionMenuBinding.inflate(inflater, container, false);
        buildRecyclerView();
        return binding.getRoot();

    }

    private void buildRecyclerView() {

        recyclerView = binding.recyclerView;
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false);
        levelList = new ArrayList<RV_Item_Level_Selection_Menu>();

        //Database query and processing to get the level information data;
        Cursor res = MainActivity.sqLite_DB.getCursor_TableLevelInfos();
        if(res!=null & res.getCount()>0) {
            while (res.moveToNext() == true) {
                int levelNumber = Integer.parseInt(res.getString(0));
                int bestResultThisLevel = Integer.parseInt(res.getString(2));
                int totalCO2SavingsThisLevel = Integer.parseInt(res.getString(3));
                int gasSavingsTotalThisLevel = Integer.parseInt(res.getString(4));
                int levelUnlockedInt = Integer.parseInt(res.getString(1));
                boolean levelUnlocked = false;
                if (levelUnlockedInt ==1) {
                    levelUnlocked = true;
                }
                boolean   isSelected = false;

                RV_Item_Level_Selection_Menu levelItem = new RV_Item_Level_Selection_Menu(levelNumber, bestResultThisLevel,totalCO2SavingsThisLevel, gasSavingsTotalThisLevel ,levelUnlocked, isSelected);
                levelList.add(levelItem);
            }
        }

        adapter = new RV_Adapter_Level_Selection(levelList, getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new RV_Adapter_Level_Selection.OnItemClickListener() {

            /*
            Define what happens when clicking on an item in the RecyclerView
            */

            @Override
            public void onItemClick(int position) {
                if(levelList.get(position).isSelected()==false) {
                    //Select the selected Level and unselect all other levels
                    if (levelList.get(position).isLevelUnlocked() == true) {
                        for (int i =0; i<levelList.size();i++) {
                            levelList.get(i).setSelected(false);
                        }
                        levelList.get(position).setSelected(true);
                    }



                }

             }
        });

    }//end method buildRecyclerView


}

Any idea what I have to change in order to adjust the 2 points? What I think is strange is regarding 1) I specify app:layout_constraintVertical_bias="0.5" and app:layout_constraintHorizontal_bias="0.5" but still it is not in the middle. And regarding 2) in the layout editor the cardview looks correct both in landscape and portrait mode. So I don't understand why in the actual app it rotates the cardview (whereas in the LayoutEditor it does not do it).

1

There are 1 best solutions below

3
Saloni Tyagi On

To center the recyclerview in the middle of the fragment you can set the constraints to both top and bottom, or left to right of the parent

  <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

and add android:rotation="90"

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="150dp"
    android:layout_height="150dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="10dp"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true"
    app:contentPaddingBottom="2dp"
    android:rotation="90">