0

I have a ListView. Each item should contain a bar chart which I create out of several TextView-Objects. Here is one for a single bar.

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" >

        <TextView
            android:id="@+id/viewMon"
            android:layout_width="25dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:background="#53933f" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="@string/chart_mon"
            android:textAlignment="center" />
    </RelativeLayout>

In the getView-Method of my Adapter, I try to calculate the height of this element and finally set it via

TextView dayView = (TextView) convertView.findViewById(R.id.viewMon);
dayView.setHeight(height);

The problem is that this seems to have no effect. The values should be correct and if I set the values as text I see them apperaing in the UI.

For me it seems that the system ignores layout parameters (getHeight also returns 0) and uses the values form the xml-file at another place. How can I avoid this?

KCD
  • 678
  • 5
  • 20

2 Answers2

0

Finally I found an answer here.

Instead of simply setting heigth via setHeight, I use this:

LayoutParams params = dayView.getLayoutParams();
if (params == null) { 
   params = new LayoutParams(LayoutParams.MATCH_PARENT, heights[x]); 
   } else {
   params.height = heights[x];
        }
   dayView.setLayoutParams(params);
Community
  • 1
  • 1
KCD
  • 678
  • 5
  • 20
0

Try this code snippet-

   yourviewforgetheight.ViewTreeObserver vto = text_about_content.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {

    yourviewforgetheight.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                float height =yourviewforgetheight.getHeight();
    TextView dayView = (TextView) convertView.findViewById(R.id.viewMon);
    dayView.setHeight(height);

                    }
                });

Hope this code helps you!!! if it is not working please let me know i will try to help you more.

shweta_jain
  • 453
  • 1
  • 5
  • 19