Issue
Is there any way I can center text within a textview and then have the drawable aligned to the left hand side, so that the text remains centered but the drawable does not affect this? Currently the whole textview is being centered.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"
android:drawableLeft="@drawable/ic_mirror_face"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="@android:color/white"
android:id="@+id/toolbar_title"
android:layout_gravity="center"/>
</android.support.v7.widget.Toolbar>
You can see the issue in this picture. I want the word to be centred with the word Library.
Solution
I saw an answer posted here which has been deleted since. That solution should work for you.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"
android:drawableLeft="@mipmap/ic_launcher"
android:textAlignment="center"
android:textSize="20sp"
android:id="@+id/toolbar_title"
android:layout_gravity="center"
android:layout_marginRight="10dp"/>
The last line is what's supposed to do the trick. Change the margin value from 10dp
to whatever works best for the image you are using.
Or
You could use an ImageView
anchored to a TextView
to get the same result. But you will have more control in this case as the size of drawable will not affect the alignment of the text.
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="someText"
android:layout_gravity="center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_anchor="@id/toolbar_title"
android:layout_gravity="start|center"
app:layout_anchorGravity="start|center"/>
</android.support.design.widget.CoordinatorLayout>
And this is how the output looks like :
Edit
I now realize that the problem you are facing is probably due to an offset(?) that appears when using a ToolBar
. This answer provides a good solution for that. It goes like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<TextView
android:id="@+id/toolbar_title"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toLeftOf="@id/toolbar_title"/>
</RelativeLayout>
Over here I am not placing the TextView
and ImageView
inside the Toolbar
as it would again offset it slightly to the right. Instead I am placing it over the Toolbar
Answered By - Ajil O.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.