-1

I have a slight problem getting my layout_weight to work. I am creating a custom bottomBar. But i cant make it to stretch as i want it to.

Bottom Bar View

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottom_bar_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/bottom_bar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="4"
        android:orientation="horizontal"/>
</RelativeLayout>

This is the big container i am adding my buttons (items) to.

Bottom Bar Item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/bottom_bar_item_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/arrow_up_float"/>
    <TextView
        android:id="@+id/bottom_bar_item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT"/>
</LinearLayout>

This is my items that i add dynamically to the view. But somehow the view is not stretched properly.

But when i hardcode it in. It works. So could it be that layout weight does not work dynamically?

How i add the views (items)

private void updateItems(final List<BottomBarTab> bottomBarTabs) {
    if (bottomBarTabs.size() < TABS_COUNT) {
        throw new RuntimeException("Not enough buttons for bottomBar");
    }

    for (int i = 0; i < TABS_COUNT; i++) {
        bottomBarTabs.get(i).setOnClickListener(this);

        bottomBarTabs.get(i).prepareLayout();
        container.addView(bottomBarTabs.get(i));
    }
}

Screenshot

enter image description here

Jemil Riahi
  • 1,360
  • 5
  • 18
  • 38
  • show the complete layout pls. best thing would be with a screenshot – Willi Mentzel Nov 25 '16 at 15:21
  • Done. Added the full xml view. + whats up with the downvote. It is a legit problem. Because i did research on my own and noticed that dynamically i where not able to make it work. But hardcoding the view with its children worked – Jemil Riahi Nov 25 '16 at 15:29
  • @Jemil Riahi can you post the code of `container` view and `prepareLayout()` method? – Altaf Shaikh Nov 25 '16 at 15:51
  • while using weight_sum to your parent , you have to use match_parent to your parent layout, and your child view also should be in match_parent.with layout_weight to your child layout – Noorul Nov 25 '16 at 15:55

2 Answers2

0

LayoutWeight is given to inner components of layout only not on parent Linearlayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:weightSum="2">
        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/bottom_bar_item_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
 android:layout_weight="1"
            android:src="@android:drawable/arrow_up_float"/>
        <TextView
            android:id="@+id/bottom_bar_item_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
 android:layout_weight="1"
            android:text="TEXT"/>
    </LinearLayout>
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
0
public void prepareLayout() {
    View view = inflate(getContext(), R.layout.bottom_bar_item,this);

    LinearLayout.LayoutParams params =
            new LayoutParams(0,LayoutParams.MATCH_PARENT,LAYOUT_WEIGHT);

    view.setLayoutParams(params);


    if(backgroundColor != null) {
        view.setBackgroundColor(backgroundColor);
    }

    TextView titleText = (TextView) view.findViewById(R.id.bottom_bar_item_text);
    titleText.setText(title);

    AppCompatImageView imageView = (AppCompatImageView) view.findViewById(R.id.bottom_bar_item_icon);
    imageView.setImageResource(iconResId);
}

I changed in my prepareLayout function and put new layoutParams. Because somehow after inflation. The view ignores the weight that was set to it. So i had to force it by code. Maybe this is a android bug?

Jemil Riahi
  • 1,360
  • 5
  • 18
  • 38