Android: width and height of dynamically created NetworkImageView return ZERO

I want to create a horizontal list of images that the user can add (and delete) dynamically, except for the first (it works like a button to add an image). The user will click on this first to add more photos to his left. As shown below, I am creating a horizontal scroll and linear layout that initially has one NetworkImageView, which is the button for the image adder.

In xml there is:

...
        <HorizontalScrollView
            android:id="@+id/horizontal_scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <com.android.volley.toolbox.NetworkImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/imageViewAdd"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:background="@color/colorAccent"
                    android:contentDescription="@string/description" />


            </LinearLayout>

        </HorizontalScrollView>
...

Then in the code, when the user clicks on this NetworkImageView, I want to add others in front of him one by one.

...
// On create code
layout = (LinearLayout) findViewById(R.id.linear);
        imageViewAdd = (NetworkImageView)findViewById(R.id.imageViewAdd);
        ImageLoader imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
                .getImageLoader();
        imageLoader.get(Config.IMAGE_PATH_URL, ImageLoader.getImageListener(imageViewAdd,
                R.drawable.image, android.R.drawable
                        .ic_menu_camera));
        imageViewAdd.setImageUrl(Config.IMAGE_PATH_URL, imageLoader);
        imageViewAdd.setOnClickListener(this);
        totalImageCount = 0;
...
// Create and add new image view
NetworkImageView newView = new NetworkImageView(this);
        newView.setId(id);
        newView.setPadding(2, 2, 2, 2);
    newView.setScaleType(ImageView.ScaleType.FIT_XY);
    layout.addView(newView, id);

    ImageLoader imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
            .getImageLoader();
    imageLoader.get(Config.IMAGE_PATH_URL, ImageLoader.getImageListener(newView,
            R.drawable.image, android.R.drawable
                    .ic_dialog_alert));
    newView.setImageUrl(Config.IMAGE_PATH_URL, imageLoader);
    newView.requestLayout();
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            (int) getResources().getDimension(R.dimen.imageview_height), // 100 dp each
            (int)getResources().getDimension(R.dimen.imageview_width));
    newView.setLayoutParams(layoutParams);
...

Later in the code, when I really want to set the path to it, I read the width and height as follows:

int targetW = newView.getWidth();
int targetH = newView.getHeight();

0. SO, ( , 3 !).

!

+4
2

globalLayoutListener, :

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
           int width = imageView.getWidth();
           int height = imageView.getHeight();
           //you can add your code here on what you want to do to the height and width you can pass it as parameter or make width and height a global variable
           imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      }
});

, :)

+3

. , , post . , , , . ,

view.post(new Runnable() {
     @Override
     public void run() {
         view.getHeight(); //height is ready
         view.getWidth(); //width is ready
     }
});
+3

Source: https://habr.com/ru/post/1628067/


All Articles