Zakaria M. Jawas
Zakaria M. Jawas

Reputation: 477

Android navigation icon is not vertically aligned when using custom toolbar

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

Upvotes: 3

Views: 654

Answers (1)

Zakaria M. Jawas
Zakaria M. Jawas

Reputation: 477

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

Upvotes: 11

Related Questions