Search code examples
javaandroidandroid-relativelayoutnine-patch

Center horizontally and/or vertically with 9 patch in Android


I can't find a topic talking about my problem which seems to me standard ... And that make me think it is not possible. But I give a try.

I have a RelativeLayout with a 9 patch background (which contain a content area).

Is that possible to center horizontally and/or vertically a TextView inside this RelativeLayout in relation to the content area ?


Solution

  • this layout file will do the task

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/background"
        android:gravity="center" >
    
        <TextView
            android:id="@+id/txtTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test TextView"
            android:textColor="#000000" >
        </TextView>
    
    </RelativeLayout>
    

    the 9-patch image i used is

    enter image description here