0

I need to get width of view that is smaller that default window width. I tried getViewTreeObserver, but it is only executed at the end of all draws that is why variable needed is 0;

I tried to call my fragment(which will use the width) in the listener, but there apeared exception:

Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=20986, uid=10161 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

I cant sove this problem. HELP

this also doesnt work:

measure(View.MEASURED_SIZE_MASK, View.MEASURED_SIZE_MASK);
getMeasuredWidth()
getWidth()
  • That Exception has nothing to do with getting a `View`'s width. Also, the first part of your question is unclear. Why doesn't the `getViewTreeObserver()` method work for you? – Mike M. Dec 10 '16 at 10:04
  • @MikeM. because `onCreate(), onResume()` is called on activity. Fragment's `onCreate(), onCreateView(), onResume()` are called between these two methods of the activity. And after these calls `getViewTreeObserver()` will work(return callback). This is not what I need. I need to get measure in `onCreate(), onResume()` of the activity or in the methods of fragment – Чечил Крым Dec 10 '16 at 10:13
  • And exception returns when I initialize fragment in listener (`getViewTreeObserver()`). – Чечил Крым Dec 10 '16 at 10:18
  • You can't get a `View`'s actual measurements until it's laid out, and pretty much the earliest you can do that is with the `OnGlobalLayoutListener`. Change your design to initiate the dependent code when you can get those dimensions in the listener. Beyond that, there's not enough information to even guess at the reason for that Exception. – Mike M. Dec 10 '16 at 10:25
  • @MikeM. i think that the exception happens because I initialize fragment in listener. i.e. parent of the fragment is listener or some similar idea – Чечил Крым Dec 10 '16 at 10:33

0 Answers0