0

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".

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rocky
  • 1,287
  • 3
  • 15
  • 37

3 Answers3

1

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

Community
  • 1
  • 1
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
  • Thx all the same, but I wanna the text center in the TextView ,So I used android:gravity="center". – Rocky Mar 22 '12 at 09:17
0

set it as below:

android:layout_width="fill_parent"
android:gravity="center"
android:paddingLeft="5dp"              //your desired value
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • But I wanna get the padding left value of the content in the TextView. – Rocky Mar 22 '12 at 09:18
  • what do you mean? please elaborate a little more – waqaslam Mar 22 '12 at 09:19
  • I mean I didnt set android:paddingLeft="5dp" ,I just set android:gravity="center"; So that the content aligns center in the TextView. But you know it has some space between the TextView's left and the content's left edge.I wanna get the width of the "space". – Rocky Mar 22 '12 at 09:25
  • then i believe it might be **Margin**. Paste your layout xml for better help – waqaslam Mar 22 '12 at 09:27
0

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; 
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
  • yeah, nothing returned.So how can I get the width between the left edge and the content's left edge? – Rocky Mar 22 '12 at 09:34
  • I saw one more function called getTotalPaddingLeft() but I think its gonna return the same result. – Shubhayu Mar 22 '12 at 09:43
  • Try this, Its a long shot. but what the hell. int txtSize = txt.getTextSize(); int count = txt.getText().toString().length(); int txtWidth = txtSize*count; int padding = txt.getWidth()/2 - txtWidth/2; – Shubhayu Mar 22 '12 at 09:48