Bonjour,

N'hésitez pas à me signaler si la question a déjà été traitée (après une petite recherche, je n'ai pas trouvé).

Ma question est la suivante : comment obtenir les dimensions d'une vue lorsqu'on est encore au stade des méthodes onCreate, onStart, ou onResume. Lorsque je fais tourner le code suivant, j'obtiens systématiquement 0 / 0 dans le logcat pour les dimensions de l'imageView :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
 
public class MainActivity extends Activity {
 
    public static final String APP_TAG="Mon_tag";
    public ImageView image;
 
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
 
        image = (ImageView) findViewById(R.id.main_image);
 
        Log.i(APP_TAG, "onCreate(): fin ! / dimensions de l'imageView: "
            + image.getWidth() + " / " + image.getHeight());
    }
 
    @Override
    public void onStart(){
        Log.i(APP_TAG, "onStart(): début ! / dimensions de l'imageView: "
                + image.getWidth() + " / " + image.getHeight());
        super.onStart();
        Log.i(APP_TAG, "onStart(): fin ! / dimensions de l'imageView: "
                + image.getWidth() + " / " + image.getHeight());
    }
 
    @Override
    public void onResume(){
        Log.i(APP_TAG, "onResume(): début ! / dimensions de l'imageView: "
                + image.getWidth() + " / " + image.getHeight());
        super.onResume();
        Log.i(APP_TAG, "onResume(): fin ! / dimensions de l'imageView: "
                + image.getWidth() + " / " + image.getHeight());
    }
}
Le XML définissant l'ImageView est le suivant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <ImageView
        android:id="@+id/main_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
</LinearLayout>
D'avance merci pour vos réponses !