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".
Asked
Active
Viewed 119 times
0
-
Do you get any info using getPaddingLeft()? – Shubhayu Mar 22 '12 at 09:31
-
nope.So can you help me to get the width between the left edge and the content's left edge? – Rocky Mar 22 '12 at 09:43
-
Did it help? The code in the comment below? – Shubhayu Mar 22 '12 at 10:06
-
yeah,Ive tried that code and it worked well.But not very accurate I think. :) – Rocky Mar 23 '12 at 04:03
-
ok so I am adding that as edit to the post. Might help someone, or someone could suggest something better in that line. – Shubhayu Mar 23 '12 at 04:35
3 Answers
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
-
-
-
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