Rocky
Rocky

Reputation: 1287

Android: Text in the TextView

I set android:gravity="center" in TextView for the text in it.And now I wanna get the paddingLeft value of the text.How can I achieve this??? Thanks in advance. PS: I did not set any padding attribute in TextView, except "center".

Upvotes: 0

Views: 122

Answers (3)

Shubhayu
Shubhayu

Reputation: 13562

Try and use getPaddingLeft() but I am not sure whether it will return any value since you are not setting any padding. I don't think setting the gravity actually effects the padding.

UPDATE

This would be a hack to do it and a little inaccurate. (I'll see if I can improve it)

int txtSize = txt.getTextSize(); 
int count = txt.getText().toString().length(); 
int txtWidth = txtSize*count; 
int padding = txt.getWidth()/2 - txtWidth/2; 

Upvotes: 0

Dmytro Danylyk
Dmytro Danylyk

Reputation: 19796

Use android:layout_gravity instead of android:gravity
Alse check this issue.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

set it as below:

android:layout_width="fill_parent"
android:gravity="center"
android:paddingLeft="5dp"              //your desired value

Upvotes: 0

Related Questions