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.
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.
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