- Forum posts: 6
Sep 23, 2017, 1:26:52 PM via Website
Sep 23, 2017 1:26:52 PM via Website
How do you force controls to be centered inside a view?
Controls inside a vertical layout inside a horizontal layout wont do it
Setting android:layout_centerHorizontal="true" on either layout won't do it.
Setting 'android:layout_centerParent="true" on either layout.
Setting 'match_parent' on either layout won't do it.
If I try and replace '365dp' in android:layout_width="395dp" with 'match_parent' then irritatingly android studio just overrides what I type and replaces it with 395dp again.
I just don't understand any of this.
What do I have to do to get android studio to centre my controls both vertically and horizontally?
<?xml version="1.0" encoding="utf-8"?>
xmlns:app=""
xmlns:tools=""
android:id="@+id/id_view_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.greg.irrigationcontroller.MainActivity">
<LinearLayout
android:layout_width="395dp"
android:layout_height="587dp"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<TextView
android:id="@+id/id_text_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:text="What would you like to do? "
android:textAppearance="@android:style/TextAppearance.Material.Medium"
android:textColor="@color/colorAccent"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/id_button_settings"
android:layout_width="300dp"
android:layout_height="40dp"
android:onClick="SendMessage"
android:text="Change watering settings"
app:layout_constraintHorizontal_bias="0.786"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="374dp" />
<Button
android:id="@+id/id_button_show"
android:layout_width="300dp"
android:layout_height="40dp"
android:text="Show station settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.297"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.479" />
<Button
android:id="@+id/id_button_search"
android:layout_width="300dp"
android:layout_height="40dp"
android:text="Search for active stations"
app:layout_constraintHorizontal_bias="0.912"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="326dp" />
</LinearLayout>
</LinearLayout>
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.