Mariox
Mariox

Reputation: 135

What is TextView Gravity by default?

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?

Upvotes: 7

Views: 4150

Answers (3)

MANI
MANI

Reputation: 187

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

Upvotes: 1

Msp
Msp

Reputation: 2493

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.

Upvotes: 5

hata
hata

Reputation: 12478

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.

Upvotes: 7

Related Questions