Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how do I set a percentage padding/margin so EditText has 10% margin on either side?

Does android support % or is there a way to approximate. I have two very different screen sizes and I but percentage-wise I want the EditBox in the activity to have same margin for both screen size as a proportion of screen size. How can this be done.

like image 237
barcodereader Avatar asked May 13 '11 19:05

barcodereader


People also ask

What is difference between margin and padding in Android?

Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent). Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object.

What is padding attribute in Android?

The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific number of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.

Why margin and padding are used in Android programming explain how both are different?

Both provides extra space/gap inside or outside the container. Then what's the exact difference? Let's get it cleared. In simple words, margin means to push outside, whereas padding means to push inside.

What is margin in Android layout?

android:layout_marginTop Specifies extra space on the top side of this view. This space is outside this view's bounds. Margin values should be positive. May be a dimension value, which is a floating point number appended with a unit such as " 14.5sp ".


1 Answers

It doesn't really support setting values by percent(except for some of the xml Animation files seem to) If you are dead set on using a percentage the best way I can think of is from java call getWidth and getHeight then multiply those by your decimal and set the result with setMargin(), or setPadding().

like image 138
FoamyGuy Avatar answered Oct 03 '22 13:10

FoamyGuy