Search code examples
javaandroidxmlandroid-layoutandroid-linearlayout

Child LinearLayouts not displaying properly


I have one LinearLayout - horizontal, and it has 3 vertical LinearLayouts in it.

I add TextViews to those LinearLayouts programmaticaly. This is my xml:

<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:id="@+id/hlkurva"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PadyFragment">


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:id="@+id/cesky">


        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/pomlcka"
            android:layout_weight="0.2"
            android:orientation="vertical"
            >

        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:id="@+id/latinsky"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">
        </LinearLayout>
</LinearLayout>

This is how it looks like on Android Studio preview. [This is how it should look like, on preview it looks correct]

However, when I run the app I get this: [The third Linear Layour is diplaying down]

When I rotate screen, it persist. I tried different layout weights, I tried it different way with no layout weight, I tried everything I can but I never get 3 Linear Layouts displayed horizontal to each other how it looks like on the preview. I am really desperate, please help.


Solution

  • This looks to me like you've passed the wrong view id to a findViewById() call in your activity. Maybe something like this:

    LinearLayout left = findViewById(R.id.cesky);
    LinearLayout middle = findViewById(R.id.latinsky); // wrong id here
    LinearLayout right = findViewById(R.id.latinsky);
    

    Then, later when you add all the - characters to the middle column, you're actually adding them to the right column because you've accidentally assigned the wrong view to middle.