ViliusK
ViliusK

Reputation: 11555

Why Android Studio is removing and/or changing layout_margin* constraints?

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:

Those random changes include:

Questions:

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:

Upvotes: 16

Views: 3843

Answers (4)

Vojtěch Strnad
Vojtěch Strnad

Reputation: 3185

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

I'm on Android Studio 4.1.1.

Upvotes: 3

dees91
dees91

Reputation: 1411

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

Upvotes: 0

ViaTech
ViaTech

Reputation: 2813

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.

Upvotes: 1

ViliusK
ViliusK

Reputation: 11555

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

Upvotes: 0

Related Questions