Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to access a view’s width and height in BindingAdapter?

Posted on November 11, 2022 By

Solution 1 :

use this

view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() {
            view.removeOnGlobalLayoutListener(this);
            int width = imageView.getWidth();
            int height = imageView.getHeight();
        }
});

this is better than adding delay, it run when you view drawn and remove listener after so no memorylake.

Solution 2 :

Try to use this thing:

@BindingAdapter("imageName")
public void bind(ImageView imageView, String imageName) {
    imageView.post(() -> {
        int width = imageView.getWidth();
        int height = imageView.getHeight();
    });
}

Problem :

I am using Android databinding and a BindingAdapter to bind an ImageView’s bitmap to a bitmap that I load.

@BindingAdapter("imageName")
public void bind(ImageView imageView, String imageName) {
   ...
   int width = imageView.getWidth();
   int height = imageView.getHeight();
   ...
}

Both width and height are zero and I understand from my research and the numerous SO posts that I’m probably calling these methods too early and the layout is not drawn yet.

But I can’t really move the code to another method when I want to use the BindingAdapter. Is there another way to get the measurements of the view or to postpone the binding process?

Comments

Comment posted by Kapta

Just add some delay, and then recheck it

Comment posted by Vincent Mimoun-Prat

Have you tried imageView.post( … )?

Comment posted by flauschtrud

Thank you, that feels way better than a delay. Just as an addition: i had to add this listener on the ViewTreeObserver like so

Comment posted by mahdi shahbazi

@JanaFlauschata Yeah, you are right a just forget it and answer edited now, thanks.

READ  [FIXED] Could not find com.android.tools.build:gradle:2.2.0
Powered by Inline Related Posts
Android Tags:android, android-databinding

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] android studio – Max. text size of TextView Android
[FIXED] java – TabLayout migration to AndroidX Android
[FIXED] xamarin.forms – Is it a bug in Xamarin StackLayout children.Add, Clear function? Android
[FIXED] java – Robolectric.setupActivity() is deprecated in Android unit test Android
[FIXED] node.js – How to send data from datagram socket in Android to Node js Server? Android
[FIXED] android – Return null when try to read data from preferences datastore Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme