TextView myTextView = findViewById(R.id.myTextView); int measuredWidth = myTextView.getMeasuredWidth(); LinearLayout myLayout = findViewById(R.id.myLayout); LayoutParams layoutParams = (LayoutParams) myLayout.getLayoutParams(); layoutParams.width = measuredWidth; myLayout.setLayoutParams(layoutParams);
TextView myTextView = findViewById(R.id.myTextView); int measuredWidth = myTextView.getMeasuredWidth(); GradientDrawable shape = new GradientDrawable(); shape.setShape(GradientDrawable.RECTANGLE); shape.setCornerRadius(30); shape.setColor(Color.BLUE); shape.setSize(measuredWidth, 100); myTextView.setBackground(shape);This code can be used in the onCreate() method of an activity. Package library: android.widget.