You can use PercentRelativeLayout
in this case
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.journaldev.percentlayouts.MainActivity">
<Button
android:text="Hello World!"
android:textSize="20sp"
app:layout_heightPercent="10%"
app:layout_widthPercent="30%" />
</android.support.percent.PercentRelativeLayout>
I will require gradle dependancy as
compile 'com.android.support:percent:23.3.0'
And if you are using latest API (As PercentRelativeLayout
is deprecated) you can do it with ConstraintLayout
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="vertical"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="vertical"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="horizontal"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bottom_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="horizontal"/>
<Button
android:text="Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
app:layout_constraintTop_toTopOf="@+id/top_guideline"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" />
</android.support.constraint.ConstraintLayout>