0

In my layout I have a TextView inside of a RelativeLayout like below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="80dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="70dp"
        android:layout_marginStart="70dp"
        android:layout_marginTop="12dp"
        android:layout_toLeftOf="@+id/imageButton2"
        android:layout_toStartOf="@+id/imageButton2"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold" />

The left margins are explicitly set to 70dp in this case. However, I would like to conditionally decrease the left margin to 30dp or so. My attempt to do so is here:

@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
    final Material m = ...
    ViewHolder h = (ViewHolder) holder;

    h.checkedImageview.setVisibility(View.GONE);

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) h.titleTextview.getLayoutParams();
    layoutParams.setMargins(
            20,
            layoutParams.topMargin,
            layoutParams.rightMargin,
            layoutParams.bottomMargin
    );
    h.titleTextview.setLayoutParams(layoutParams);

^^ However, that is having no effect at all. If I modify the values in the XML layout, will it work without any issue? What am I doing wrong? I'm using the SectionedRecyclerViewAdapter RecyclerView library, if that matters at all.

Sachin Aggarwal
  • 1,095
  • 8
  • 17
mango
  • 5,577
  • 4
  • 29
  • 41

2 Answers2

0

for example :

public static class PersonViewHolder extends RecyclerView.ViewHolder {

CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;

PersonViewHolder(View itemView) {
    super(itemView);
    cv = (CardView)itemView.findViewById(R.id.cv);
    personName = (TextView)itemView.findViewById(R.id.person_name);
    personAge = (TextView)itemView.findViewById(R.id.person_age);
    personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
}

}

so i read the git hub page justo do it :

    @Override
public void onBindItemViewHolder(PersonViewHolder holder, int position) {

  //here --->>>
  PersonViewHolder itemHolder = (PersonViewHolder) holder; // important !

    holder.checkedImageview.setVisibility(View.GONE);

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.titleTextview.getLayoutParams();
    layoutParams.setMargins(
            20,
            layoutParams.topMargin,
            layoutParams.rightMargin,
            layoutParams.bottomMargin
    );
    holder.titleTextview.setLayoutParams(layoutParams);
White Druid
  • 295
  • 1
  • 12
0

The issue is that you're reusing the existing LayoutParams from the text view. Instead of reusing, create a new RelativeLayout.LayoutParams object, set its margins, and then set the TextView's params appropriately. Code:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
    (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

params.setMargins(0, 0, 0, 0); // or any other values

textView.setLayoutParams(params);
AlexOlsen
  • 187
  • 2
  • 10