Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 925
Jake warton Avatar asked Sep 01 '25 16:09

Jake warton


2 Answers

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

like image 86
Ashish Avatar answered Sep 04 '25 18:09

Ashish


Android Studio XML Code Scheme - like in this picture

Change XML Code Scheme for "Default IDE" in Android Studio Preferences.

Hope it works for you.

like image 25
Sebastian Zalewski Avatar answered Sep 04 '25 16:09

Sebastian Zalewski