34

So upgraded to Android Studio 3.5 yesterday, and it brought weird problem while auto-formatting XML layouts.

All the views inside a layout change order when Auto Formatted. So, if the views are inside LinearLayout, then they'd change their order and all the similar type of views come together. And I just used LinearLayout as an example, it happens everywhere in XML.

I tried Unexpected auto-formatting of XML layout in Android Studio this, but it didn't work for me. Is it a bug in Android Studio itself? Or did I mess some settings up while upgrading?

Here's the video of that happening https://youtu.be/1emrQFksg30. I pressed Ctrl+Alt+L for auto formatting.

This is especially a problem in LinearLayout as order of elements is important in that. I used ConstraintLayout as I was in kind of hurry to test something in my app.

Important:

See the comment below by @kAliert below if you don't want to jump around to find the answer (because some people marked this question as duplicate).

Edit: This question is about the problem that I faced in my layout, the question whose duplicate this question is supposed to be, is speaking of Android Manifest (which is XML too, but not many people will face that problem, as many people will be refactoring the layouts as compared to the manifest file). I personally wasn't able to find the answer to my question even after searching because that question didn't contain anything related to layouts, and I searched for layouts, which is a lot bigger problem than refactoring of Manifest file.

Vedprakash Wagh
  • 2,552
  • 2
  • 5
  • 23
  • I do have the same problem here but inside of a ConstraintLayout. AutoFormat changes the order of my views (some views are not clickable anymore after that because they are behind some others). It also places the data block of databinding at the end of the file. – Maik Peschutter Aug 23 '19 at 07:35
  • I have the same problem. Happens only on one Layout file. My fellow developer doesn't experience the problem. I noticed a new function under `Code` named `Rearrange Code`. Clicking on it, the layout code is rearranged the same way as I get when clicking the `Reformat Code`. That means that all layout elements are moving around without a specific logic (I couldn't find one anyhow). I think this could be some explanation for the bug. – Ambran Aug 23 '19 at 12:36
  • 30
    Its bit odd that his question is marked as duplicate. The other post has nothing to do with this problem. Anyway. What did the trick: File > Settings > Editor > Code-Style > XML. Click the arrangement tab. In the upper right, choose Set From > Android. – kAliert Aug 25 '19 at 11:45
  • In particular I think the issue is the lack of "attribute" property tag's in the arrangement tab. See my answer in the "duplicate" question for a screenshot: https://stackoverflow.com/a/57668634/6573127 – Slartibartfast Aug 27 '19 at 06:23
  • @Slartibartfast, I think what you're speaking of is the order in which the tags are arranged while refactoring. – Vedprakash Wagh Aug 27 '19 at 07:14
  • I had the same issue and I searched in stakoverflow and this question was the only one that match with my problem, and the answer from @kAliert solve my problem. – snti Aug 27 '19 at 16:31
  • Yes, @Vedprakash Wagh, is that not the problem? From what I can tell the reason kAliert's suggestion of "choose Set From > Android" works is precisely because it inserts the "attribute" type tags. It also resets any other customisations one may have set in the process but it does work. – Slartibartfast Aug 28 '19 at 00:39
  • Ooh! Alright. But the other way of selecting "Android" is way more simpler. @Slartibartfast – Vedprakash Wagh Aug 28 '19 at 05:43
  • This Answer worked for me https://stackoverflow.com/a/57591181/6869491 – Amit Verma Aug 28 '19 at 12:06
  • @kAliert Not odd at all. The symptoms are different but the cause and the solution are the same – Tim Aug 29 '19 at 11:47

0 Answers0