What is TextView Gravity by default?
Asked Answered
I

3

7

It seems TextView text direction (or Gravity) automatically changes in Android when it gets text from RTL resources such as Arabic text or LTR resources like English. What is the default TextView direction (or Gravity) and how to get that? Actually I'm trying to find out what kind of text language (RTL or LTR) is entered? And what if it is hybrid text with both RTL and LTR?

Ivo answered 8/11, 2015 at 12:19 Comment(1)
If you want to know what kind of text language (RTL or LTR) is entered,just add a textWatcher.Yukyukaghir
H
7

What is the default TextView direction (or Gravity)

In the TextView source code it is initialized as:

private int mGravity = Gravity.TOP | Gravity.START;

It seems Gravity.TOP and Gravity.START. The Gravity START is independent for LTR or RTL.

and how to get that

So getting it (you only know it Gravity.START) is non-sense for your purpose -- to find out what kind of text language (RTL or LTR) is entered.

For other approach you can estimate whether text itself is RTL or LTR; discussed here: Identifyng RTL language in Android for example.

Hockey answered 8/11, 2015 at 12:53 Comment(1)
Thank you so much. I got the answer from the link you mentioned.Ivo
S
5

If you have a TextView in your layout, you can get the gravity of that view by using,

TextView textView = (TextView) findViewById(R.id.text_view);
int gravity = textView.getGravity();

By default, the value will be 8388659. Which is equal to 0x800033.

Refer the documentation for TextView here. By manipulating hex values you can get the default text direction for TextView.

Eg: In this case it is 0x800033. Which is 0x800003 | 0x30. That means gravity is start | top.

Shanteshantee answered 8/11, 2015 at 12:50 Comment(0)
I
1

default TextView gravity is - Gravity.TOP | Gravity.START

Inclination answered 29/3, 2016 at 9:4 Comment(1)
Does this answer the question, or is it a comment?Hardison

© 2022 - 2024 — McMap. All rights reserved.