Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout views in top left corner

Every time I create views like Button and TextView in ConstraintLayout, they all get stuck at the top corner instead of where I placed them.

I tried to create new activities and change the emulator, but the result is still the same.

This is a screenshot of what's happening:

enter image description here

What may be the issue?

like image 970
hassanito hajj Avatar asked Mar 04 '17 08:03

hassanito hajj


People also ask

What is the difference between ConstraintLayout and LinearLayout?

Following are the differences/advantages: Constraint Layout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout).

Is ConstraintLayout a view?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

Which is better RelativeLayout or ConstraintLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.

What is vertical bias in constraint layout?

Bias, in terms of ConstraintLayout , means "if there is extra room, slide the widget in this direction along the axis". The default bias is 0.5, meaning that the widget is centered in the available space.


2 Answers

As stated in Constraint Layout guides:

If a view has no constraints when you run your layout on a device, it is drawn at position [0,0] (the top-left corner).

You must add at least one horizontal and one vertical constraint for the view.

I guess you haven't applied any constraints.

Either manually apply constraints to the view, or let the layout editor do it for you using "Infer constraints" button:

enter image description here

like image 61
azizbekian Avatar answered Sep 23 '22 16:09

azizbekian


When you drop a view into the Layout Editor, it stays where you leave it even if it has no constraints... this is only to make editing easier

In other words, the View will "stay where you leave it" until you add constraints.

The reason it looks different in the Preview vs your app is because anything in your XML with tools: is going to be removed from the code when your app runs. These options are only for the Android Studio layout editor, not representative of what you should expect to see when the code runs.

And it may be troublesome for beginners because

a missing constraint won't cause a compilation error

However

the Layout Editor indicates missing constraints as an error in the toolbar. To view the errors and other warnings, click Show Warnings and Errors (red button with number). To help you avoid missing constraints, the Layout Editor can automatically add constraints for you with the Autoconnect and infer constraints features

More details

like image 30
OneCricketeer Avatar answered Sep 22 '22 16:09

OneCricketeer