Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to center text of chip with fixed size?

I have a chip with 100dp of width but the text is not centered how I can center the text.

I use androidx with material library, I've tried put android:textAlignment="center" and android:gravity="center" but not work

<com.google.android.material.chip.Chip
    android:id="@+id/chip"
    style="@style/Widget.MaterialComponents.Chip.Choice"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="7:00" />

I have this

chip

I want this

enter image description here

like image 815
Jefferson Rojas Avatar asked Aug 30 '19 17:08

Jefferson Rojas


People also ask

How do you center chips in ChipGroup?

Put the width of chip group into wrap_content and make the parent view to gravity to center which contains chip group. So that it will be aligned center.

What is chip button in Android?

Chips are compact elements that represent an attribute, text, entity, or action. They allow users to enter information, select a choice, filter content, or trigger an action.

What is chip group in Android Studio?

A ChipGroup is used to hold multiple Chip s. By default, the chips are reflowed across multiple lines. Set the app:singleLine attribute to constrain the chips to a single horizontal line. If you do so, you'll usually want to wrap this ChipGroup in a HorizontalScrollView .

What are chips in Ux?

Chips allow users to enter information, make selections, filter content, or trigger actions. While buttons are expected to appear consistently and with familiar calls to action, chips should appear dynamically as a group of multiple interactive elements.


2 Answers

just now I faced with the same problem, and I solved it by set a chip property: android: textAlignment = "center". I tested your example and it works fine too, here the code that I tested:

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <com.google.android.material.chip.Chip
            android:id="@+id/chip"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="7:00"
            android:textAlignment="center"/>
</FrameLayout>

Also make sure that you don't set or change a chip's text alignment somewhere in your code.

like image 179
easy_breezy Avatar answered Oct 11 '22 14:10

easy_breezy


The short answer:

Chips aren't meant to be used the way you are trying to use them. They are supposed to wrap your content. Therefore there isn't a clean way to align the text in the center. There is a workaround tho, you can use Chip_textEndPadding and Chip_textStartPadding attributes, which will be kinda awkward I guess.

I don't really know what you are trying to achieve, I mean, what is your why? Is it a button? Is it suppose just to show some text? Please describe the feature, or at least, part of it.

Anyway:

According to the material design guidelines

Chips allow users to enter information, make selections, filter content, or trigger actions. Chips should appear dynamically as a group of multiple interactive elements. Unlike buttons, which should be a consistent and familiar call to action, one that a user expects to appear as the same action in the same general area.

Does your feature as anything to do with this?

In case you want a clickable, circular component you can simply use material button.

There is a similar question that was asked at github.

like image 30
San Mo Avatar answered Oct 11 '22 13:10

San Mo