Align the controls in the center of the screen

I have controls that I want to focus on the center of the screen, so I used the following code

<?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:gravity="center_vertical|center_horizontal" android:background="#DDDDDD"> <TextView android:id="@+id/tv_un" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt" android:textColor="#444444" android:layout_marginRight="9dip" android:layout_marginTop="20dip" android:layout_marginLeft="10dip" android:text="User Name:"/> <EditText android:id="@+id/et_un" android:layout_width="150dip" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_toRightOf="@id/tv_un" android:layout_alignTop="@id/tv_un"/> <TextView android:id="@+id/tv_pw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt" android:textColor="#444444" android:layout_below="@id/tv_un" android:layout_marginRight="9dip" android:layout_marginTop="15dip" android:layout_marginLeft="10dip" android:text="Password:"/> <EditText android:id="@+id/et_pw" android:layout_width="150dip" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_toRightOf="@id/tv_pw" android:layout_alignTop="@id/tv_pw" android:layout_below="@id/et_un" android:layout_marginLeft="17dip" android:password="true" /> <Button android:id="@+id/btn_login" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_below="@id/et_pw" android:layout_marginTop="15dip" android:layout_marginLeft="110dip" android:text="Login" /> <TextView android:id="@+id/tv_error" android:layout_width="fill_parent" android:layout_height="40dip" android:textSize="7pt" android:layout_below="@id/btn_login" android:layout_marginRight="9dip" android:layout_marginTop="15dip" android:layout_marginLeft="15dip" android:textColor="#AA0000" android:text=""/> </RelativeLayout> 

but the result was the next image

enter image description here

any idea how to center them regardless of screen orientation

Best wishes

+4
source share
5 answers

edited your code

here

 <?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:layout_centerHorizontal="true" android:background="#DDDDDD" android:gravity="center_vertical|center_horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_un" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Name:" android:textColor="#444444" android:textSize="10pt" /> <EditText android:id="@+id/et_un" android:layout_width="150dip" android:layout_height="wrap_content" android:layout_alignTop="@id/tv_un" android:layout_toRightOf="@id/tv_un" android:background="@android:drawable/editbox_background" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_pw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_un" android:text="Password:" android:textColor="#444444" android:textSize="10pt" /> <EditText android:id="@+id/et_pw" android:layout_width="150dip" android:layout_height="wrap_content" android:layout_alignTop="@id/tv_pw" android:layout_below="@id/et_un" android:layout_marginLeft="17dip" android:layout_toRightOf="@id/tv_pw" android:background="@android:drawable/editbox_background" android:password="true" /> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_below="@id/et_pw" android:layout_marginLeft="110dip" android:layout_marginTop="15dip" android:text="Login" /> <TextView android:id="@+id/tv_error" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_below="@id/btn_login" android:layout_marginLeft="15dip" android:layout_marginRight="9dip" android:layout_marginTop="15dip" android:text="" android:textColor="#AA0000" android:textSize="7pt" /> </LinearLayout> 

+4
source

Try android:layout_centerInParent = "true" This may solve your problem. You can apply this attribute to any element that you want to display in the center of the screen.

+7
source

Set the parameters RelativeLayout : android:layout_height="wrap_content" and android:layout_width="wrap_content"

Give up everything you like about XML.

+2
source

Use android: layout_centerHorizontal = "true" in the layout and layout files-land folder xml. Hope this helps.

Ps Please do not do anything in the xml file if you want the layout to fit all screen sizes

+1
source

You don't need a RelativeLayout . You can also use two LinearLayouts , as shown below:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#DDDDDD" android:gravity="center" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- here your textViews --> </LinearLayout> </LinearLayout> </LinearLayout> 

Important is android:layout_gravity="center" in the nested LinearLayout .

+1
source

Source: https://habr.com/ru/post/1413676/


All Articles