Problems with Linearlayout

Asked

Viewed 89 times

0

I’m getting started in learning about android development.Taking a basic student course. When I got to class on Linearlayout’s things went sideways. The idea is to insert 1 Linearlayout(General) that supports 2 other internal Linearlayout’s.But when inserting components as textview, it is not displayed in the emulation.It is hidden type.

Follow some photos to further unmount the problem:

How it’s supposed to be: Foto 1 Developing: Foto 2 Foto 3 Note: how to observe when inserting the text view components into Layout3 (Viewed layout3). The components are no longer displayed.

Emulation: Foto 4

XML code:

<?xml version="1.0" encoding="utf-8"?>
<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">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/iconetopo" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="12dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    android:textSize="35sp"
    app:layout_constraintStart_toEndOf="@+id/imageView"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/layoutGeral"
    android:layout_width="395dp"
    android:layout_height="649dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="16dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView">

    <LinearLayout
        android:id="@+id/layoutDados"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <EditText
            android:id="@+id/editText4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <EditText
            android:id="@+id/editText5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutResusltado"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

1 answer

1


The problem of displaying components is divided into the sizes placed on them.

  1. You have put a fixed size on the layerIt is ideal to use layout_width="match_parent" to take the total space horizontally and layout_height="wrap_content" to pick up the size needed to meet the display of the items inside it.

  2. In layoutDados and layoutRespected you are using layout_height="match_parent" match_parent will pick up the total screen size, this way the layoutDados plays the layoutRespected out of the screen view, switch in the two layouts to layout_height="wrap_content" and each layout will only take up the space needed to display your items.

  3. In the layoutGeral you put app:layout_constraintBottom_toBottomOf="parent" this makes the layoutGeral is aligned at the end of the page, I removed so that the layoutGeral comes right after the image and the text at the top, in case you want to space more you can use the marginTop.

Corrected example:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/iconetopo" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        android:textSize="35sp"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/layoutGeral"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:orientation="vertical"

        app:layout_constraintTop_toBottomOf="@+id/imageView">

        <LinearLayout
            android:id="@+id/layoutDados"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <EditText
                android:id="@+id/editText5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutResusltado"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView" />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
  • The xml of the question was not properly indented, so it did not appear all. It has as main layout a Constraintlayout.

  • I think the Constraintlayout should be the main one (as the ramaral quoted). Professor Guanabara did so:Image View and text view loose at the top (fixed by the constrainslayout arrows) and then he created the linearLayouts below. "However according to the XML code you passed in the question, your layout is with 3 loose objects an Imageview, a Textview and a Linearlayout." What would be the Linearlayout you said is loose? Is it Layoutgeneral? The app now shows the components.But it’s all on top of each other.

  • Constraintlayout was not appearing in the question, I will edit the answer.

  • It is. I’m sorry. Only now I realized I was missing an excerpt of the code.Thank you for the correction.

  • @Ronan fixed the response, and updated the example xml, tested it here and it worked.

  • 1

    @Alissonmarqui, thank you. It helped a lot.I spent a long time beating my head to be able to solve.Now, the app is in compliance and I can continue learning.Thank you very much!!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.