ViewGroup myLayout = findViewById(R.id.my_layout); int width = myLayout.getWidth(); System.out.println("Width of myLayout: " + width);
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = getWidth(); int height = getHeight(); System.out.println("Width: " + width + ", Height: " + height); }This example shows how to use getWidth() in a custom ViewGroup class. In this case, we override the onMeasure() method, which is called when the system needs to know the size of the view. We call getWidth() and getHeight() to get the current dimensions of the view, and then print them out to the console. The android.view.ViewGroup class is part of the Android framework, which is located in the android.view package.