Android obtiene la altura y el ancho del diseño en un fragmento
Estoy trabajando en un fragmento y quiero obtener la dimensión de unlayout
contenido en elxml
fragment layout
. Cuando intento el codigo
RelativeLayout myLayout = view.findViewById(R.id.myLayout);
myLayout.getHeight();
devuelve 0. Necesito estas dimensiones para colocar dentro de myLayout otros objetos.
Intento usar:
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
mHeight = myLayout.getHeight();
mWidth= myLayout.getWidth();
System.out.println("width: "+mWidth+" height: "+mHeight);
}
});
pero este código invoca mucho tiempo y no sé exactamente cuándo se ejecuta. Necesito estas dimensiones enpublic void onActivityCreated ()
método. ¿Es posible?