Reputation: 35
I'm new to android studio and this is my first app. It works correctly but having an issue with the emulator, it shows the button in the wrong place.
<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="com.example.dell_pc.myapplication.MainActivity">
<EditText
android:id="@+id/txtyear"
android:layout_width="193dp"
android:layout_height="60dp"
android:ems="10"
android:hint="Enter year of birth"
android:inputType="number"
tools:layout_editor_absoluteY="129dp"
tools:layout_editor_absoluteX="96dp" />
<Button
android:id="@+id/butage"
android:layout_width="115dp"
android:layout_height="41dp"
android:onClick="BuGetAge"
android:text="Show Age"
tools:layout_editor_absoluteY="277dp"
tools:layout_editor_absoluteX="135dp" />
</android.support.constraint.ConstraintLayout>
How can I make it to appear in the right place?
Upvotes: 2
Views: 16323
Reputation: 89234
Click on an element and click the Magic Wand icon in the toolbar above the design view to Infer Constraints so the element will not jump to the (0, 0) position at runtime.
Upvotes: 1
Reputation: 1
Just change:
</android.support.constraint.ConstraintLayout>
to
</RelativeLayout>
Problem solved! Be happy :D
Upvotes: -1
Reputation: 61
Select Component/Item(such as BUTTON) in activity_main.xml -> click on "Infer Constrains(as I show in attached file)"
Your item will take right place in ASE
Do same for the rest Component/Items
Upvotes: 6
Reputation: 2664
tools:layout_editor_absoluteY and other attributes from tools namespace only have effect in layout editor. Your working code does not use these attributes, so your views are in upper left corner, because this is default. As noted above RelativeLayout or even LinearLayout suits better such simple layout.
Upvotes: 0
Reputation: 126
Do you absolutely need to use constraint layout? If not, you can use a relative layout, and use layout_centerHorizontal and layout_centerVertical like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="blah blah"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press"
android:layout_centerHorizontal="true"
android:layout_below="@id/edittext"
/>
</RelativeLayout>
Upvotes: 0