...
 
Commits (2)
package net.dankito.utils.android.ui.view
import android.content.Context
import android.graphics.drawable.Drawable
import android.support.v7.widget.ActionBarContextView
import android.support.v7.widget.ActionMenuView
import android.support.v7.widget.SearchView
......@@ -15,7 +16,7 @@ import net.dankito.utils.android.extensions.setRightMargin
import org.slf4j.LoggerFactory
class ToolbarUtil {
open class ToolbarUtil {
companion object {
const val DefaultCountActionItems = 4
......@@ -24,13 +25,13 @@ class ToolbarUtil {
}
fun setupActionItemsLayout(menu: Menu, onClickListener: ((MenuItem) -> Unit)? = null) {
open fun setupActionItemsLayout(menu: Menu, onClickListener: ((MenuItem) -> Unit)? = null) {
for(i in 0..menu.size() - 1) {
setupActionItemsLayout(menu.getItem(i), onClickListener)
}
}
fun setupActionItemsLayout(menuItem: MenuItem?, onClickListener: ((MenuItem) -> Unit)? = null) {
open fun setupActionItemsLayout(menuItem: MenuItem?, onClickListener: ((MenuItem) -> Unit)? = null) {
menuItem?.let {
when(menuItem.actionView) {
is SearchView -> setupSearchView(menuItem.actionView as SearchView, menuItem)
......@@ -39,7 +40,7 @@ class ToolbarUtil {
}
}
private fun setupActionLayoutItem(actionView: RelativeLayout, menuItem: MenuItem, onClickListener: ((MenuItem) -> Unit)?) {
protected open fun setupActionLayoutItem(actionView: RelativeLayout, menuItem: MenuItem, onClickListener: ((MenuItem) -> Unit)?) {
setTitleAndIcon(actionView, menuItem)
actionView.setOnClickListener {
......@@ -47,25 +48,25 @@ class ToolbarUtil {
}
}
fun updateMenuItemView(menuItem: MenuItem?) {
open fun updateMenuItemView(menuItem: MenuItem?) {
menuItem?.actionView?.let { actionView ->
setTitleAndIcon(actionView, menuItem)
}
}
private fun setTitleAndIcon(actionView: View, menuItem: MenuItem) {
protected open fun setTitleAndIcon(actionView: View, menuItem: MenuItem) {
actionView.imgActionIcon.setImageDrawable(menuItem.icon)
actionView.txtActionTitle.text = menuItem.title
}
private fun setupSearchView(searchView: SearchView, menuItem: MenuItem) {
protected open fun setupSearchView(searchView: SearchView, menuItem: MenuItem) {
(searchView.findViewById(android.support.v7.appcompat.R.id.search_button) as? ImageView)?.let { searchIcon ->
setupSearchView(searchView, menuItem, searchIcon)
}
}
private fun setupSearchView(searchView: SearchView, menuItem: MenuItem, searchIcon: ImageView) {
protected open fun setupSearchView(searchView: SearchView, menuItem: MenuItem, searchIcon: ImageView) {
val searchIconIndex = (searchIcon.parent as ViewGroup).indexOfChild(searchIcon)
(searchIcon.parent as ViewGroup).removeView(searchIcon) // remove search icon and place it in an ActionLayout
......@@ -92,20 +93,20 @@ class ToolbarUtil {
// we have to wait some time till actionModeBar is deflated and its children are added
fun adjustToolbarLayoutDelayed(toolbarOrActionModeBar: ViewGroup, countActionItems: Int = DefaultCountActionItems, reserveOverflow: Boolean? = null) {
open fun adjustToolbarLayoutDelayed(toolbarOrActionModeBar: ViewGroup, countActionItems: Int = DefaultCountActionItems, reserveOverflow: Boolean? = null) {
toolbarOrActionModeBar.postDelayed({
adjustToolbarLayout(toolbarOrActionModeBar, countActionItems, reserveOverflow)
}, 100L)
}
fun adjustToolbarLayout(toolbarOrActionModeBar: ViewGroup, countActionItems: Int = DefaultCountActionItems, reserveOverflow: Boolean? = null) {
open fun adjustToolbarLayout(toolbarOrActionModeBar: ViewGroup, countActionItems: Int = DefaultCountActionItems, reserveOverflow: Boolean? = null) {
reduceCloseButtonAndTitleViewSize(toolbarOrActionModeBar)
setMaxActionItemsCount(toolbarOrActionModeBar, countActionItems, reserveOverflow)
}
fun reduceCloseButtonAndTitleViewSize(toolbarOrActionModeBar: ViewGroup) {
open fun reduceCloseButtonAndTitleViewSize(toolbarOrActionModeBar: ViewGroup) {
val density = toolbarOrActionModeBar.context.resources.displayMetrics.density
for(i in 0..toolbarOrActionModeBar.childCount - 1) {
......@@ -126,7 +127,7 @@ class ToolbarUtil {
}
fun setMaxActionItemsCount(actionMenuViewParent: ViewGroup, countActionItems: Int, reserveOverflow: Boolean? = null) {
open fun setMaxActionItemsCount(actionMenuViewParent: ViewGroup, countActionItems: Int, reserveOverflow: Boolean? = null) {
for(i in 0..actionMenuViewParent.childCount - 1) {
val child = actionMenuViewParent.getChildAt(i)
......@@ -138,7 +139,7 @@ class ToolbarUtil {
}
}
private fun setMaxActionItemsCountInActionMenuView(actionMenuView: ActionMenuView, countActionItems: Int, reserveOverflow: Boolean?) {
protected open fun setMaxActionItemsCountInActionMenuView(actionMenuView: ActionMenuView, countActionItems: Int, reserveOverflow: Boolean?) {
val presenterField = actionMenuView.javaClass.getDeclaredField("mPresenter")
presenterField.isAccessible = true
val menuPresenter = presenterField.get(actionMenuView) // class ActionMenuPresenter is not accessible -> have to do everything via reflection
......@@ -167,4 +168,19 @@ class ToolbarUtil {
updateMenuViewMethod.invoke(menuPresenter, true)
}
open fun getToolbarIcon(toolbarOrActionModeBar: ViewGroup?): Drawable? {
toolbarOrActionModeBar?.let {
for(i in 0..toolbarOrActionModeBar.childCount - 1) {
val child = toolbarOrActionModeBar.getChildAt(i)
if(child is ImageView && child is ImageButton == false) {
return child.drawable
}
}
}
return null
}
}
\ No newline at end of file