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 d
<TextView
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:drawableLeft="@drawable/distance"
android:drawablePadding="10dp"
android:padding="10dp"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="20sp" />
no test too much, another way
class CenterDrawableToggleButton @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatToggleButton(context, attrs, defStyleAttr) {
private val centerMatrix = Matrix()
override fun onDraw(canvas: Canvas?) {
canvas?.save()?:return
centerMatrix.reset()
centerMatrix.postTranslate(width / 2F - (getDrawWidth() / 2), 0F)
canvas.concat(centerMatrix)
super.onDraw(canvas)
canvas.restore()
}
private fun getDrawWidth(): Float {
return paint.measureText(text.toString()) + compoundPaddingRight + compoundPaddingLeft
}
}
You can set the parent of the TextView as a RelativeLayout whose width is match_parent.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/edit_location_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/add_location_text_view"
android:gravity="start|center_vertical"
android:layout_centerHorizontal="true"
android:drawableStart="@android:drawable/ic_menu_edit"
android:text="Edit Location" />
</RelativeLayout>
u can set your header like this
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/head" >
<ImageView
android:id="@+id/cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:onClick="onClick"
android:layout_marginTop="10dp"
android:src="@drawable/btn_back_" />
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/TvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#000000"
android:textSize="20sp"/>
</RelativeLayout>
</RelativeLayout>
Just Give Hieght of Textview,image source as per your need
Try following:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textAlignment="center" />
simple as this.. try it..
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|left"
android:drawableLeft="@drawable/icon"
android:text="something something" />