Search code examples
androidandroid-relativelayout

Need help editing relative layout


I'm new to android studio and building a little calculator. Here's a screenshot:

https://i.sstatic.net/Q76Oo.jpg

As you can see, it's aligned with the left border, rather than in the center, which I'd like. All of the buttons are based on the √ button. Is this a bad thing? How do I make them center?

Thanks

Edit - Here's my content_only code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.thegreat.douglas.calculator.OnlyActivity"
    tools:showIn="@layout/activity_only">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sqrt_key"
        android:id="@+id/sqrt_key"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="178dp"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/seven_key"
        android:id="@+id/seven_key"
        android:layout_below="@+id/sqrt_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/four_key"
        android:id="@+id/four_key"
        android:layout_below="@+id/seven_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/one_key"
        android:id="@+id/one_key"
        android:layout_below="@+id/four_key"
        android:layout_alignRight="@+id/four_key"
        android:layout_alignEnd="@+id/four_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/zero_key"
        android:id="@+id/zero_key"
        android:layout_below="@+id/one_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/point_key"
        android:id="@+id/point_key"
        android:layout_below="@+id/one_key"
        android:layout_toRightOf="@+id/one_key"
        android:layout_toEndOf="@+id/one_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/two_key"
        android:id="@+id/two_key"
        android:layout_above="@+id/zero_key"
        android:layout_toRightOf="@+id/one_key"
        android:layout_toEndOf="@+id/one_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/five_key"
        android:id="@+id/five_key"
        android:layout_above="@+id/one_key"
        android:layout_toRightOf="@+id/four_key"
        android:layout_toEndOf="@+id/four_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/eight_key"
        android:id="@+id/eight_key"
        android:layout_above="@+id/four_key"
        android:layout_toRightOf="@+id/seven_key"
        android:layout_toEndOf="@+id/seven_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/left_parenth_key"
        android:id="@+id/left_parenth_key"
        android:layout_above="@+id/seven_key"
        android:layout_toRightOf="@+id/sqrt_key"
        android:layout_toEndOf="@+id/sqrt_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/right_parenth_key"
        android:id="@+id/right_parenth_key"
        android:layout_above="@+id/eight_key"
        android:layout_toRightOf="@+id/eight_key"
        android:layout_toEndOf="@+id/eight_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nine_key"
        android:id="@+id/nine_key"
        android:layout_below="@+id/left_parenth_key"
        android:layout_toRightOf="@+id/eight_key"
        android:layout_toEndOf="@+id/eight_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/six_key"
        android:id="@+id/six_key"
        android:layout_below="@+id/eight_key"
        android:layout_toRightOf="@+id/five_key"
        android:layout_toEndOf="@+id/five_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/three_key"
        android:id="@+id/three_key"
        android:layout_above="@+id/point_key"
        android:layout_toRightOf="@+id/point_key"
        android:layout_toEndOf="@+id/point_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/negative_key"
        android:id="@+id/negative_key"
        android:layout_alignBottom="@+id/point_key"
        android:layout_toRightOf="@+id/point_key"
        android:layout_toEndOf="@+id/point_key"
        android:onClick="specialOperator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/multiply_key"
        android:id="@+id/multiply_key"
        android:layout_above="@+id/nine_key"
        android:layout_toRightOf="@+id/right_parenth_key"
        android:layout_toEndOf="@+id/right_parenth_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/subtract_key"
        android:id="@+id/subtract_key"
        android:layout_above="@+id/six_key"
        android:layout_toRightOf="@+id/nine_key"
        android:layout_toEndOf="@+id/nine_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_key"
        android:id="@+id/add_key"
        android:layout_above="@+id/three_key"
        android:layout_toRightOf="@+id/six_key"
        android:layout_toEndOf="@+id/six_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/enter_key"
        android:id="@+id/enter_key"
        android:layout_toRightOf="@+id/three_key"
        android:layout_toEndOf="@+id/three_key"
        android:layout_alignBottom="@+id/negative_key"
        android:layout_alignTop="@+id/three_key"
        android:onClick="enterKey" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear_key"
        android:id="@+id/clear_key"
        android:onClick="clearEquation"
        android:layout_above="@+id/left_parenth_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:id="@+id/equation"
        android:layout_above="@+id/clear_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/multiply_key"
        android:layout_alignEnd="@+id/multiply_key"
        android:clickable="false"
        android:textAlignment="textEnd"
        android:height="@dimen/abc_action_bar_stacked_max_height" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/square_key"
        android:id="@+id/square_key"
        android:layout_below="@+id/equation"
        android:layout_toRightOf="@+id/sqrt_key"
        android:layout_toEndOf="@+id/sqrt_key"
        android:onClick="specialOperator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/negative_exp_key"
        android:id="@+id/neg_exp_key"
        android:layout_below="@+id/equation"
        android:layout_toRightOf="@+id/left_parenth_key"
        android:layout_toEndOf="@+id/left_parenth_key"
        android:onClick="specialOperator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/divide_key"
        android:id="@+id/divide_key"
        android:layout_below="@+id/equation"
        android:layout_toRightOf="@+id/right_parenth_key"
        android:layout_toEndOf="@+id/right_parenth_key"
        android:onClick="operator" />


</RelativeLayout>

Solution

  • Can you Share the xml-layout design code than we can help you:
    
    **Edited:**
     Use `android:gravity="center"` code in Root node of the xml file.
    
    **In your case:**
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.thegreat.douglas.calculator.OnlyActivity"
        tools:showIn="@layout/activity_only">
    <Button ...
    // Add all button design code here
    
    </RelativeLayout>