0

I'm new in the android programming.

I made a new view in the android studio but the emulator doesn't show it like the preview. I don't have any idea what the reason is.

Preview in Android Studio

Emulator

The ConstraintLayout was default. Is it possible to use another one? Why are all my elements at the top of the view? Is a attribute missing?

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=".activity.DetailDeviceInformation">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:layout_weight="0.11"
    android:orientation="horizontal"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="147dp">

    <TextView
        android:id="@+id/weight_label"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.18"
        android:text="@string/gewicht"
        android:textAlignment="gravity"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/weightDropdown"
        android:layout_width="190dp"
        android:layout_height="match_parent"
        android:layout_weight="0.00" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="39dp"
    android:layout_weight="0.07"
    android:orientation="horizontal"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="204dp">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="4.26"
        android:text="@string/wiederholungen"
        android:textAlignment="gravity"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/repetitionDropdown"
        android:layout_width="190dp"
        android:layout_height="match_parent"
        android:layout_weight="0.07" />

</LinearLayout>

<Button
    android:id="@+id/saveButton"
    android:layout_width="match_parent"
    android:layout_height="62dp"
    android:layout_weight="0.07"
    android:text="@string/button"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="418dp"
    tools:text="Speichern" />

</android.support.constraint.ConstraintLayout>
Larissa
  • 5
  • 4
  • is this your full xml code ? – karthik Aug 09 '18 at 14:36
  • I insert the missing code snippet above. – Larissa Aug 09 '18 at 14:50
  • You are missing constraints on your constraint layout, remove tools:ignore="MissingConstraints". The preview is displaying that layout (the well-formed one) due to layout_editor_absoluteX and layout_editor_absoluteY attributes. This attributes works only during preview but if you don't add constraint you'll occurr into problems at runtime. If you remove tools:ignore="MissingConstraints" the editor will notify you about this issue – Nicola Gallazzi Aug 09 '18 at 14:51
  • @NicolaGallazzi I removed tools:ignore="MissingConstraints" but it looks identically. – Larissa Aug 09 '18 at 14:59
  • 1
    Possible duplicate of [ConstraintLayout views in top left corner](https://stackoverflow.com/questions/42594033/constraintlayout-views-in-top-left-corner) – Ben P. Aug 09 '18 at 15:21
  • @Larissa all that removing `tools:ignore="MissingConstraints"` will do is allow Android Studio to tell you that your views are missing constraints. You still need to add constraints! See the question I linked above for more info. – Ben P. Aug 09 '18 at 15:22

4 Answers4

1

you use Constraint layout as the base layout. But the declaration is missing.

so add below code in the top

<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">

I find you are using textView and spinner and wrap this two in a linear layout. but you don't need to use linear layout. you should remove those two excess linear layouts.

Shudipto Trafder
  • 1,932
  • 1
  • 14
  • 27
0

try using this way realtive layout:

change :

<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=".activity.DetailDeviceInformation">



</android.support.constraint.ConstraintLayout>

to :

<RelativeLayout
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=".activity.DetailDeviceInformation"
>

</RelativeLayout>
karthik
  • 528
  • 4
  • 19
0

Refactor your code as below, to populate spinners follow the documentation at: https://developer.android.com/guide/topics/ui/controls/spinner

<?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="wrap_content">

    <Spinner
        android:id="@+id/weightDropdown"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/repetitionDropdown" />

    <Spinner
        android:id="@+id/repetitionDropdown"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/saveButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/weightDropdown"
        tools:text="Speichern" />

</android.support.constraint.ConstraintLayout>
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
0

Try this one

<?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=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="147dp"
        android:layout_weight="0.11"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/weight_label"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.18"
            android:text="sss"
            android:textAlignment="gravity"
            android:textSize="20sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/weightDropdown"
            android:layout_width="190dp"
            android:layout_height="match_parent"
            android:layout_weight="0.00" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:layout_marginBottom="32dp"
        android:layout_marginTop="2dp"
        android:layout_weight="0.07"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/saveButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="4.26"
            android:text="ssss"
            android:textAlignment="gravity"
            android:textSize="20sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/repetitionDropdown"
            android:layout_width="190dp"
            android:layout_height="match_parent"
            android:layout_weight="0.07" />

    </LinearLayout>

    <Button
        android:id="@+id/saveButton"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="84dp"
        android:layout_weight="0.07"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
        tools:ignore="MissingConstraints"
        tools:text="Speichern" />

</android.support.constraint.ConstraintLayout>
Ishan Fernando
  • 2,758
  • 1
  • 31
  • 38