16

I'm using android.support.constraint.ConstraintLayout to layout a simple card's content views.

I'm getting random changes in my XML, every time, when:

  • I switch to Android Studio's layout editor's Design tab
  • Or when I have a Preview pane turned on (in this case changes happens even more often during my changes).

Those random changes include:

  • Removal of my manually defined layout margins (eg. layout_marginEnd)
  • Changing values of my manually entered margin value
  • Adding tools:layout_editor_absoluteX values

Questions:

  • Anyone else experience this Android Studio's feature?
  • How to switch this "feature" off, so my layouts would not be destroyed?
  • Where to report this bug to Google or JetBrains?

Please take a look to this file diff, which was made automatically when switched to Design tab: Automatically made changes' diff in layout XML file

PS.: My Android Studio version is 2.2.3; I'm running it on macOS Sierra.

Update #1

As suggested, after making layout_height="match_parent", I get modifications less often, but still my XML gets messed-up, when I edit layout in Layout Design'er: Automatic changes #2 This time I wanted to set layout_marginTop through Design UI (selected line is what I wanted to add), but I also got those other unwanted changes:

  • layout_marginStart removed;
  • app:layout_constraintBottom_toBottomOf referenced IDs got a prefix of "+" sign, which means, those IDs are newly declared, so when I'll use Goto declaration functionality, I'll have to choose it from the list - not a desired behaviour.
  • tools:layout_editor_absoluteX gets added. I could ignore it, but still - not what I'd want to happen.
Community
  • 1
  • 1
ViliusK
  • 11,345
  • 4
  • 67
  • 71
  • 1
    Just FYI -- ConstraintLayout tends to behave itself better when it's set to match_parent for both width and height. If you need it to wrap_content, then I suggest using tools:layout_height="match_parent" – JoeyJubb Jan 31 '17 at 10:00
  • Are you having this issue with other XML files? try a new screen and see if it does the same thing. Try creating a new project and see if that fixes your problem. If the problem is still there it might be a bug of AS – Tudor Jan 31 '17 at 10:20
  • @PopTudor, I have same issues in all XML files. Going to try @JoeyJubb's suggestion, as I'm using `wrap_content` for my `ConstraintLayout`. – ViliusK Jan 31 '17 at 10:27
  • 2.2.3 has a quite a few bugs with the layout editor -- if you can, try Android Studio 2.3. – Nicolas Roard Feb 01 '17 at 17:53
  • I had same problems as you. I survived without open designer. Now with Android 2.3 is even worst, Constraint is not even rendered in the designer window. – ARLabs Mar 09 '17 at 16:19
  • Same problem here! any advance on this? – venimania Apr 06 '17 at 11:21
  • it's AS issue, more info here https://issuetracker.google.com/issues/37119059 – Wackaloon Sep 18 '17 at 16:44

4 Answers4

3

In my case removing the layout constraint and adding it again fixed the disappearing margins.

I'm on Android Studio 4.1.1.

Vojtěch Strnad
  • 2,420
  • 2
  • 10
  • 15
1

I have been noticing the same issue with Android Studio 2.3, the code changed automatically every time I went from text view to design view on a recent project with ConstraintLayout. - I'm pretty sure it happens with RelativeLayout or whatever other layout you want to use as well.

I guess it is some type of auto code generation bug linked to the design view, I have not been able to find a way to turn it off yet.

I'm not sure this helps because I'm confident you've already realized it, but if you work almost completely in text view, the issue never happens.

I occasionally use design view to begin my layout but once I have something working, staying in text view personally seems to be faster for the development process.

ViaTech
  • 2,143
  • 1
  • 16
  • 51
  • I work exclusively in text view and it still happens (AS 2.3.3) – Maalevolent Sep 13 '17 at 15:45
  • That does make much sense @poetichustla. Text View is straight XML so there is no automated code generation using it. Using text view would be the same as opening and using any text editor for your XML. However, if you accidentally switch from text to design view, I have seen the automated code changes occur. – ViaTech Sep 14 '17 at 18:04
0

As @JoeyJubb mentioned, using layout_height="match_parent" solves this problem. At least XML is not modified. Although, sometimes preview is generated incorrectly.

ViliusK
  • 11,345
  • 4
  • 67
  • 71
  • 3
    Update #1: After making `layout_height="match_parent"`, I still get my XML messed-up, when I edit layout in Layout Design'er. See Update in main question. – ViliusK Jan 31 '17 at 11:11
0

In my case upgrade to newest gradle dependecy helped:

com.android.support.constraint:constraint-layout:1.0.2

Also I started using 0dp instead of match_parent for layout_width

dees91
  • 1,211
  • 13
  • 21