I am trying to implement below layout. I want to position the items in such a way that it takes full width of the screen:
Expected Layout:
Actual Layout:
This is the code for item of my Recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/size_8dp"
android:paddingStart="@dimen/size_20dp"
android:paddingEnd="@dimen/size_20dp"
android:paddingBottom="@dimen/size_8dp">
<ImageView
android:id="@+id/legendsItemIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/text_legend_status_name"
/>
<TextView
android:id="@+id/text_legend_status_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Low"
android:textSize="@dimen/text_size_8sp"
app:layout_constraintTop_toBottomOf="@+id/legendsItemIcon"
app:layout_constraintStart_toStartOf="@+id/legendsItemIcon"
app:layout_constraintEnd_toEndOf="@+id/legendsItemIcon"
app:layout_constraintBottom_toBottomOf="parent"
android:textColor="@color/legends_item_status_text_color"
android:layout_marginTop="@dimen/size_4dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
How can I achieve this?
This custom class will help you https://gist.github.com/heinrichreimer/39f9d2f9023a184d96f8
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class SpanningLinearLayoutManager extends LinearLayoutManager {
public SpanningLinearLayoutManager(Context context) {
super(context);
}
public SpanningLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public SpanningLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return spanLayoutSize(super.generateDefaultLayoutParams());
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
return spanLayoutSize(super.generateLayoutParams(c, attrs));
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
return spanLayoutSize(super.generateLayoutParams(lp));
}
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
return super.checkLayoutParams(lp);
}
private RecyclerView.LayoutParams spanLayoutSize(RecyclerView.LayoutParams layoutParams){
if(getOrientation() == HORIZONTAL){
layoutParams.width = (int) Math.round(getHorizontalSpace() / (double) getItemCount());
}
else if(getOrientation() == VERTICAL){
layoutParams.height = (int) Math.round(getVerticalSpace() / (double) getItemCount());
}
return layoutParams;
}
@Override
public boolean canScrollVertically() {
return false;
}
@Override
public boolean canScrollHorizontally() {
return false;
}
private int getHorizontalSpace() {
return getWidth() - getPaddingRight() - getPaddingLeft();
}
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
}
Set RecyclerView layoutManager to SpanningLinearLayoutManager to instead of LinearLayoutManager
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With