3

I need to find a view by id and then override its onMeasure method. Does anyone know how to do that?

The following will not work in Java, but conceptually it's what I need:

ImageView myImage = (ImageView) findViewById(R.id.some_pic);
myImage.onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int width = MeasureSpec.getSize(widthMeasureSpec);
  int height = MeasureSpec.getSize(heightMeasureSpec);
    showOther(width, height);
};

myImage.setImageBitmap(bmp);

Java offers this

ImageView myImage = new ImageView(this) {
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    showOther(width, height);               }
};

or this

ImageView myImage. = (ImageView) findViewById(R.id.some_pic);
myImage.setImageBitmap(bmp);

I thought of using setOnMeasureListener but no such method is defined for ImageView. Any thoughts on how to get this going?

learner
  • 11,490
  • 26
  • 97
  • 169
  • What's wrong with your first Java option? – Catherine Apr 18 '13 at 08:37
  • Which one? the one with `myImage.onMeasure(int widthMeasureSpec, int heightMeasureSpec){...};`? – learner Apr 18 '13 at 08:41
  • The one where you're overriding the onMeasure method. – Catherine Apr 18 '13 at 08:41
  • @Catherine: the ImageView has already been instantiated, which is why findViewById returns a non-null ImageView. You can add methods to a class before (or while) instantiating an object of that class. You cannot add methods to the class of an object that has already been instantiated. – Gary Sheppard Oct 31 '13 at 11:59
  • Oh, right, of course. Wasn't thinking. Could define his own subclass of ImageView then I guess. – Catherine Oct 31 '13 at 19:15

2 Answers2

1

I'm guessing that the some_pic ImageView was declared in XML and then inflated, probably by calling setContentView in your Activity class. Inflation means that Android reads the XML and translates it into Java objects. Once you call setContentView, all the objects get instantiated, and after that it's too late to make changes to their classes, such as overriding ImageView's onMeasure method.

I can think of two options:

  1. (preferred) Create a new class that extends ImageView and overrides onMeasure. When you declare some_pic in XML, instead of using an ImageView tag, name the tag after your new class (fully qualified name):

    <com.mycompany.myproject.MyImageView
        android:id="@+id/some_pic"
        ...
    />

  2. Don't declare some_pic in XML. In your Activity's onCreate method, after the call to setContentView, instantiate a new ImageView and override its onMeasure method, as you did in your question's first Java option. You'll have to manually insert this ImageView into your layout; use the Google to learn how to insert a view into another view (here's an example).

Community
  • 1
  • 1
Gary Sheppard
  • 4,764
  • 3
  • 25
  • 35
1

I just had the same problem like yours. Maybe you already solved this, but there is no explicit solution here. Just extend the ImageView in a new class for example MyImageView and use that in the layout definition. This way you can use:

MyImageView myImage = (MyImageView) findViewById(R.id.some_pic);

Example class and xml can be found here in the second answer: ImageView be a square with dynamic width?

Community
  • 1
  • 1
Peter Ambruzs
  • 7,763
  • 3
  • 30
  • 36