In my app I have a header bar which consists of a single textview with fill_parent as width, which have a specific background color and some centered text. Now I want to add a drawable on the left side of the header bar, so I set the drawableLeft and sure enough the text and image is displayed. However the problem here is that the text is no longer properly centered, e.g., when the drawable is added the text is shifted a bit to the right as shown in the screenshots here:
Is there anyway I can center the text properly and have the drawable positioned as it is above without using an additional layout item (such as a LinearLayout)?
u can set your header like this
Just Give Hieght of Textview,image source as per your need
The accepted answer is not working for me, it fails if TextView width is match parent I did it using FrameLayout.
You can set the parent of the TextView as a RelativeLayout whose width is match_parent.
I've faced a similar problem. Solved it by using single
TextView
withlayout_width="wrap_content"
andlayout_gravity="center"
parameters:One simple solution is to use a transparent image with the same size on the opposite side of the textview. In my example i copied my actual vector_image and changed the colors to be transparent.
Try following: