setLayouParams didn't work in cardView

730 views Asked by At

I have two activities each of them bounded to the same adapter one of them when I click on item it opens the second activity with all data, the cardview width is wrap_content it works fine in first activity but in second I want the width to be match_parent where I make the layout horizontal I try setLayoutParams() but did not work

public AzkaarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    CardView cardView=(CardView)parent.findViewById(R.id.cardview);

    CardView.LayoutParams layoutParams = (CardView.LayoutParams)cardView.getLayoutParams();
    layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
    cardView.setLayoutParams(layoutParams);
    ...

And I have this error

java.lang.NullPointerException
at com.nourabosily.aa.azkaar.AzkaarAdapter.onCreateViewHolder(AzkaarAdapter.java:37)
at com.nourabosily.aa.azkaar.AzkaarAdapter.onCreateViewHolder(AzkaarAdapter.java:23)

this is the code of adapter

public class AzkaarAdapter extends RecyclerView.Adapter<AzkaarAdapter.AzkaarViewHolder>  {
    ArrayList<AzkarForm> azkarForms=new ArrayList<AzkarForm>();
    Context ctx;

public  AzkaarAdapter (ArrayList<AzkarForm> azkarForms,Context ctx){
    this.azkarForms=azkarForms;
    this.ctx=ctx;
}
@Override
public AzkaarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        CardView cardView=(CardView)parent.findViewById(R.id.cardview);
    CardView.LayoutParams layoutParams = (CardView.LayoutParams)cardView.getLayoutParams();
    layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
    cardView.setLayoutParams(layoutParams);
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false);
    AzkaarViewHolder azkaarViewHolder=new AzkaarViewHolder(view,ctx,azkarForms);
    ViewGroup viewGroup=(ViewGroup)parent.getParent();
    CardView.LayoutParams params=(CardView.LayoutParams)(viewGroup.getLayoutParams());
    params.height=800;
    return azkaarViewHolder;

}


@Override
public void onBindViewHolder(AzkaarViewHolder holder, int position) {


    AzkarForm azkarForm=azkarForms.get(position);
    if(ctx instanceof MainActivity) {
        holder.sample.setText(azkarForm.getSample());
        holder.number.setText(String.valueOf(azkarForm.getNumber()));
    }else{
        holder.sample.setText(azkarForm.getComplete_morning());
        holder.number.setText(String.valueOf(azkarForm.getNumber()));
    }
    int width =holder.itemView.getWidth();
    ViewGroup.LayoutParams params=holder.itemView.getLayoutParams();
    params.height= RelativeLayout.LayoutParams.MATCH_PARENT;
    holder.itemView.setLayoutParams(params);

}

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

public static class AzkaarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    TextView sample, number;
    Context ctx;
    ArrayList<AzkarForm> azkarForms = new ArrayList<AzkarForm>();

    public AzkaarViewHolder(View itemView, Context ctx, ArrayList<AzkarForm> azkarForms) {
        super(itemView);
        this.ctx = ctx;
        if(this.ctx instanceof MainActivity)
            itemView.setOnClickListener(this);
        this.azkarForms = azkarForms;
        sample = (TextView) itemView.findViewById(R.id.sample);
        number = (TextView) itemView.findViewById(R.id.number);

    }


    @Override
    public void onClick(View v) {
        int position=getAdapterPosition();
        AzkarForm azkarForm=azkarForms.get(position);
        Intent intent=new Intent(this.ctx,DetailsActivity.class);
        intent.putExtra("numbers",azkarForm.getNumber());
        intent.putExtra("sample",azkarForm.getSample());
        intent.putExtra("complete",azkarForm.getComplete_morning());
        intent.putExtra("position",position);
        this.ctx.startActivity(intent);

    }
}

}

and this is the code of layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearlayout">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cardview">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:id="@+id/relative">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sample"
            android:text="Noura Ali Bosily"
            android:textSize="20dp"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_alignParentTop="true"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="20dp"
            android:layout_alignParentRight="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/number"
            android:text="5"
            android:textSize="20dp"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="20dp"
            android:layout_alignParentLeft="true"/>

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

2

There are 2 answers

5
Rushi Ayyappa On BEST ANSWER
            @Override
        public AzkaarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false);
            AzkaarViewHolder azkaarViewHolder=new AzkaarViewHolder(view,ctx,azkarForms);

            return azkaarViewHolder;

        }


        @Override
        public void onBindViewHolder(AzkaarViewHolder holder, int position) {


            CardView.LayoutParams layoutParams = (CardView.LayoutParams)holder.cardView.getLayoutParams();
            layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
        holder.cardView.setLayoutParams(layoutParams);
            AzkarForm azkarForm=azkarForms.get(position);
     ViewGroup viewGroup=(ViewGroup)holder.cardview.getParent();
            CardView.LayoutParams params=(CardView.LayoutParams)(viewGroup.getLayoutParams());
            params.height=800;

            if(ctx instanceof MainActivity) {
                holder.sample.setText(azkarForm.getSample());
                holder.number.setText(String.valueOf(azkarForm.getNumber()));
            }else{
                holder.sample.setText(azkarForm.getComplete_morning());
                holder.number.setText(String.valueOf(azkarForm.getNumber()));
            }
            int width =holder.itemView.getWidth();
            ViewGroup.LayoutParams params=holder.itemView.getLayoutParams();
            params.height= RelativeLayout.LayoutParams.MATCH_PARENT;
            holder.itemView.setLayoutParams(params);

        }

public static class AzkaarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    TextView sample, number;
    Context ctx;
    CardView cardView
    ArrayList<AzkarForm> azkarForms = new ArrayList<AzkarForm>();

    public AzkaarViewHolder(View itemView, Context ctx, ArrayList<AzkarForm> azkarForms) {
        super(itemView);
        this.ctx = ctx;
        if(this.ctx instanceof MainActivity)
            itemView.setOnClickListener(this);
        this.azkarForms = azkarForms;
        sample = (TextView) itemView.findViewById(R.id.sample);
        number = (TextView) itemView.findViewById(R.id.number);
        cardView=(CardView)parent.findViewById(R.id.cardview);

    }


    @Override
    public void onClick(View v) {
        int position=getAdapterPosition();
        AzkarForm azkarForm=azkarForms.get(position);
        Intent intent=new Intent(this.ctx,DetailsActivity.class);
        intent.putExtra("numbers",azkarForm.getNumber());
        intent.putExtra("sample",azkarForm.getSample());
        intent.putExtra("complete",azkarForm.getComplete_morning());
        intent.putExtra("position",position);
        this.ctx.startActivity(intent);

    }
4
Binil On
CardView.LayoutParams layoutParams = (CardView.LayoutParams)cardView.getLayoutParams();
layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
cardView.setLayoutParams(layoutParams);

Instead try using,

LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
holder.cardView.setLayoutParams(params);

do it in

@Override
public void onBindViewHolder(AzkaarViewHolder holder, int position) {

cardView should be in ViewHolder.