Android ticks: display text vertically

Source: Internet
Author: User

Textview of Android is a text label to display text. but it can show text only horizontally by default, left to right or right to left. there are some chances that we wowould like to show text vertically, top to bottom or bottom
To top, for layout of modern smart phone in particle. in landscape layout, the height is not enough to show all information, we have to arrange them horizontally because width is enough to hold them. but for some label, like Date and Time Label, it wowould
Be nicer to display them in only one line, which is impossible to do in landscape. The text is one line though, it grows horizontally to push following layouts out of screen, just like this:

But the ideal layout wocould be like this: one line label shows itself vertical so in horizontal, it takes up only its height and leaves much free space to put other views:

Fortunately, this is totally possible in Android only with a little extra efforts. with graphics library we can draw text in any way we want: rotate in some direction, following a path and so forth. an example textalign in apidemos
Just shows us the way to draw text following a customizedPath. We can inherite textview and override its ondraw (), then do what ever we like in ondraw. in here, of course, to draw the text in a path that vertically up from bottom. here is
The code:

/* * for the attributes of TextView, some works some not. * 1. setTextsize works * 2. setBackgroundColor works * 3. setTextColor also works * You can adjust the size of TextView by margins and the drawing area by paddings(only paddingTop and paddingBottom works). * For other attributes, like lines or maxLines, or ellipsize are not supported currently. To support them, you should get  * the attributes value before drawText and apply them. */public class VerticalTextView extends TextView {    private static final String TAG = "VerticalTextView";    public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public VerticalTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public VerticalTextView(Context context) {        super(context);    }        @Override    protected void onDraw(Canvas canvas) {        final ColorStateList csl = getTextColors();        final int color = csl.getDefaultColor();        final int paddingBottom = getPaddingBottom();        final int paddingTop = getPaddingTop();        final int viewWidth = getWidth();        final int viewHeight = getHeight();        final TextPaint paint = getPaint();        paint.setColor(color);        final float bottom = viewWidth * 9.0f / 11.0f;        Path p = new Path();        p.moveTo(bottom, viewHeight - paddingBottom - paddingTop);        p.lineTo(bottom, paddingTop);        canvas.drawTextOnPath(getText().toString(), p, 0, 0, paint);    }}

This verticaltextview works much like a textview, it supports most usual attributes like color, size, background, margin and padding (only paddingtop and paddingbottom ). for others, I did not implement but you can get the attributes
In ondraw and apply them. Cost efforts though, it is feasible. Here is an example to use it:

    <com.hilton.todo.VerticalTextView        android:id="@+id/header"        android:layout_height="fill_parent"        android:layout_width="30dip"        android:textColor="#ffff00"        android:textSize="24sp"        android:background="#a50909"        android:text="Hello, world"        android:paddingBottom="40dip" />

There might be some other ways to achieve this like rotation, which I tried first but not with success. This example really works for me and if you ever find another way please share to us.

From this example, we learn that android is so flexible that we can achieve almost anything we want and the only cost is your efforts.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.