Search code examples
androidxmlandroid-layoutandroid-linearlayoutandroid-scrollview

Android Linear Layout keeps reordering children whenever I change it's properties


I'm an android beginner, so forgive me if this is a stupid question!

I'm trying to create a basic activity that contains a form - a series of questions (TextViews) each followed by an answer field (EditTexts), with a SUBMIT button at the bottom. The form is a little longer than the screen, so I'm containing them all within a ScrollView, and then within a Linear Layout.

But every time I update the properties of the Linear Layout or the ScrollView (e.g. amend the size of the container, or set center gravity), the items in my linear layout are reordered so that the SUBMIT button is at the top, followed by all of the EditTexts, then all of the TextViews. The XML is also reordered.

It's not stopping my app from running as I can reorder them and then press go (so long as I don't then need to tweak anything) but it's really hard to work with as I've spent ages reordering them - I'm wondering what I'm doing wrong?

Any help appreciated!

xml is below (there is no accompanying java yet, the activity is still blank):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".CreateNewEventActivity">

    <ScrollView
        android:layout_width="409dp"
        android:layout_height="601dp"
        android:layout_marginBottom="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

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

            <EditText
                android:id="@+id/dateEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="date"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/locationEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/themeEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/participantEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/chooseEventIdEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/introTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center|center_horizontal|center_vertical"
                android:text="INTRO \n"
                android:textAlignment="center"
                android:textSize="40sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dateTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/locationTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/themeTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/participantTextView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/participantTextView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/choseEventIdTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

desired order reordered after amending the size of the LinerLayout


Solution

  • This is a known issue with android studio from Android Studio 3.5 Canary 8 and has been fixed in Android studio version 3.5.2 (have a look at issue #129457736 for more details). So all you need to do is update your Android Studio version.

    When editing XML code, the IDE might apply an incorrect code style when you select Code > Reformat Code from the menu bar. To fix this issue, reset the appropriate Android code style as follows.

    In case you want to keep your Android Studio version, do as the following:

    1. Open the Settings window by clicking File > Settings (on macOS, Android Studio > Preferences).
    2. In the left panel, click Editor > Code Style > XML.
    3. Near the top-right corner of the right panel, select Set from > Predefined Style > Android
    4. Click OK (you may need to restart Android Studio)