-2

I have some LinearLayout(let's call it as root). And I want to add to that layout 40 buttons with 8 rows and 5 columns. For each row I have special LinearLayout. Also each button has its own width and height. Below you can see how I generate and add buttons to layout:

    for(int i = 0 ; i<8 ; i++)
    {

        LinearLayout linearLayout = new LinearLayout(this);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        linearLayout.setGravity(Gravity.CENTER);

        for(int j = 0 ; j<5 ; j++) {


            Button button = new Button(this);



            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpToPx(45), dpToPx(45)); //convert dp to px here

            params.setMargins(5, 5, 5, 5);

            button.setLayoutParams(params);

            button.setBackgroundResource(R.drawable.letter_real_background);

            linearLayout.addView(button);

        }

        root.addView(linearLayout); // adding to root 


    }

So what is the problem? Below that root I have some TextView that overlays buttons on some small devices. In my opinion the reason for this is following: below that TextView I have RecyclerView that contains a set of images with width wrap_content and height wrap_content. I mean that images can have any size that was set to it and RecyclerView push up TextView above , which overlays 8x5 buttons. Below is my xml:

  <?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context="com.incubic.second.soz.MainActivity"
    android:background="@color/main_back_color"
    android:layout_margin="5dp">

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

    </LinearLayout>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:height="60dp"
        android:gravity="center"
        android:layout_above="@+id/citiesInMainActivity"
        android:textSize="30sp"/>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/citiesInMainActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        >
    </android.support.v7.widget.RecyclerView>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        />

</RelativeLayout>

So my question is how to solve that overlaying problem? What to do in order to prevent it? Is it recommended to use GridLayout instead of generating new LinearLayout each time?

neo
  • 1,314
  • 2
  • 14
  • 34

1 Answers1

1

The text view is still in your relative view.

i also added a scrollview as bonus, since the width and height is unknown.

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context="com.incubic.second.soz.MainActivity"
    android:background="@color/main_back_color"
    android:layout_margin="5dp">

    <ScrollView
        android:id="@+id/fullscreen_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/linear_wrapper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

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

            </LinearLayout>

            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:height="60dp"
                android:gravity="center"
                android:layout_above="@+id/citiesInMainActivity"
                android:textSize="30sp"/>


            <android.support.v7.widget.RecyclerView
                android:id="@+id/citiesInMainActivity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                >
            </android.support.v7.widget.RecyclerView>

            <ProgressBar
                android:id="@+id/progress_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:visibility="gone"
                />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>
  • Why I need `linear_wrapper`? – neo Aug 14 '17 at 13:28
  • 1
    Things go weird when they try to fall off the screen and "match_parent" is in the view. for more info check: https://stackoverflow.com/questions/17968344/how-to-avoid-overlap-view-in-relative-layout-in-android Does it work? :) – Teun Gelder Aug 14 '17 at 14:00
  • Unfortunately it does not work , cause you use `RelativeLayout`'s properties such as `alignParentBottom` inside `LinearLayout`. – neo Aug 14 '17 at 18:32