1

I'm having a problem with setting the layout in RecyclerView.

here is an layout xml code:

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

    <TextView
        android:id="@+id/dummy_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.5"
        android:background="#ffffff" />

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

</LinearLayout>

here is my MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);

        List<String> list = new ArrayList<>();
        for (int i = 0; i <= 25; i++) {
            list.add("item " + i);
        }

        DummyRVAdapter adapter = new DummyRVAdapter(MainActivity.this, list);
        recyclerView.setAdapter(adapter);

    }

Code for Adapter :

public class DummyRVAdapter extends RecyclerView.Adapter<DummyRVAdapter.Holder> {

    private Context mContext;
    private List<String> mList;

    public DummyRVAdapter(Context context, List<String> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public DummyRVAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_dummy_layout, null);
        //View view =LayoutInflater.from(mContext).inflate(R.layout.item_dummy_layout, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(DummyRVAdapter.Holder holder, int position) {
        holder.textView.setText(mList.get(position));
    }

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

    public class Holder extends RecyclerView.ViewHolder {
        TextView textView;

        public Holder(View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.dummy_text_view);
        }
    }
}

Screenshot of my output.

enter image description here

In my layout XML file, I declare the size for textView as 50% and Button as 30% ,but still, my recyclerView not showing in a correct manner it is not showing in a correct manner.

This layout works fine in Listview.

I don't know where is the problem in my code. I set the Layout manager and adapter to my RecyclerView in the correct manner.

Edit: Please see the ScreenShot it shows that TextView is not allocating 50% of the screen, that's the problem.

It I create complex Layout for Recyclerview its alignment is not in mentioned format.

but that works in Listview

Can anyone help me on this?

Thanks in advance.

Madhan
  • 495
  • 2
  • 10
  • 22

3 Answers3

2

Instead of

View view = View.inflate(mContext, R.layout.item_dummy_layout, null);

use:

View view = LayoutInflater.from(mContext).(R.layout.item_dummy_layout, parent, false);
GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • 1
    @GVillani82 thanks man it works for me as well but can u please explain whats the difference? – Rahul Aug 17 '17 at 05:31
0

As android:weightSum="1" but sum of all android:layout_weight was not equal to 1 so this was not showing properly

you have declare the size for textView as 50% and Button as 30% but rest remaining is 20% due to this reason it is not showing in proper way. so can do as either increase textview to 70% and Button to 30% so android:weightSum and android:layout_weight sum will be equal.

Change your layout to

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

    <TextView
        android:id="@+id/dummy_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:background="#ffffff" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="Button"
        android:textAllCaps="false" />
</LinearLayout >
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

Its not just weight that is having problem its the view textview may be occupying its size but it's text are samller than button size
one important thing Even after weight or without it use minsize and max size in button to fit it according to need like in your layout do this:

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

<TextView
    android:id="@+id/dummy_text_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.5"
    android:background="#ffffff" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="Button"
     android:max_height="30dp"
    android:textAllCaps="false" />

DevKRos
  • 422
  • 2
  • 15