5

I have a ListView inside which I dynamicaly fill the list items in the getView() method, with a combination of TextView, and an ImageView.

I need to calculate the size of the Text and Image view inside the getView, so I will be able to set visibility=gone, if View is too big. I am tring:

public View getView(int position, View convertView, ViewGroup parent) {
  ..
  View listItem=view.findViewById(R.id.listItem);
  ImageView imageView=(ImageView)view.findViewById(R.id.myImg);
  TextView textView=(TextView)view.findViewById(R.id.mytxt);
  if (imageView.getRight()>listItemText.getRight()) {
    imageView.setVisibility(View.GONE);   
  }
  if (textView.getRight()>listItemText.getRight()) {
    textView.setVisibility(View.GONE);    
  }
  ..
}

However since I'm inside the getView(), the values of layout are not yet created, so I get false values for the View.getRight() call.

Any ideas how to do this?

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Tomer
  • 859
  • 3
  • 11
  • 19
  • Why don't you decide that according to the length of the String you ar going to put inside the TextView? – Thommy Dec 01 '11 at 08:15
  • because the same text length may have diffrent actual width – Tomer Dec 01 '11 at 08:31
  • How about calculating the entire width and subtract width of listItemtext. If your textview adn image view content is small enough for that particular width. Display it or hide it. Just a curious question cant you ellipsize the text and scale the image down so that its always displayed? – Jayshil Dave Mar 03 '12 at 12:53

1 Answers1

0

If you really need to do such thing, you can register a onLayoutChangeListener and listen for changes in layout parameters, like left, right, top, and down.

textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                // TODO: Take care of visibility here. You can access the parent View by v.getParent() method.
            }
        });

But you are not supposed to do such thing in the getView method. You can make the textView multiline, or set android:ellipsize="marquee" on the textView's layout. More on marquee: Marquee text in Android

mehrmoudi
  • 1,096
  • 1
  • 11
  • 22