Search code examples
javaandroidandroid-chips

GetText() for a chipgroup (entry)


I'm quite new in java/android and i need for a project to implement chip (entry). I followed a tutorial and the chip works. what i made

XML

<Button
    android:id="@+id/email_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonClick"
    android:text="Ajouter"
    android:textSize="10sp"
    app:layout_constraintBottom_toTopOf="@+id/chip_group"
    app:layout_constraintLeft_toRightOf="@+id/fields_2_form"
    app:layout_constraintTop_toBottomOf="@+id/title_field_3">

</Button>
<com.google.android.material.chip.ChipGroup
    android:id="@+id/chip_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    app:layout_constraintTop_toBottomOf="@+id/fields_2_form">

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

Activity

  public void buttonClick(View view) {
    final EditText fieldForm2 = findViewById(R.id.fields_2_form);
    final ChipGroup chipGroup = findViewById(R.id.chip_group);
    final Button emailButton = findViewById(R.id.email_button);

    final Chip chip = new Chip(this);
    ChipDrawable drawable = ChipDrawable
    .createFromAttributes(this, null, 0, R.style.Widget_MaterialComponents_Chip_Entry);
    chip.setChipDrawable(drawable);
    chip.setCheckable(false);
    chip.setClickable(false);
    chip.setChipIconResource(R.drawable.ic_fingerprint_black_24dp);
    chip.setIconStartPadding(3f);
    chip.setPadding(60, 10, 60, 10);
    chip.setText(fieldForm2.getText().toString());

    // remove chip on click on the -
    chip.setOnCloseIconClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chipGroup.removeView(chip);
        }
    });
    if (!EmailValid(fieldForm2.getText().toString())) {
        Toast.makeText(getApplicationContext(),
                "Veuillez rentrer un email valide",
                Toast.LENGTH_LONG).show();
        fieldForm2.setText("");
    } else {

        chipGroup.addView(chip);
        fieldForm2.setText("");
    }
}

Well but my problem is when i need to get all the typed emails from the chip for another activity. I tried to do it in this method :

public Intent getFormInfos() {

    TextView mEditTextHour = findViewById(R.id.fields_1_form);
    final Spinner spin = (Spinner) findViewById(R.id.Spinner);
    final ChipGroup chipGroup = findViewById(R.id.chip_group);


    //FIXME
    String chiptext = chipGroup.INEEDTOGETCHIPTEXTHERE();


    String myEditedText1 = mEditTextHour.getText().toString();
    String spinnerText = spin.getSelectedItem().toString();


    Intent intent = new Intent(this, ActivityListMareu.class);
    ApiService.getmReunions().add(new Reunion(myEditedText1, spinnerText, chiptext));


    return intent;
}

I need to do something like chipgroup.getText() but it doesn't work i tried some stuff but i'm really confused with chip and actually i've no idea how to make it. Does someone have an idea for this?
(sorry for my bad english)


Solution

  • Using this you can get all the text set over all Chip in that ChipGroup.

            ArrayList<String> emails = new ArrayList<>();
    
            for (int i = 0; i < chipGroup.getChildCount(); i++) {
                String email = ((Chip) chipGroup.getChildAt(i)).getText().toString();
                emails.add(email);
            }
    

    In your case if you have only one chip in chipGroup you can directly :

            if (chipGroup.getChildCount() > 0) {
                CharSequence yourText = ((Chip) chipGroup.getChildAt(0)).getText();
            }