NimaAzhd
NimaAzhd

Reputation: 192

Why Doesn't gravity or text alignment of vertical textview work?

Recently I've had to create a vertical textview. after searching I found out inherited class from textView that other people used. it is worked good. but I realize some problems. this class does not work with any gravity or alignment attributes. I tried to set gravity or layout direction to RTL but it does not work either. I use Persian language and this problem gets too bad. the thing I need set my texts in center of vertical textview. p.s: I have to use "android: gravity =" bottom " to rotating text 90 degree according to the condition in class.

so the vertical class :

class VerticalTextView(context: Context, attrs: AttributeSet) : AppCompatTextView(context, attrs) {
    private val topDown = gravity.let { g ->
        !(Gravity.isVertical(g) && g.and(Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM)
    }
    private val metrics = BoringLayout.Metrics()
    private var padLeft = 0
    private var padTop = 0

    private var layout1: Layout? = null

    override fun setText(text: CharSequence, type: BufferType) {
        super.setText(text, type)
        layout1 = null
    }

    private fun makeLayout(): Layout {
        if (layout1 == null) {
            metrics.width = height
            paint.color = currentTextColor
            paint.drawableState = drawableState
            layout1 = BoringLayout.make(text, paint, metrics.width, Layout.Alignment.ALIGN_NORMAL, 2f, 0f, metrics, false, TruncateAt.END, height - compoundPaddingLeft - compoundPaddingRight)
            padLeft = compoundPaddingLeft
            padTop = extendedPaddingTop
        }
        return layout1!!
    }

    override fun onDraw(c: Canvas) {
        //      c.drawColor(0xffffff80); // TEST
        if (layout == null)
            return
        c.withSave {
            if (topDown) {
                val fm = paint.fontMetrics
                translate(textSize - (fm.bottom - fm.descent),0f)
                gravity= Gravity.CENTER //never works
                textAlignment= TEXT_ALIGNMENT_CENTER //never works
                rotate(90f)
            } else
            {
                translate(textSize, height.toFloat())
                rotate(-90f)
            }
//            translate(padLeft.toFloat(), padTop.toFloat())
            makeLayout().draw(this)
        }
    }
}

my xml layout:

<androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/linear"
            android:layout_width="30dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:textDirection="rtl"

            android:textColor="@color/black"
            android:elevation="2dp"
            android:background="@color/primaryColor"
            app:layout_constraintLeft_toLeftOf="@id/IVPlace"
            app:layout_constraintTop_toTopOf="@id/IVPlace"
            android:orientation="vertical"
            android:weightSum="3"
            android:layoutDirection="rtl"
            android:textAlignment="center"
            >

            <nima.mythird.simpleloginregister.utils.VerticalTextView
                android:id="@+id/TVNameViewPLace"
                android:layout_weight="2"
                android:textSize="14sp"
                android:layout_width="30dp"
                android:layout_height="0dp"
                android:gravity="bottom"
                android:text="متن تست"

                android:textColor="@color/black"
                android:elevation="2dp"
/>

            <nima.mythird.simpleloginregister.utils.VerticalTextView
                android:id="@+id/TVِDistance"
                android:layout_weight="1"
                android:textSize="14sp"
                android:layout_width="30dp"
                android:layout_height="0dp"
                android:text="  123 "
                android:textDirection="rtl"
                android:gravity="bottom"
                android:textColor="@color/black"
                android:elevation="2dp"
                />



        </androidx.appcompat.widget.LinearLayoutCompat>

Current result:

enter image description here

Upvotes: 0

Views: 525

Answers (2)

Cheticamp
Cheticamp

Reputation: 62841

Your custom TextView takes over the layout and the drawing of the text. It is in the standard layout and drawing of the view that the text alignment properties to adjust the view are made, specifically in onDraw(). Here is one place in TextView.java where gravity is used.

protected void onDraw(Canvas canvas) {  

...

    if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
        voffsetText = getVerticalOffset(false);
        voffsetCursor = getVerticalOffset(true);
    }
    canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText);

...

Since your code doesn't call through to the onDraw() super, gravity has no effect. You probably can't call through to the super, anyway, since it will do things you don't wish to do.

You will have to handle text alignment yourself in the custom view. I suggest that you take a look at TextView.java to see how you can do that.

Upvotes: 1

androidLearner
androidLearner

Reputation: 1702

Try below line for all your textview.

android:textAlignment="viewStart"

Upvotes: 0

Related Questions