xmlandroid-studio

How to reformat XML layouts in Android Studio


In my project I created some UI in the design tab of an XML layout. But when I go to the Text tab, the code is not properly formatted!

I selected Reformat code from right click menu on XML but the code is still not properly formatted!

<?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=".MainActivity">


    <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/nameTxt" android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" android:hint="Name"/>
</android.support.constraint.ConstraintLayout>

How can I reformat my above code?


Solution

  • For Reformat Code works properly In Android Studio

    Your XML File Code after use Reformat Code

    <EditText
            android:id="@+id/nameTxt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:ems="10"
            android:hint="Name"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    

    It works with CTRL + ALT + L

    Or you can just Click On Code in Menu Bar and Select Code Format as shown in Image

    enter image description here