Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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.
like image 809
ViliusK Avatar asked Jan 31 '17 09:01

ViliusK


People also ask

What is missing constraints in Android Studio?

Constraints are a set of rules which determine how an object positions itself relative to other objects and the wider layout. Without constraints, the objects in your app would likely jump out of position when the app is loaded or the device is rotated.

Why do we use constraints in Android?

Advantages of using ConstraintLayout in AndroidIt helps to improve the UI performance over other layouts. With the help of ConstraintLayout, we can control the group of widgets through a single line of code. With the help of ConstraintLayout, we can easily add animations to the UI components which we used in our app.

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.

What is Android Layout_margin?

android:layout_margin. Specifies extra space on the left, top, right and bottom sides of this view. android:layout_marginBottom.


2 Answers

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

I'm on Android Studio 4.1.1.

like image 50
Vojtěch Strnad Avatar answered Oct 17 '22 21:10

Vojtěch Strnad


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.

like image 1
ViaTech Avatar answered Oct 17 '22 23:10

ViaTech