0

I'm using AndroidStudio3.2.1. I'm designing a recycler view, when running it using emulator, it does not fill the whole width of the activity. Why is this happening?

I tried "fill_parent", "layout_weight" but none of that worked. I have a CardView that contains the components that should appear in the view holder.

The layout of the main activity

<?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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

The layout of list_items that will be displayed in each ViewHolder

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="0"
        app:cardBackgroundColor="@color/colorPrimary"
        app:cardCornerRadius="5dp"
        app:cardElevation="6dp">


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

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

                <TextView
                    android:id="@+id/textViewTeacher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextView"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Section 1"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Database 1"
                    android:textSize="18sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical">

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


    </android.support.v7.widget.CardView>
</LinearLayout>

The code for MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SectionAdapter adapter;
    private List<Section> sectionList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerView);
        sectionList = new ArrayList<Section>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        Section sec = null;
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);
        sec = new Section("Database 1", "Section 1", "John Stanford");
        sectionList.add (sec);

        adapter = new SectionAdapter(this,sectionList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);

    }
}

The code for the Adapter

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class SectionAdapter extends RecyclerView.Adapter<SectionAdapter.SectionViewHolder> {
    private Context context;
    private List<Section> sectionList;

    public SectionAdapter(Context context, List<Section> sectionList) {
        this.context = context;
        this.sectionList = sectionList;
    }

    @NonNull
    @Override
    public SectionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view  = inflater.inflate(R.layout.list_layout,null);
        return new SectionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SectionViewHolder holder, int position) {
        Section sec = sectionList.get(position);
        holder.setValues(sec);
    }

    @Override
    public int getItemCount() {
        return sectionList.size();
    }

    class SectionViewHolder extends RecyclerView.ViewHolder
    {
        private TextView nameTV;
        private TextView idTV;
        private TextView teacherTV;

        public SectionViewHolder(View itemView) {
            super(itemView);
            nameTV = itemView.findViewById(R.id.textViewName);
            idTV = itemView.findViewById(R.id.textViewID);
            teacherTV = itemView.findViewById(R.id.textViewTeacher);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = getAdapterPosition();
                    Toast.makeText(context, "Laptop "+pos+" has been clicked", Toast.LENGTH_LONG).show();
                }
            });
        }

        public void setValues(Section sec)
        {
            nameTV.setText(sec.getName());
            idTV.setText(sec.getId());
            teacherTV.setText(sec.getTeacher());
        }
    }
}

The code for Section.java

public class Section {
    private String name;
    private String id;
    private String teacher;

    public Section(String name, String id, String teacher) {
        this.name = name;
        this.id = id;
        this.teacher = teacher;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTeacher() {
        return teacher;
    }

    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
}

The result when running on the emulator

  • 1
    Are you sure you're inflating the item layout correctly in your `RecyclerView.Adapter`? Do the items have different widths when the data is different in each? – Mike M. May 02 '19 at 23:21
  • I will provide my java code. May be anyone find whats going wrong. – Osama Al-Haj Hassan May 03 '19 at 07:32
  • Yeah, change the `inflate()` call in `onCreateViewHolder()` to `inflater.inflate(R.layout.list_layout, parent, false)`. – Mike M. May 03 '19 at 07:37

2 Answers2

0

Changing your recycler view to this should fix it.

 <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
Tristan O'Keefe
  • 371
  • 2
  • 4
  • 14
0

I managed to fix it. Thanks for the hint from Mike M.

I was not inflating the layout properly.

I changed "View view = inflater.inflate(R.layout.list_layout,null);
to "View view = inflater.inflate(R.layout.list_layout,parent,false);"