i want to create a custom view extending ConstraintLayout in which i am having a textview.I want text view to be always in center of ConstraintLayout irrespect of its width .Here is the code
class CustomView @JvmOverloads constructor(context: Context,attributeSet: AttributeSet):ConstraintLayout(context,attributeSet) {
var textView:AppCompatTextView
var constraintView:ConstraintLayout
init {
val height: String =
attributeSet.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height")
val width: String =
attributeSet.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_width")
background=resources.getDrawable(R.drawable.upsell_unselected_text_bg_for_discount_rectangle)
textView= AppCompatTextView(context)
constraintView=this
textView.id=R.id.vtextview
constraintView.id=R.id.vlinecview
textView.setTextColor(resources.getColor(R.color.black))
textView.text="B"
textView.rotation=90F
textView.textSize=60F
textView.setSingleLine()
textView.maxLines=1
textView.maxEms=7
textView.ellipsize= TextUtils.TruncateAt.END
textView.setHorizontallyScrolling(false)
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textView, 30, 180, 1,TypedValue.COMPLEX_UNIT_SP);
constraintView.addView(textView)
var layoutParams=ConstraintLayout.LayoutParams(ConstraintSet.MATCH_CONSTRAINT_SPREAD,ConstraintSet.WRAP_CONTENT)
layoutParams.topToTop=R.id.vlinecview
layoutParams.bottomToBottom=R.id.vlinecview
layoutParams.startToStart=R.id.vlinecview
layoutParams.endToEnd=R.id.vlinecview
textView.layoutParams=layoutParams
textView.bringToFront()
}
fun setTextSize(textsize:Float)
{
textView.textSize=textsize
}
fun setTextColor(tcolor:Int)
{
textView.setTextColor(tcolor)
}
fun setText(str:String)
{
textView.text=str
}
}
But when i use this customView with width 70dp ,the text view does not fit center of CustomView and can anyone please tell me how to size textView according to width of layout