Omri Shapira
Omri Shapira

Reputation: 209

Create custom toolbar in android Kotlin

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?

Upvotes: 0

Views: 2706

Answers (2)

Rahul Rokade
Rahul Rokade

Reputation: 162

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")
}}

Upvotes: 0

Rishabh Kumar Singh
Rishabh Kumar Singh

Reputation: 452

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"

Upvotes: 1

Related Questions