ConstraintLayout views in top left corner
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:
Constraint layout always alinging to top left corner
I suggest you learn more about constraints, you are not using it quiet right
app:layout_constraintStart_toEndOf="parent"
You see this line from your xml code, it will just put your view out of screen, instead it should be app:layout_constraintStart_toStartOf="parent"
ConstraintLayout views at top left when added programatically
In order to constrain the newly added Views
you should be cloning the ConstraintSet
of the parent layout instead of the View
that is being added. After defining constraints, this ConstraintSet
should then be applied to the parent layout. Also make sure that new View
is added to the layout before cloning the ConstraintSet
.
In your case the added View
is also a ConstraintLayout
which is why you are not getting any errors when cloning the set.
These lines:
constraintSet.clone(levelItem);
constraintSet.applyTo(levelItem);
should be changed to:
constraintSet.clone(mapLayout);
constraintSet.applyTo(mapLayout);
All widgets in Constraint Layout getting stuck to upper left corner in Android Studio
TRY THE FOLLOWING CODE SNIPPET
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="49dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:text="Student Details"
android:textColor="@color/colorPrimaryDark"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="37dp"
android:layout_marginLeft="37dp"
android:layout_marginTop="57dp"
android:layout_marginEnd="27dp"
android:layout_marginRight="27dp"
android:text="Name"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView9" />
</androidx.constraintlayout.widget.ConstraintLayout>
All items merges to top left in constraint Layout , Android Studio
Here's what you want to achieve using ConstraintLayout
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/email_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMAIL"
android:textSize="22sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.2" />
<EditText
android:id="@+id/email_input"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="16dp"
android:background="@android:color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email_label" />
<TextView
android:id="@+id/pwd_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="PWD"
android:textSize="22sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email_input" />
<EditText
android:id="@+id/pwd_input"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="16dp"
android:background="@android:color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pwd_label" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN"
android:textSize="16sp"
android:layout_margin="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pwd_input" />
</android.support.constraint.ConstraintLayout>
In the email label's widget, You can also remove the "layout_constraintVertical_bias" and "layout_constraintBottom_toBottomOf" attributes and fix a top margin.
This can be done using Chains too.
Make sure you are using ConstraintLayout 1.0.2.
You can read this great tutorial about ConstraintLayout
Building interfaces with ConstraintLayout
Hope this helps!
How to position a view in Android constraint layout outside the screen at the beginning
Okay so to have a negative margin you can use translateX, translateY or TranslationZ.
in xml like so:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:translationX="-60dp"
android:translationY="-90dp"
android:translationZ="-420dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
or programmatically like so:
View view = ...;
view.setTranslationX(-60);
view.setTranslationY(-90);
view.setTranslationZ(-420);
Then in order to slowly bring it in from right to left you can use the animate() method like so:
View view = ...;
view.animate().setDuration(1000).translationX(-600).start();
Layout view is in top left corner in emulator
It seems that you've set your RelativeLayout
to a layout_width
of 364dp and a layout_height
of 494dp. It looks fine on your preview because you are using a Nexus 4 (look above your preview) as your preview but you are testing with a emulated Nexus 5. Due to the fact that you've coded the resolution to be 364dp
x494dp
(which is fine for a Nexus 4) your view will look different on different devices as they have difference screens. You should try changing your layout_height
and layout_width
to fill_parent
Android ConstraintLayout - Put one view on top of another view
Set an elevation on the ProgressBar
; 2dp
seems to work.
android:elevation="2dp"
You could also try setting translationZ
as suggested in the accepted answer to a similar question.
I also came across this answer as an alternative.
Related Topics
Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'
Getting Exception "Illegalstateexception: Can Not Perform This Action After Onsaveinstancestate"
Drawing a Line/Path on Google Maps
How to Use Prepared Statements in Sqlite in Android
Onsaveinstancestate () and Onrestoreinstancestate ()
How to Save Users Score in Firebase and Retrieve It in Real-Time in Android Studio
Picasso V/S Imageloader V/S Fresco VS Glide VS Coil
How to Build a Horizontal Listview With Recyclerview
Scrollview Vertical and Horizontal in Android
How to Detect When Wifi Connection Has Been Established in Android
Android Broadcastreceiver on Startup - Keep Running When Activity Is in Background
How to Listen For a Webview Finishing Loading a Url
Application Skeleton to Support Multiple Screens
Adding External Library in Android Studio