0

I'm trying to create a custom toolbar with image buttons, each image button is a menu item.

Picture for demonstration: enter image description here

How I connect each Image Button to be a menu item?

2 Answers 2

1

You can do it like this:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/homeToolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="Sangeet"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <ImageButton
        android:id="@+id/fabRefresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/refresh_option"
        android:backgroundTint="@android:color/transparent"
        android:layout_gravity="center|right"
        />

</androidx.appcompat.widget.Toolbar>

Output

Two attributes are main:

android:backgroundTint="@android:color/transparent"
android:layout_gravity="center|right"
Sign up to request clarification or add additional context in comments.

Comments

0

Custom toolbar XML code

<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/rl_toolbar"
android:background="@color/colorPrimary"
android:layout_height="?attr/actionBarSize">
<RelativeLayout
    android:id="@+id/rl_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_4sdp"
    android:layout_centerVertical="true"
    android:padding="10dp">
    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_back"
        android:visibility="gone"/>
    <ImageView
        android:id="@+id/img_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu"
        android:visibility="gone"/>
</RelativeLayout>
<TextView
    android:id="@+id/txt_title"
    android:layout_toEndOf="@id/rl_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="Title"
    android:textAllCaps="true"
    android:textStyle="bold"
    android:textSize="18sp"
    android:textColor="@color/white"/></RelativeLayout>

custom toolbar used in any activity - xml side

<include layout="@layout/custom_toolbar"/>

Base activity kotlin code

abstract class BaseActivity : AppCompatActivity() {
private var imgBack: ImageView? = null
private  var imgMenu: ImageView? = null
private var txtTitle: TextView? = null
private var rlToolbar: RelativeLayout? = null
open fun findItem(back: Boolean, menu: Boolean) {
    imgBack = findViewById<ImageView>(R.id.img_back)
    if (back) {
        imgBack!!.visibility = VISIBLE
    }
    imgMenu = findViewById<ImageView>(R.id.img_menu)
    if (menu) {
        imgMenu!!.visibility = VISIBLE
    }
    txtTitle = findViewById<TextView>(R.id.txt_title)
    rlToolbar = findViewById<RelativeLayout>(R.id.rl_toolbar)
}
open fun setTitleWithBack(title: String?) {
    findItem(true, false)
    imgBack!!.setOnClickListener { finish() }
    txtTitle!!.text = title
}
open fun setTitleWithMenu(title: String?) {
    findItem(false, true)
    imgMenu!!.setOnClickListener {
        Toast.makeText(this@BaseActivity, "Progress bar working", Toast.LENGTH_SHORT).show()
    }
    txtTitle!!.text = title}}

Method call in any activity

class HomeActivity : BaseActivity() {
lateinit var bindingHome: ActivityHomeBinding
lateinit var context: Context
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    bindingHome = ActivityHomeBinding.inflate(layoutInflater)
    setContentView(bindingHome.root)
    init()
}
private fun init(){
    context = this
    setTitleWithMenu("Home")
}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.