Android programming is a shithole (part I)
How do you get the screen dimmensions in Android? You could say that this code would work:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Well, no. Not quite. The returned height is not always the screen height because it includes things like the height Android buttons take and etc.
You have to resort to tricks like creating a View that takes all the screen and reading its height instead.
Simple, isn't it? No it isn't. You can't read a View dimmensions just like that, because the View isn't actually drawn until much later than you do the SetContentView
. You have do to asycronous things like:
final View view=//smth;
...
view.post(new Runnable() {
@Override
public void run() {
view.getHeight(); //height is ready
}
});
Comentarios
Publicar un comentario