Reputation: 9123
Here's my xml using a LinearLayout. Both the Toolbar and the Button are at the top of the screen. However I want the Button centrally horizontal and centrally vertical. Also the Button is floated to the left and not in the center.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorVeryDark"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="text"
android:textSize="22dp"
android:layout_gravity="center_vertical"
android:background="@color/colorPrimaryDark"
/>
</LinearLayout>
How would I do this?
Upvotes: 0
Views: 6754
Reputation: 1473
Add these two lines in you Button
android:gravity="center"
android:layout_gravity="center_horizontal"
refer this image for proper understanding of use of gravity
Upvotes: 0
Reputation: 23881
Try this: use a RelativeLayout
and use android:layout_centerInParent="true"
for the Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorPrimaryDark"
android:text="text"
android:textSize="22dp" />
</RelativeLayout>
</LinearLayout>
Output:
Upvotes: 3
Reputation: 174
Just use this line of code in your button xml design-
android:gravity="center"
For more ideas you can check this link- Center a button in a Linear layout
Upvotes: 0
Reputation: 41
Use a RelativeLayout then you add the code:
android:gravity="center"
android:layout_gravity="center_horizontal"
in your Button XML.
In alternative, you can use the "design" tool of AndroidStudio for layouts: you can add a button and then drag wherever you want in the preview.
However, there are tons of post like this: next time try to search better :D
Upvotes: 0
Reputation: 2856
try to use gravity
<Button
android:id="@+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:text="text"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textSize="50sp" />
Upvotes: 0
Reputation: 4323
Unfortunately LinearLayouts are not the best for this. FrameLayout, RelativeLayout or ConstraintLayout will give you the flexibility to do what you want.
Upvotes: 0