简体   繁体   中英

How to set a count value (badge) for a textview in android using java (Not for an icon or any button or any other nav items)

How to set a count value (badge) for a textview in android (Not for an icon or any button or any other nav items)

Image https://i.stack.imgur.com/jJcyC.png

I need to set a count value for a TextView in android. Now i have set like with hyphen in same line. But I need as a badge.

My code

 private void setText(@NonNull TextView view, @StringRes int stringRes, int count) {
        Utils.clear(mTextView);
        view.setText(mTextiew
                .append(getResources().getString(stringRes))
                .append(SharedStrings.HYPHEN)
                .append(count);
               
    }

In case you like a native solution, you could look into BadgeDrawable .

Two caveats:

  • Note: This is still under development and may not support the full range of customization Material Android components generally support (eg themed attributes).
  • You can't set the badge text, only int s, but this seems fine for your usecase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM