0

The LinearLayoutOutlined class I'm using is from this answer with only a few small edits for color.

This is what my current activity looks like. There is a horizontal scroll view in the center that I want to fill with items by putting them in the layout_holder. This is the xml layout for the page:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ControlPanel"
        android:orientation="vertical"
        android:id="@+id/main">

    <HorizontalScrollView 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:fillViewport="true">

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

        </LinearLayout>
    </HorizontalScrollView>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom">

        <Button
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/connect_button_title"
                android:id="@+id/button_button"
                android:layout_gravity="start|bottom"
                android:layout_width="0dp"
                android:onClick="connectServer"/>

        <EditText
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:id="@+id/editText"
                android:layout_gravity="end|bottom"
                android:text="@string/default_ip"
                android:layout_width="0dp"/>

    </LinearLayout>
</LinearLayout>

The bellow image is how I want block layouts to be inflated into layout_holder:

enter image description here

And this is model for it

<LinearLayoutOutlined xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/block">

    <ImageView
            android:layout_marginTop="15dp"
            android:layout_height="50dp"
            android:layout_width="50dp"
            android:layout_gravity="top|center"/>

    <LinearLayoutOutlined
            android:orientation="vertical"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal|fill_vertical"/>

</LinearLayoutOutlined>

However when trying to dynamically add the block layout; I get a different result. This is how I'm trying to inflate the layout:

LayoutInflater layoutInfralte = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout_holder);
View block = layoutInfralte.inflate(R.layout.block, null);
linearLayout.addView(block);

This is what it looks like when it gets inflated with a block:

enter image description here

The most nested LinearLayoutOutlined in the block layout is not matching up with the layouts I defined in the model. It's height appears to be zero when it should be the distance from the top of the screen until the top of the "connect" button. Any idea what I'm doing wrong here?


EDIT Took a better screenshot to explain the issue better.

Community
  • 1
  • 1
flakes
  • 21,558
  • 8
  • 41
  • 88
  • LinearLayoutOutlined should be followed by package name, like yourpackagensme. LinearLayoutOutlined. – Sanjeet A Feb 01 '16 at 00:44
  • @SanjeetAjnabee i just have it in a package named `android.widget` so i could be lazy while typing. Its definitely getting imported. – flakes Feb 01 '16 at 00:50
  • Your layout_holder LinearLayout is set to a Horizontal Orientation. – Devsil Feb 01 '16 at 14:19
  • @Devsil Is that wrong? I'm trying to make the `block` items fill the screen width and then scroll if they start expanding out of the screen. So a horizontal LinearLayout inside a HorizontalScrollView – flakes Feb 01 '16 at 14:22
  • @flkes If you inflate your layouts programmatically I think you should define also height programmatically before adding views to the LinearLayout. In particular you should do this once parent LinearLayout has been rendered with height defined (looking at your code it seems you are not doing this). – andrea.petreri Feb 01 '16 at 20:00
  • @thetonrifles shouldn't it look at the parents value to get its `layout_height="match_parent"` value once addView is called? I have no idea how the android api handles that. – flakes Feb 01 '16 at 22:11
  • @flkes what I'm wondering is where you are executing `linearLayout.addView(block);`. You are doing it into onCreate method of your Activity? – andrea.petreri Feb 01 '16 at 22:14
  • @thetonrifles I have tried it in the onCreate and outside of the onCreate method when an event happens. When its created by an event I am calling it through a custom `Runnable` object using the `runOnUiThread` method from the main `Activity` – flakes Feb 01 '16 at 22:19
  • @thetonrifles I have also tried manually typing one into the main layout first, then adding another using an inflator (after the onCreate). The result is one normal sized block next to one small block. – flakes Feb 01 '16 at 22:22
  • 1
    @flkes I'm going to write you an answer. Hope I could help you. – andrea.petreri Feb 01 '16 at 22:23

2 Answers2

1

Try to inflate and add View after height of LinearLayout has been measured. Below I reported implementation for onCreate method of your Activity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_holder);

    final LayoutInflater li = LayoutInflater.from(this);
    final LinearLayout layout = (LinearLayout) findViewById(R.id.layout_holder);

    layout.post(new Runnable() {
        @Override
        public void run() {
            int height = layout.getMeasuredHeight();
            LinearLayoutOutlined view = (LinearLayoutOutlined) li
                .inflate(R.layout.block, null);
            view.setMinimumHeight(height);
            layout.addView(view);
        }
    });
}

This is how it looks on my device:

enter image description here

Hope this could help.

andrea.petreri
  • 4,137
  • 2
  • 22
  • 21
  • Thankyou so much! Still messing around with it a bit, but this solves my main issue. In general do you not use the xml heights/ widths if you instantiate the View in code? All of the internal items in the block also had to be re-sized to work. – flakes Feb 01 '16 at 22:51
  • 1
    @flkes well, problem is that adding the view directly in `onCreate` method would mean trying to include it into a layout that was not rendered yet. Using `post` method on layout allows you to wait until it is rendered before getting its size. Unfortunately some troubles still occur with `match_parent` property. BTW, in your case you can use `layout_height="0dp"`and `layout_weight="1"`in inner `LinearLayoutOutlined ` of `block.xml`. – andrea.petreri Feb 01 '16 at 23:03
  • 1
    @flkes Glad I could help! ... well so I think you can accept the answer :) – andrea.petreri Feb 01 '16 at 23:18
0

You are adding layouts to your layout_holder which is set to orientation=horizontal. So of course every block goes next to the previous one in a horizontal fashion. Setting that layout_holder to "vertical" will put each block under the previous one.

The layout_width of a layout determines how wide the view(group) is, so setting that to "wrap_content" will allow it to be as wide as it needs to be. Just be sure your blocks are set to a minimumWidth which is as wide as you need it to be (or have that content setup so that it wraps content which is as wide as it needs to be).

MacD
  • 783
  • 7
  • 23
  • I need to get a new pic for the last one i think. I only had a picture of trying to add 5 blocks which must be misleadinging. The issue isnt how theyre lined up, its the individual height of each block. Im going to update the image asap with only inflating a single block. – flakes Feb 01 '16 at 21:58