2

I ask this question because I have searched everywhere and I can't find anything to solve the problem. As the title says, I can't find anything in Kotlin, everything is in Java and I'm new to Kotlin. I need to do something, as in the image below. I have seen the following links but they do not particularly work for me:

Actionbar notification count icon (badge) like Google has

How to get text on an ActionBar Icon?

This is the code that I have and what is not working for me is the item just from the counter, which I should call a TabListActivity (which shows me a list), but it does not.

OnOptionsItemSelected does not recognize me when I click on the item, I know because I use Debug.

enter image description here

This is in my main activity called TabActivity.kt

 override fun onCreateOptionsMenu(menu: Menu?): Boolean {
             menuInflater.inflate(R.menu.tab_menu, menu)

         countButton = menu?.findItem(R.id.tab_list_button)
         countButton?.setActionView(R.layout.tab_button)
         var notif = countButton?.actionView as RelativeLayout
         var tv: TextView = notif.findViewById(R.id.tabCount)
         tv.text = TabInfo.count().toString()
         countButton?.title = tv.text

         return super.onCreateOptionsMenu(menu) }

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.forward_button -> {
                TabInfo.currentWebView().goForward()
                refreshForwardButton()
            }
            R.id.reload_button -> {
                reloadCurrentTab()
                return true
            }
            R.id.tab_list_button -> {
                val intent = Intent(this, TabListActivity::class.java)
                startActivity(intent)
                return true
            }
        }

        return super.onOptionsItemSelected(item)
    }

My menu is called tab_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/forward_button"
          app:showAsAction="collapseActionView"
          android:title="@string/go_forward"
          android:icon="@drawable/ic_arrow_forward_black_24dp" />

    <item android:id="@+id/reload_button"
          app:showAsAction="collapseActionView"
          android:title="@string/reload_page"
          android:icon="@drawable/ic_refresh_black_24dp" />

    <item android:id="@+id/tab_list_button"
          app:showAsAction="always"
          android:title="@string/number_tabs"
          android:actionLayout="@layout/tab_button"
        />
</menu>

My layout is tab_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="32sp"
    android:layout_height="32sp"
    xmlns:tools="http://schemas.android.com/tools">

    <ImageView
        android:id="@+id/img_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:src="@drawable/ic_check_box_outline_blank_black_24dp"/>


    <TextView
        android:id="@+id/tabCount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textAlignment="center"
        android:textColor="?attr/textcolor"
        android:textSize="12sp"
        android:textStyle="bold"
        tools:text="1" />
</RelativeLayout>

I had it this way as I wrote it down here and it works for me, but it only shows me the counter and not the icon plus the counter.

enter image description here

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.tab_menu, menu)

        countButton = menu?.findItem(R.id.tab_list_button)
        countButton?.title = TabInfo.count().toString()  
        countButton?.actionView?.
            setBackgroundResource(R.drawable.ic_check_box_outline_blank_black_24dp)

            return super.onCreateOptionsMenu(menu)
    }

My English is not very good, excuse me. Thanks

  • Why don't you use a drawable shape as background to tab button with only textview and not with imageview/relative layout ? – Naveen Avidi Dec 05 '19 at 09:41
  • @NaveenAvidi - Let's see if I understood, what you propose is to add to the menu item with the `android:id="@+id /tab_list_button"`, the attribute `android:icon="@drawable/ic_check_box_outline_blank_black_24dp"`, in this case, I already tried that way but I didn't show the counter on the icon. Or I misunderstood what you have proposed. – Luis Coronel Dec 05 '19 at 10:39
  • Yes with xml drawable file ? – Naveen Avidi Dec 05 '19 at 10:41
  • @NaveenAvidi - That's right, I've tried both ways: 1) the attribute in the menu item of the xml drawable and only shows the icon without the counter. 2) In code as described in the last part of the posting and only the counter is seen and not the icon. – Luis Coronel Dec 05 '19 at 10:48
  • I added an answer , try it ! – Naveen Avidi Dec 05 '19 at 10:59
  • Hey bro ! I tested it, but same result. To make it use an alternative way. Make horizontal linear layout then use it as action bar and disable system action bar – Naveen Avidi Dec 06 '19 at 04:25
  • Sorry for the delay in responding, what about the action bar, how would it be? I have talked to other people and they have told me that I could implement an ImageButton with its own implementation, but I am not sure. – Luis Coronel Dec 08 '19 at 06:20
  • Use this layout structure in xml : RootLayout(Lines Layout Vertical Orientation) -- Linear Layout (Horizontal orientation) - for action bar ---- Your Content layout/view – Naveen Avidi Dec 08 '19 at 06:41
  • @NaveenAvidi - I have already solved it, as soon as I have time, I publish the solution, thanks for all your answers. – Luis Coronel Dec 19 '19 at 09:11
  • @NaveenAvidi - Not if doing so by editing the post above or adding the solution below as an answer, so that others can also help you. – Luis Coronel Dec 19 '19 at 09:15

0 Answers0