I was following along some Android tutorial on Youtube when I encountered the following problem:
(left the designer, right the emulator)
I tried searching on google, but can't seem to find the right link.
Does anyone have any idea what is going on? I tried deleting and placing new elements, but the same problem keeps occurring.
[Edit]
XML code which I didn't change. This was created with a new project.
All elements in the picture were dragged and dropped. No code was changed.
<?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"
android:visibility="visible"
tools:context="com.example.todor.myapplication.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="159dp"
tools:layout_editor_absoluteY="235dp"
tools:text="#string/loginbutton" />
<TextView
android:id="#+id/textView2"
android:layout_width="132dp"
android:layout_height="31dp"
android:layout_marginBottom="42dp"
android:fontFamily="monospace"
android:textAlignment="center"
android:textColor="#color/colorAccent"
android:textColorHint="#android:color/holo_blue_bright"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/editText"
tools:text="#string/logintext"
tools:layout_editor_absoluteX="126dp" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="33dp"
android:width="300dp"
android:ems="10"
android:hint="#string/emailhint"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toTopOf="#+id/editText2"
tools:layout_editor_absoluteX="42dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:width="300dp"
android:ems="10"
android:hint="#string/passowrdhint"
android:inputType="textPassword"
app:layout_constraintBottom_toTopOf="#+id/button"
tools:layout_editor_absoluteX="42dp" />
</android.support.constraint.ConstraintLay
out>
Here are some tips for you !
Read about Tools Attributes Reference
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
You need to understand the difference of below attributes!
tools:text="loginbutton"
android:text="loginbutton"
Your views are not constrained they have only design time position!
Go to the design, drag the corners until the circle turns green and then give the constraint.Here is an extra guidance from linkOne LinkTwo related with ConstraintLayout
If you mess it up big-time you can replace android.support.constraint.ConstraintLayout with LinearLayout but that's not the solution it's another way instead!
Related
I am trying out a simple layout exercise but am struggling to get even a simple layout to work.
In the designer it is as follows:
But when run, the output on an emulator or real phone is:
The Xml markup is as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hceng.myapplication.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail:"
tools:layout_editor_absoluteY="52dp"
tools:layout_editor_absoluteX="18dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="96dp" />
<EditText
android:id="#+id/editText"
android:layout_width="266dp"
android:layout_height="36dp"
android:ems="10"
android:inputType="textEmailAddress"
tools:layout_editor_absoluteX="66dp"
tools:layout_editor_absoluteY="41dp" />
</android.support.constraint.ConstraintLayout>
I am new to this markup language, but I thought it seems simple enough with absolute positioning throughout. In any case, the layout seen in the designer is what I want. Why is the above not working?
According to the documentation
When you drop a view into the Layout Editor, it stays where you leave it even if it has no constraints. However, this is only to make editing easier; 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).
This is the reason why you see layout correctly in editor but not on the emulator.
You can try this, if not bound to ConstraintLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hceng.myapplication.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/email_tv"
android:text="E-mail:" />
<EditText
android:id="#+id/editText"
android:layout_width="266dp"
android:layout_height="36dp"
android:ems="10"
android:layout_toRightOf="#+id/email_tv"
android:inputType="textEmailAddress" />
</RelativeLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:gravity="center" />
</LinearLayout>
Constraint layout has some limitations but I am not sure if colour is one of them.
First
You could try changing the constraint layout to maybe coordinator layout or relative layout.
If that fails
Maybe, the target SDK version in your Android Manifest is too high, if the target version is higher than phone version, it reverts to defaults. So you could try doing placing a lower target version.
I am new to Android Studio, when I created my app all my elements overlapped on each other. I am not sure how to edit this code. Please give me a detailed description.
Layout editor and emulator result
Code for the layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="schemas.android.com/apk/res/android";
xmlns:app="schemas.android.com/apk/res-auto";
xmlns:tools="schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.first.anew.MainActivity">
<TextView
android:layout_width="216dp"
android:layout_height="60dp"
android:text="Hello World!"
tools:layout_editor_absoluteY="16dp"
tools:layout_editor_absoluteX="61dp" />
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="45dp"
android:text="Login"
tools:layout_editor_absoluteY="343dp"
tools:layout_editor_absoluteX="177dp" />
<TextView
android:id="#+id/textView"
android:layout_width="215dp"
android:layout_height="65dp"
android:text="username"
tools:layout_editor_absoluteY="117dp"
tools:layout_editor_absoluteX="61dp" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/guideline"
app:layout_constraintGuide_begin="20dp"
android:orientation="horizontal" />
<EditText
android:id="#+id/editText"
android:layout_width="217dp"
android:layout_height="57dp"
android:ems="10"
android:inputType="textPassword"
tools:layout_editor_absoluteY="218dp"
tools:layout_editor_absoluteX="61dp" />
</android.support.constraint.ConstraintLayout>
Your emulator screen and the layout editor are different because of tools namespace. In a few words, it is a tool that helps you to better visualise the written code. This tool also generates constraints like tools:layout_editor_absoluteY="343dp", which inform the layout editor on how it should draw the views. The problem is that this constraints will not be present in your app code, resulting in that weird behaviour when all views are overlapped (since the above mentioned constraints are not used in the app code we can deduct that there are no constraint at all).
Since you are a beginner I would recommend to start with a LinearLayout or RelativeLayout because I think that ConstraintLayout is harder to understand, use and debug.
put the whole code. if you are using Relativelayout, this will happen. change it to Linearlayout at the top i mean the bottom most element, and put orientation as vertical
see this - for me my soltion works...
add this in xml to the button etc
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.57"
then play with it in the design.
Good luck! :)
you can click the infer Constraints buttonclick on this
and run your programme again..it will solve your problem
So Android Studio has the drag/drop feature in the design tab. I am able to place the elements where I want them, and the layout looks great on the screen.
However, when I go to run the app emulator, it looks completely different. Everything gets shoved up to the top-left hand corner of the screen, nowhere near where I placed it in design mode.
Is there a way to have your elements show the same way in the emulator, as the way you place them in design mode? Right now I'm having to go back and edit all the code each time.
I have my code below, as well as a image link below to further clarify my point.Comparison of Design mode vs App Emulator
<?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="com.example.luke.currencyconverter.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter The Amount in Dollars"
tools:layout_editor_absoluteX="-159dp"
tools:layout_editor_absoluteY="126dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="403dp"
android:layout_height="0dp"
android:text="Enter The Amount in Dollars:"
android:textColor="#android:color/black"
android:textSize="22sp"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="365dp" />
<ImageView
android:id="#+id/currency"
android:layout_width="361dp"
android:layout_height="450dp"
app:srcCompat="#drawable/currency"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="-117dp" />
<Button
android:id="#+id/convert"
android:layout_width="361dp"
android:layout_height="wrap_content"
android:onClick="convert"
android:text="Convert"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="462dp" />
<EditText
android:id="#+id/dollarField"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="408dp" />
</android.support.constraint.ConstraintLayout>
Drag and drop for android will only place the object within the screen. To keep it there you should modify the xml so that the objects will be constrained to each other.
You can use either relative layout or constraint layout for an easier time in designing your layout.
I figured it out. I was using "constraint layout", and I needed to use "relative layout".
I just had this error as well and as annoying as it sounds, my solution was to delete my xml file and recreate it.
My app was first developed for Android 2.3, but has been updated many times and currently targets V21. So I was surprised when my Nexus 5 received the Marshmallow update and found that a critical feature of my app was broken. I have a screen with a line at the top where I want two buttons at the far right and an EditText box (for search text) filling up the remainder of the screen to the left. This was my first app and I was unclear about various layout techniques, but from Android 2.3 to 5.1 this code has continued to work:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/upper_detail_section"
android:elevation="#dimen/upper_elevation"
>
<Button android:id="#+id/sort"
style="#style/large_cust_btn"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/sort"
/>
<Button android:id="#+id/clear"
style="#style/large_cust_btn"
android:layout_toLeftOf="#+id/sort"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:text="#string/clear"
/>
<EditText android:id="#+id/lookup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/clear"
android:layout_alignBaseline="#+id/clear"
android:singleLine="true"
android:hint="#string/item_search_hint"
android:scrollHorizontally="true"
android:textSize="16sp"
android:inputType="textFilter"
/>
</RelativeLayout>
Bringing this screen up in Android 6.0, the buttons are there but the EditText field completely disappears! Knowing what I know now it turned out to be a simple fix using a LinearLayout and weighting:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/upper_detail_section"
android:orientation="horizontal"
android:elevation="#dimen/upper_elevation"
>
<EditText android:id="#+id/lookup"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="#string/item_search_hint"
android:scrollHorizontally="true"
android:textSize="16sp"
android:inputType="textFilter"
/>
<Button android:id="#+id/clear"
style="#style/large_cust_btn"
android:text="#string/clear"
android:onClick="clearClicked"
/>
<Button android:id="#+id/sort"
style="#style/large_cust_btn"
android:text="#string/sort"
android:onClick="sortClicked"
/>
</LinearLayout>
So all is well now and I'm updating the store with a new APK, but I thought I'd warn people that some layout tricks that used to work may now be broken.
If I'm understanding and reproducing your problem correctly, it's quite simple to fix your issues with RelativeLayout as well. Marshmallow only handles a width of match_parent differently. So just set it to something else like with the LinearLayout and alignParentLeft to true:
<EditText android:id="#+id/lookup"
android:layout_alignParentLeft="true"
android:layout_width="0dp"
[...] />
This yielded the same layout for me like in pre Marshmallow.
I am using Eclipse. I can't Drag or Drop anything into graphical interface of my xml file.
When ever I drag anything like a button or a text view it just shows that position dotted block and just stuck at there.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginTop="88dp"
android:text="text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginTop="120dp"
android:text="text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginTop="160dp"
android:text="Type your secret password"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
I just copied and pasted your code into a RelativeLayout file I just made on Eclipse, and it seems to be working fine. Have you tried maybe playing around with the height and width of the TextViews or adding background images to the buttons to try and locate them easier? It is possible the wrap_content may be making it too small. And if this is your first time using Android layouts, I would recommend using LinearLayout instead. RelativeLayout can be tough and hard to work with, even for more experienced programmers.