1

Here is my layout file that I inflated the image view from.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.Tamaritz.dailyselfie.MainActivity" >
<ImageView
    android:id="@+id/selfieImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
<TextView
    android:id="@+id/timestamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/selfieImage"
    />

And here is my code inside my base adapter that will inflate this layout file into view objects with view holder class(inner static class)

    static class ViewHolder {
            ImageView selfieImage;
            TextView timestamp;
            public ViewHolder(View selfieLayout) {
                       selfieImage = (ImageView) selfieLayout.findViewById(R.id.selfieImage);
                      timestamp = (TextView) selfieLayout.findViewById(R.id.timestamp);
             }
    }
   ....
    public View getView(int position, View convertView, ViewGroup parent) {
            View selfieImage = convertView;
           ViewHolder holder = null;
         if(selfieImage == null) {
              LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
               selfieImage = inflater.inflate(R.layout.activity_main, parent, false);
                holder = new ViewHolder(selfieImage);
               int height = holder.selfieImage.getHeight();
               int width = holder.selfieImage.getWidth();
                selfieImage.setTag(holder);
          } 
   }

What I am basically trying to do is to take advantage of Android View holder's pattern. If convertView is null, it means it has not been previously inflated(cannot reuse), so we should take the steps to inflate it. I pass the inflated view into the constructor for the view to be able to find its image view and text view. My question is after the the image view has been inflated and attached to a view holder, why does it have a height and width of 0?(Know this because I stepped through my code). It's important that these values are actual values because I need to use them in the setPic method of http://developer.android.com/training/camera/photobasics.html.

I looked at other possible threads for my issue. The person https://stackoverflow.com/questions/20434257/gridview-setadapter-imageview-width-and-height-0 had a similar issue but never got an answer. This Why does my FrameLayout have 0 height? couldn't have been my issue because the UI should have already been drawn(didn't call this in onCreate) And it couldn't have been this Get ImageView width and height because I didn't call for height and width inside the constructor of the ImageView And not this one Inflated ImageView to put in GalleryView isn't the right size because I made sure to specify a parent.

Does anyone know why height and width are returning 0? I could hardcode height and width in my xml file but I believe that be bad style.

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126

1 Answers1

1

Well I'm now having a computer to test for you. This is what I have tried: my getView method's internal part comes like this, and It can successfully log the width and height of the imageview. This only try to make what you want to achieve: get width and height of imageview right inside the getView method. I have no idea if this is a good practice or not. So use it by your own risk. I would like to suggest you define the size inside your xml, though. It depends on your requirement.

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View selfieImage = convertView;
            final ViewHolder holder;
            if (selfieImage == null) {
                LayoutInflater inflater = (LayoutInflater) parent
                        .getContext().getSystemService(
                                Context.LAYOUT_INFLATER_SERVICE);
                selfieImage = inflater.inflate(R.layout.widget_sample_item,
                        parent, false);
                holder = new ViewHolder(selfieImage);

                selfieImage.setTag(holder);

            } else {
                holder = (ViewHolder) selfieImage.getTag();
            }

            holder.timestamp.setText("this is: " + position + "th child");

            selfieImage.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);

            int height = holder.selfieImage.getMeasuredHeight();
            int width = holder.selfieImage.getMeasuredWidth();
            Log.d("image-info", width + " | " + height);

            return selfieImage;
        }
Nguyễn Hoài Nam
  • 1,130
  • 1
  • 9
  • 20