adi
adi

Reputation: 1130

how to align android chips to the end in chipgroup?

Hi I have a chipgroup and I dynamically create chips and add to chipgroup.

However I want the chips to be at the right end of chipgroup but it is always to the left as shown here.

enter image description here

I want the chips Text:0 and Text:1 to be at the right most end.

This is my code:

 <RelativeLayout
     ....
         >

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextTitle"
        android:layout_alignBaseline="@id/chipGroup"
        android:gravity="end|bottom"
        android:layout_marginTop="16dp"
        android:layout_below="@id/toptv"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="14sp"
        android:id="@+id/tt1"
        />

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@id/tt1"
        android:layout_below="@id/toptv"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:textSize="12sp"
        />
     </RelativeLayout>

This is how I dynamically add chips to chip group:

    Chip chip;

    for(int i=0;i<2;i++){
        chip = new Chip(this);
        chip.setText("Text:"+i);
        chip.setChipBackgroundColorResource(android.R.color.darker_gray);
        chip.setTextColor(Color.WHITE);
        mChipGroup.addView(chip);
    }

I searched certain SOF posts but wasn't able to align the chips to the right.Any help will be really helpful.

Upvotes: 1

Views: 3738

Answers (4)

Noha
Noha

Reputation: 94

I used this and it worked for me

add this line: android:layoutDirection="rtl"

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/questionLayout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layoutDirection="rtl"
            >


        </com.google.android.material.chip.ChipGroup>

Upvotes: 1

Meysam Hadigheh
Meysam Hadigheh

Reputation: 321

You can add scaleX=-1 to your chipGroup and also your chips should have scaleX=-1

Upvotes: 0

Sanjay Hadiya
Sanjay Hadiya

Reputation: 945

Just Replace it

 <RelativeLayout
 ....
     >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextTitle"
    android:layout_alignBaseline="@+id/chipGroup"
    android:layout_toLeftOf="@+id/chipGroup"
    android:gravity="end|bottom"
    android:layout_marginTop="16dp"
    android:layout_below="@+id/toptv"
    android:textColor="@android:color/white"
    android:textStyle="bold"
    android:textSize="14sp"
    android:id="@+id/tt1"
    />

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chipGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/toptv"
    android:textColor="@android:color/black"
    android:textStyle="bold"
    android:textSize="12sp"
    />
 </RelativeLayout>

Upvotes: 0

Mehul Solanki
Mehul Solanki

Reputation: 1141

You need to change your ChipGroup group property. here is changed code.

<RelativeLayout
 ....
     >

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextTitle"
    android:layout_alignBaseline="@id/chipGroup"
    android:gravity="end|bottom"
    android:layout_marginTop="16dp"
    android:layout_below="@id/toptv"
    android:textColor="@android:color/white"
    android:textStyle="bold"
    android:textSize="14sp"
    android:id="@+id/tt1"
    />

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chipGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@id/tt1"
    android:layout_below="@id/toptv"
    android:textColor="@android:color/black"
    android:textStyle="bold"
    android:gravity="end"
    android:textSize="12sp"
    />
 </RelativeLayout>

I hope this will work. if not then post your RelativeLayout on your question.

Upvotes: 0

Related Questions