I try in my Android App to create a thin -90 degrees rotated TextView (one horizontal text line for example 30dp width with 400 dp height). Unfortunately instead that the TextView takes the height and width of its parent (LinearLayout) in the LinearLayout there is a very small square (with no text cause it's too small) displayed.
XML
<LinearLayout
android:layout_width="30dp"
android:layout_height="match_parent"
android:background="#B33C3C3C"
android:id="@+id/verticalLayout"
android:orientation="vertical"
android:gravity="center">
</LinearLayout>
Kotlin
var width:Int?=null
var height:Int?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity)
var context: Context = this
val vto: ViewTreeObserver = verticalLayout.getViewTreeObserver()
vto.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
verticalLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this)
} else {
verticalLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this)
}
width = verticalLayout.measuredHeight
height = verticalLayout.measuredWidth
val t = TextView(context)
t.textSize = 15f
t.text = "Testttttttttttttttttttt"
t.rotation = -90f
t.setBackgroundResource(R.color.primary)
t.height = convertPixelsToDp(convertPixelsToDp(height!!, context), context)
t.width = convertPixelsToDp(convertPixelsToDp(width!!, context), context)
Log.d("aaa", "height " + height + " width " + width)
Log.d("aaa", "height " + convertPixelsToDp(height!!, context) + " width " + convertPixelsToDp(width!!, context))
verticalLayout.addView(t)
}
})
}
fun convertPixelsToDp(px: Int, context: Context): Int {
return px / (context.resources
.displayMetrics.densityDpi.toInt() / DisplayMetrics.DENSITY_DEFAULT)
}
The Log.d displays actually the correct numbers:
height 90 width 1415
height 30 width 471.
Do you have any ideas how I could achieve the desired thin rotated Text? Thank you!