TextView textView = findViewById(R.id.my_textview); int width = textView.getWidth(); Log.i("MyApp", "TextView width: " + width);
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); int height = getHeight(); // draw something using the view's size }This example shows how getWidth can be used in a custom View's onDraw method to get the size of the view and use it for drawing operations. Package Library: The getWidth method is part of the android.view.View class, which is part of the Android framework's core libraries.