TextView tv = findViewById(R.id.my_textview); int height = tv.getHeight(); Log.d("TextViewHeight", "Height of the TextView is: " + height);
TextView tv = new TextView(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); tv.setLayoutParams(layoutParams); tv.setText("Hello, World!"); int height = tv.getHeight();This code creates a new TextView programmatically, sets its layout parameters to match_parent for both width and height, sets the text of the TextView, and then retrieves its height in pixels using the getHeight() method. The getHeight() method, as well as the TextView class, is part of the android.widget package library.