2

I'm trying to create a LinearLayout programmatically but for some reason it is not shown and I have no errors in Logcat or Run terminals.

Here is my Java code:

public class MainActivity extends AppCompatActivity {
    String [] arr = {"1","2","3"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout parent = findViewById(R.id.rootLayout);
        LinearLayout child;

        for(int i = 0; i < arr.length; i++) {
            child = new LinearLayout(this);
            child.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            child.setOrientation(LinearLayout.VERTICAL);
            child.setBackgroundColor(Color.YELLOW);
            parent.addView(child);
        }

    }
} 

and my XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
</LinearLayout>

What I'm trying to achieve is to have 3 LinearLayouts created according to the array length as I will pass some TextViews to them later

I tried to follow the answer I found Creating LinearLayout Programmatically/Dynamically with Multiple Views but still cannot see the LinearLayouts created on the simulator.

Here is how it shows: Simulator Preview

I'm unsure what am I doing wrong?

Thank you for the hints and help.

Bumblebee
  • 85
  • 5
  • 1
    Maybe there is code that you are not showing, but your linear layouts don't have any content so the height is zero. You can test this easily by replacing `LinearLayout.LayoutParams.WRAP_CONTENT` with a specific height, say, "200". – Cheticamp Feb 25 '22 at 17:15
  • True, that was my problem, thank you for the hint. I used `LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)` then ` lp.height = 150` and this fixed my issue – Bumblebee Feb 25 '22 at 17:19
  • 1
    Android Studio's Layout Inspector is a good tool to debug layout problems like this - for future reference. – Cheticamp Feb 25 '22 at 17:27

1 Answers1

0

Used the below and this fixed the issue:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.height = 150;
Bumblebee
  • 85
  • 5
  • The `WRAP_CONTENT` you're passing in to the constructor is in fact the height, so you are creating it with one value and then overriding it straight away with a different one. It's fine if you're just doing this so you can see them now, but you probably want to stick with `WRAP_CONTENT` when you add the subview. You might also want to consider what happens when you have more than 3 items in the array — you may need to wrap `rootLayout` in a `ScrollView` or, if there are likely to be a lot of elements, replace the whole thing with a `ListView` or `RecyclerView`. – chrisbtoo Feb 26 '22 at 01:49