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?