12

I'm trying to add some buttons/textviews or any other components to my relativelayout. I can't place them anywhere but top left. However, if I edit XML code it works fine.

Here is the screenshot of current status

I already changed my layout to relativeLayout but still no luck. (Android Studio 3.3)

My layout xml is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    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"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</RelativeLayout>

I would expect to move these buttons/textviews relative to each other but they just stuck at top left.

Onik
  • 19,396
  • 14
  • 68
  • 91
BurakS
  • 133
  • 1
  • 2
  • 6
  • try changing `RelativeLayout` to `LinearLayout`. Else you have to use options like `android:layout_alignParentRight="true"` or in the textview `android:layout_alignRight="@id/button"` – Pierre Jan 25 '19 at 13:42
  • May first you'll read something about [RelativeLayout](https://www.tutorialspoint.com/android/android_relative_layout.htm)? – grabarz121 Jan 25 '19 at 13:52

6 Answers6

25

there is a magnetic button on top of view editor its called (on/off auto connect ). if you change it you can move the views inside relative layout.

19

Just Enable AutoConnection to Parent In Design Mode click the magnet icon and choose "AutoConnection to Parent" enter image description here

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
mohammad
  • 191
  • 1
  • 3
0

This is the basic code of how you can prevent everything from cluttering in the left corner.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    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"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">
 <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textView"
        android:text="Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:text="TextView" />
</RelativeLayout>

To know more about Positioning Views in a Relative Layout, refer:

https://developer.android.com/guide/topics/ui/layout/relative

Akanshi Srivastava
  • 1,160
  • 13
  • 24
0

U did not Render the Relative Layout Rules. You have layout_alignParentRight layout_alignParentLeft , etc.U should Use Them . For Example, IF u Want Your TextView to be below of The Button U should Write Code Like This:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    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"
    tools:context=".MainActivity">
 <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
       android:text="Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:text="TextView" />
</RelativeLayout>

https://developer.android.com/guide/topics/ui/layout/relative

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

If you do not download "SDK Platform Tool" that same as You are using in "API Version For Preview" Tab which is used for preview XML layout, This problem can occur

Go to Android XML Visualizer Tool -> Use Design View -> API version for Preview tab

How to Navigate

After that, Check API Version and Make sure to download the SDK platform tool which is similar to that

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

Just tick all the layout_alignParentXXXX attributes and then you can see the element move in the parent relative layout with the margins.