0

I am using android.support.design.chip.Chip and the problem I get is that when I set the text dynamically by setText("..."), the text wrongly appears by overlapping the icon, but when I set it on the xml file as app:chipText="Hello" it appears correctly.

here you have my code:

activity.java:

        Chip x= new Chip(mView);
    x.setChipDrawable(ChipDrawable.createFromResource(mView, R.xml.chip_style));

chip_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<chip xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.MaterialComponents.Chip.Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.MaterialComponents.Chip"
    app:chipIcon="@drawable/ic_person"/>

---------------------SOLUTION---------------------

Then, the solution was to use Chip method setChipText(CharSequence text)

mantc_sdr
  • 451
  • 3
  • 17

2 Answers2

0

instead of

android.support.design.chip.Chip

use

implementation 'com.google.android.material:material:1.0.0-beta01'

<com.google.android.material.chip.Chip
    style="@style/Widget.MaterialComponents.Chip.Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chip_text"
    app:chipIcon="@drawable/ic_avatar_circle_24"/>

dynamically

val chip = Chip(context)
chip.text = "Name Surname"
chip.chipIcon = ContextCompat.getDrawable(requireContext(), baseline_person_black_18)
chip.isCloseIconEnabled = true
// following lines are for the demo
chip.setChipIconTintResource(R.color.chipIconTint)
chip.isClickable = true
chip.isCheckable = false
chipGroup.addView(chip as View)
chip.setOnCloseIconClickListener { chipGroup.removeView(chip as View) }

detailed reference: https://medium.com/material-design-in-action/chips-material-components-for-android-46001664a40f

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
  • The thing is that Material Design advises not to use both libraries together, and I need android.support.design for other UI elements – mantc_sdr Mar 05 '19 at 11:39
  • yes you are right. If this is the case then please first confirm that you are using Chip class from design library the complete name is android.support.design.chip.Chip. – Nouman Ch Mar 05 '19 at 11:42
  • The error looks to happen when I set the text by using setText(..), but if I set it by default in the xml file, appears correctly... – mantc_sdr Mar 05 '19 at 12:52
0

I finally noticed my error, I was using:

trabajador.setText(FunctionsUtil.safeCursorGetValue(trabajadorData, KEY_NOMBRE));

instead of:

trabajador.setChipText(FunctionsUtil.safeCursorGetValue(trabajadorData, KEY_NOMBRE));

Then, the solution was to use Chip method setChipText(CharSequence text)

mantc_sdr
  • 451
  • 3
  • 17