I have this problem of the textview is not centering below the image button. anyway to fix this issue? the coding and image is given below thx!
<LinearLayout android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnContact"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnRoute"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnChecList"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.
android:layout_centerHorizontal="true" android:gravity="center" I think it's correct. so that TextView will be centered in Layout, and text will be centered in TextView.
android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.
Try this snippet :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center_vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With