Reputation: 119
i want to set padding of an element in android in order to have 3.2% in left , right and bottom padding, i've done some research but i didn't get anything interesting , here what i've tried.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
FrameLayout rel = (FrameLayout) findViewById(R.id.tabcontent);
double width_pas= width*3.2;
double height_pas=height*3.2;
rel.setPadding(width_pas,0,width_pas,height_pas);
but i get error in the setpadding , it only accepte integers , but i need to set margin at 3.2 hope you can help me.
Upvotes: 2
Views: 2323
Reputation: 17854
You can achieve this with Guidelines
<?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">
<android.support.constraint.Guideline
android:id="@+id/startVerticalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.032" />
<android.support.constraint.Guideline
android:id="@+id/endVerticalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.968" />
<android.support.constraint.Guideline
android:id="@+id/bottomGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.968" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#AAA"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/startVerticalGuideline"
app:layout_constraintEnd_toStartOf="@+id/endVerticalGuideline"
app:layout_constraintBottom_toTopOf="@+id/bottomGuideline" />
</android.support.constraint.ConstraintLayout>
Upvotes: 1
Reputation: 31468
Three things:
0.032
and not by 3.2
.ints
instead of floats/doubles
you will end up with 3%
of padding instead of 3.2%
. It depends on what you're gonna round. If you calculate your paddings this way:int width_pas = Math.round(width * 0.032f);
int height_pas = Math.round(height * 0.032f);
you will end up with as accurate 3.2% percent of height/width as you can get.
Upvotes: 1