8

I am new in android development. I want to set margin & padding in Xml,not in dp or px but in percentage. Is there any way to do this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Himanshu Dudhat
  • 1,609
  • 3
  • 14
  • 27
  • Hey i think this is not possible [Look Here](http://stackoverflow.com/questions/5996571/android-how-do-i-set-a-percentage-padding-margin-so-edittext-has-10-margin-on-e) – LuminiousAndroid Jun 29 '12 at 05:53

4 Answers4

5

it is not possible, though You better do it by taking width and height of screen like below in your java code, and

 Display display = getWindowManager().getDefaultDisplay(); 
 int width = display.getWidth();
 int height = display.getHeight();

then calculate your margin from screen size, and set it by

  setmargin(int x) //method of view/layout.

this way you can set margin depending on the screen size not fixed for all screen, and basically it is like setting in percentage but from Java code.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • 1
    Using getWidth() and getHeight() is deprecated in API level 13+. http://developer.android.com/reference/android/view/Display.html. Use getSize(point) instead. – maraci Oct 14 '14 at 16:48
  • 1
    @maraci check my another answer for your concern Link >> http://stackoverflow.com/a/11483404/519718 – AAnkit Oct 15 '14 at 03:05
1

It became possible with Guidelines introduced in ConstraintLayout.

Here's the example of TextView placed at 60% of screen width and 25% of screen height:

Layout Editor

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintLeft_toLeftOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline1"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.6" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />

</android.support.constraint.ConstraintLayout>
Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
0

If you are trying to share sreen width between your controls in percentage. Best way to do that in android is using weight property for them. Check following URL for more details.

Android Layout Paramas guide

regards, Aqif Hamid

Aqif Hamid
  • 3,511
  • 4
  • 25
  • 38
0

Though it is quite late, it can be done with Android percentageRelativeLayout https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html One can use percentage with some attributes but not all are supported. Like padding is still not supported in percentage whereas margin is.

Raghav Sharma
  • 780
  • 10
  • 18