I'm new in Android, i just create my first simple app on android studio, but constraints layout doesn't work correctly on real phone or virtual phone. I intent to put a button on the center of layout, and an edit text at top of layout, but when i run, both of it located same position at right-top layout, so i can't type a text and press the button.
I try to use RelativeLayout and it worked, so i guess the problem come from ConstraintLayout. Please help me fix it!
I have already install"ConstraintsLayout For Android 1.0.1 or lower", and "Solver For ConstraintsLayout 1.0.1 or lower". In build.gradle have added compile 'com.android.support.constraint:constraint-layout:1.0.1'
I use Android Studio version 2.3.
And here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="my.android.myfirstapp.MainActivity">
<Button
android:id="@+id/btn_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jump Activity"
tools:layout_editor_absoluteX="128dp"
tools:layout_editor_absoluteY="269dp" />
<EditText
android:id="@+id/edt_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
tools:layout_editor_absoluteX="85dp"
tools:layout_editor_absoluteY="62dp" />
</android.support.constraint.ConstraintLayout>
Your constraints weren't setup correct, If you are unsure click on the button shown in the image below to see what the issue is.. usually all the layout issues are shown there.
Use the below code it fixes the issue for constraint.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/btn_jump"
android:layout_width="128dp"
android:layout_height="48dp"
android:text="Jump Activity"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edt_Name"
android:layout_width="215dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="107dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.502" />
</android.support.constraint.ConstraintLayout>
Hope this help, if it works do mark the answer