Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android navigation icon is not vertically aligned when using custom toolbar

Tags:

android

kotlin

I have used a custom toolbar class so i can align the title to the right and every thing works fine except the navigation back icon is not vertically aligned

enter image description here

and this is the custom toolbar class

class RTLToolbar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : Toolbar(context, attrs, defStyleAttr) {


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        val childCount = childCount
        for (i in 0 until childCount) {
            val view = this.getChildAt(i)
            if (view is TextView) {
                forceTitleCenter(view,l, r)
                break
            }
        }
    }
    private fun forceTitleCenter(view: TextView, l: Int,  r: Int) {
        val top = view.top
        val bottom = view.bottom
        view.layout(l, top, r, bottom)
        navigationIcon?.let{ view.setPadding(it.intrinsicWidth,0,0,0) }
        view.gravity = Gravity.RIGHT
    }
}
like image 448
Zakaria M. Jawas Avatar asked Sep 18 '25 15:09

Zakaria M. Jawas


1 Answers

i found this xml attribute app:buttonGravity="center_vertical" and it did the job, now the back icon is aligned with the title

like image 109
Zakaria M. Jawas Avatar answered Sep 21 '25 06:09

Zakaria M. Jawas