0

I've tried this, and this, and any other else. But it always returns 0. Or do I miss something?

This is my code :

@SuppressLint("NewApi")
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_photo_editor, container, false);

        img = (ImageView) rootView.findViewById(R.id.photo_editor_img);
        watermark = (ImageView) rootView.findViewById(R.id.photo_editor_watermark);

        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        if (android.os.Build.VERSION.SDK_INT >= 13){
            Point size = new Point();
            display.getSize(size);
            screenWidth = size.x;
        }
        else screenWidth = display.getWidth();

        ViewTreeObserver vto = watermark.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                watermarkWidth = watermark.getWidth();
                watermarkHeight = watermark.getHeight();
            }
        });

        vto = img.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                scale = (float) (img.getWidth()/screenWidth);
            }
        });

        return rootView;
    }

I've tried to replace those codes inside onResume and onActivityCreated, but still returns 0.

And this is my xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/photo_editor_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" >

        <ImageView android:id="@+id/photo_editor_img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/bg" />

        <ImageView  android:id="@+id/photo_editor_watermark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|left"
            android:layout_margin="5dp"
            android:src="@drawable/watermark"/>

    </FrameLayout>

</RelativeLayout>
Community
  • 1
  • 1
Dark Leonhart
  • 1,494
  • 2
  • 13
  • 21
  • try this : http://stackoverflow.com/a/4680660/2382964 – Tushar Pandey Oct 30 '14 at 13:21
  • if you are using `wrap_content` for imageView and no image has set, then it will return size zero. Try setting any images in xml if you havent. – Sreejith B Naick Oct 30 '14 at 13:28
  • BTW, you are missing `super.onCreateView(inflater, container, savedInstanceState);` Not that it will fix something... – dragi Oct 30 '14 at 13:38
  • @vakman I've set an image into my ImageView, I've added my xml.. – Dark Leonhart Oct 31 '14 at 03:48
  • @helleye I've never used that code line in every onCreateView and never got an error, I don't really know what it is for, though.. – Dark Leonhart Oct 31 '14 at 04:11
  • I checked the code in super class and it does nothing (only returns null). However, personally I think it is a good practice to call the super class every time you override a method, unless there is a specific reason no to do it. One note here: You have posted the xml containing **photo_editor_root_layout**, but you are inflating **fragment_photo_editor**. – dragi Oct 31 '14 at 07:52
  • @helleye hm..ok, thank you for that. I'll add it right away. photo_editor_root_layout is the id, but the xml file name is fragment_photo_editor – Dark Leonhart Oct 31 '14 at 08:09
  • I failed again... But I have a better hint. Can you put a log message inside your onGlobalLayout()? I see that you are not removing the listener, so maybe it is called more than once, and the last time it is called you have value of 0, but maybe previous times it is fine (if you check the links to the second SO question you have provided, there you will see `removeGlobalOnLayoutListener(this)`) – dragi Oct 31 '14 at 08:22
  • one doubt, are you using 9png as images and if it is what are their sizes (size as in resolution h,w)? – Sreejith B Naick Oct 31 '14 at 13:19
  • @helleye I've tried that one also (removing the listener), yet it's still the same.. – Dark Leonhart Nov 03 '14 at 02:13
  • @vakman nope, I didn't use any 9png..It's just an ImageView that I've already set its image resource via xml. – Dark Leonhart Nov 03 '14 at 02:13
  • Is it drawing properly? Or just have problem with reading measurements? – Sreejith B Naick Nov 03 '14 at 11:16
  • @vakman it force closed even before the app displays the layout.. – Dark Leonhart Nov 04 '14 at 12:40

0 Answers0