How to Display Upside Down Text with a Textview in Android

How can you display upside down text with a textview in Android?

in the xml file add:

android:rotation = "180"

in the respective element to display text upside down.

for example:

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView"
android:rotation="180"/>

Drawing text at an angle (e.g. upside down) in Android

How can you display upside down text with a textview in Android?

how to rotate a textView in android?

you can use rotate function in xml

android:rotation="-180"

for dynamic use

textview.setRotation(-180);

Override Button to draw text upside down

You can use the rotation attribute in your button something like this:

<Button
android:id="@+id/test_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text button"
android:rotation="180"/>

Or if you prefer using a custom button, you can create it something like this:

public class FlippedTextButton extends Button {
public FlippedTextButton(Context context) {
super(context);
}

public FlippedTextButton(Context context, AttributeSet attrs) {
super(context, attrs);
}

public FlippedTextButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.save();

float y = this.getHeight() / 2.0f;
float x = this.getWidth() / 2.0f;
canvas.rotate(180, x, y);

super.onDraw(canvas);
canvas.restore();
}
}

Then you can use it with:

<!-- Change to your class package name. -->
<com.example.flippedtext.FlippedTextButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flipped text"/>

Here is the sample project on github: https://github.com/isnotmenow/FlippedText

Is it possible to write vertically in a textview in android?

You can set your textview as you would normally do

for example:

 <TextView android:id="@+id/txtview"
android:layout_height="fill_parent"
android:layout_width="wrap_content" />

and write a function in your activity to

  • reverse the characters in your text
  • insert \n after every characters

and then set the text to the TextView.

If you dont want to insert the \n, you will have to set the size of android:layout_width and play with font size not to have 2 characters fitting on the same line and no truncation

Edit
If I have understood you correctly, you can get what you want by using animation.

For example

Under res/anim/myanim.xml:

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:duration="0" />

You will have to play with this file to define where you want your text view to be placed.

In your activity:

  TextView t = (TextView)findViewById(R.id.txtview);
String txt = "Stackoverflow";
t.setText(txt);

RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim);
ranim.setFillAfter(true); //For the textview to remain at the same place after the rotation
t.setAnimation(ranim);

TextView upside down in Android when set support RTL and Device in Hebrew Language

There are two options:
1.Change your layout to be RelativeLayout instead of LinearLayout and set the direction using "layout_leftOf" etc

Edit 1

Something like that:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/Net"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:gravity="end"
android:text="@string/Net"
android:textColor="@color/textPrimaryColor"
android:textDirection="rtl"
android:textSize="25sp"
android:textStyle="bold" />

<TextView
android:id="@+id/animText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_toRightOf="@+id/Net"
android:gravity="end"
android:text="@string/C"
android:textColor="@color/animl"
android:textDirection="rtl"
android:textSize="30sp"
android:textStyle="bold"

/>

<TextView
android:id="@+id/lub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:layout_toRightOf="@+id/animText"
android:gravity="end"
android:text="@string/Lub"
android:textColor="@color/textPrimaryColor"
android:textDirection="rtl"
android:textSize="25sp"
android:textStyle="bold" />

</RelativeLayout>

2.Change your layout order to be:

<TextView
android:layout_width="wrap_conten1t"
android:layout_height="wrap_content"
android:id="@+id/lub"
android:text="@string/Lub"
android:gravity="end"
android:textDirection="rtl"
android:layout_marginEnd="3dp"
android:textStyle="bold"
android:textColor="@color/textPrimaryColor"
android:textSize="25sp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/animText"
android:text="@string/C"
android:layout_marginEnd="2dp"
android:textDirection="rtl"
android:gravity="end"

android:textStyle="bold"
android:textColor="@color/animl"
android:textSize="30sp"

/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:layout_marginEnd="2dp"
android:textDirection="rtl"
android:text="@string/Net"
android:textStyle="bold"
android:textColor="@color/textPrimaryColor"
android:textSize="25sp"
android:id="@+id/Net"
/>

Rotate Text in Canvas

I got the solution from the comment by Romain Guy below the accepted answer

How can you display upside down text with a textview in Android?

Quoting You can just scale by -1 on the Y axis.

  @Override 
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cx = this.getMeasuredWidth() / 2;
int cy = this.getMeasuredHeight() / 2;
canvas.scale(1f, -1f, cx, cy);
canvas.drawText("3AM", cx, cy, p);

}

Sample Image

Complete Example:

public class SView extends View {

Paint p,paint;
public SView(Context context) {
super(context);
// TODO Auto-generated constructor stub
p = new Paint();
p.setColor(Color.RED);
p.setTextSize(40);
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(40);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cx = this.getMeasuredWidth() / 2;
int cy = this.getMeasuredHeight() / 2;

canvas.drawText("3AM", cx, cy, paint);
canvas.save();

canvas.scale(1f, -1f, cx, cy);
canvas.drawText("3AM", cx, cy, p);
canvas.restore();
}
}

Snap

Sample Image



Related Topics



Leave a reply



Submit