4

I'm referring to this answer.

It worked, giving me count in front of cart in menu of navigation drawer, but how shall I make the counter TextView inside a circle?

What I want is:

enter image description here

What I get from the above link is that it only displays the text 2, not in a circle.

My code:

menu:--

      <item
        android:id="@+id/MyCart"
        android:icon="@drawable/ic_shopping_cart_black_24dp"
        android:title="My Cart"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:actionViewClass="android.widget.TextView"

        />

activity:--

 val  slideshow =
        MenuItemCompat.getActionView(navigationView!!.menu.findItem(R.id.MyCart)) as TextView
    //Gravity property aligns the text

    slideshow.setGravity(Gravity.CENTER_VERTICAL);
    slideshow.setTypeface(null,Typeface.BOLD);                                                                                                                    slideshow.setTextColor(getResources().getColor(R.color.colorAccent));
    //count is added


    RetrofitClient.instancecart.listcart(token).enqueue( object :
        Callback<CartResponse> {
        override fun onFailure(call: Call<CartResponse>, t: Throwable) {
            Toast.makeText(applicationContext,"falied", Toast.LENGTH_LONG).show()
        }

        override fun onResponse(
            call: Call<CartResponse>,
            response: Response<CartResponse>
        ) {

            val res=response
            if (response.isSuccessful) {

                val retro: String = response.body()!!.count.toString()

                slideshow.setText(retro) //retrofit api count

            }
        }

    })
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
IRON MAN
  • 191
  • 3
  • 18

1 Answers1

4

It seems the answer you linked is outdated in some ways, but also has limited styling options for the TextView. So you can do this instead: setting an actionLayout instead of actionViewClass.

screenshot of navigation drawer with circled counter

First, create a circle drawable XML resource (let's call it res/drawable/navdrawer_counterbackground.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ff0000"/>
</shape>

Then create a layout for your counter. Let's call it res/layout/nav_counter.xml. The FrameLayout lets us add gravity around the TextView, so it can be centered vertically in the menu item. Note the TextView has an id of counter. The layout width and height of the TextView control the size of the circle.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/counter"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:background="@drawable/nav_counterbackground"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textStyle="bold"
        tools:text="99+">
    </TextView>
</FrameLayout>

Assign this layout to your menu item:

        <item
            android:id="@+id/nav_itemwithcounter"
            android:icon="@drawable/ic_whatever"
            android:title="@string/menu_whatever"
            app:actionLayout="@layout/nav_counter"/>

Then in your Activity or Fragment, create a property for the TextView:

private lateinit var counterView: TextView

In onCreate()/onViewCreated() you can get the navigation view and use it to get your TextView reference:

val navView: NavigationView = findViewById(R.id.nav_view)
counterView = navView.menu.findItem(R.id.nav_itemwithcounter).actionView.findViewById(R.id.counter)

And you can update the text property and visibility of this counterView whenever you need to.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • i dont have xml for textview ..i have declared app:actionViewClass="android.widget.TextView" – IRON MAN Aug 24 '20 at 13:22
  • see above latest image ..i want a circle not oval – IRON MAN Aug 24 '20 at 13:40
  • its is not giving me the circle no matter how i give dimensions like 10dp,20dp...it is still oval and 2 is displaying in very start of circle not in center – IRON MAN Aug 24 '20 at 13:47
  • your 48 was giving error telling to change to 48F..i did fix...then app crashes Caused by: java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference – IRON MAN Aug 24 '20 at 13:55
  • OK, I figured out a way to do it and actually tested it this time. Answer revised. Sorry about that. I made the circle size 30dp, but I think it would look better with 20dp and then shrink the text size of the TextView's text if the number has more than 1 digit. – Tenfour04 Aug 24 '20 at 14:39
  • can you help https://stackoverflow.com/questions/69311365/how-can-we-customize-an-crop-image-activity-using-ucrop-library – IRON MAN Sep 24 '21 at 07:52