1

How to get the selected Chip item text from ChipGroup via OnclickListener. Here's what I've done so far. I'm not able toe get the selected item to the ClipBoard:

            for(int i=0; i<tagsJsonArray.length();i++){
                try {
                    String tag;
                    tag = tagsJsonArray.getString(i);
                    LayoutInflater inflater = LayoutInflater.from(YTTags.this);
                    Chip chip = (Chip) inflater.inflate(R.layout.my_tag,null,false);
                    chip.setText(tag);
                    chip.setClickable(true);
                    chip.setCheckable(true);
                    chipGroup.addView(chip);
                    chip.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            ClipboardManager copytag = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            ClipData clip = ClipData.newPlainText("tag", "Hi");
                            copytag.setPrimaryClip(clip);
                            Toast.makeText(YTTags.this, "Copied",Toast.LENGTH_LONG).show();
                        }
                    });
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }

Onclick listener word fine and copies the dummy text that I've set in the ClipData clip. But I want to access the chip item text.

Andrew
  • 23
  • 5
  • Does this answer your question? https://stackoverflow.com/questions/6710350/copying-text-to-the-clipboard-using-java – itsrajon Apr 27 '20 at 11:07
  • otherwise, you can try ClipboardManager https://developer.android.com/reference/android/text/ClipboardManager – itsrajon Apr 27 '20 at 11:09
  • No, I mean chip item onclick even. How do I access the chip item Text string? – Andrew Apr 27 '20 at 11:10

1 Answers1

2

this may help you!

public void onClick(View v) {
    ChipGroup chg = findViewById(R.id.chipGroup);
    int chipsCount = chg.getChildCount();

    int i = 0;
    while (i < chipsCount) {
      Chip chip = (Chip) chg.getChildAt(i);
      if (chip.isChecked() ) {
         msg += chip.getText().toString() + " " ;  
      }
      i++;
    }; 
    // show message here
}
itsrajon
  • 283
  • 2
  • 15