0

I'm trying to standardize the size of a button across multiple Android devices with regards to height and width. For example, I'd like the length to be one-third of the device's width and the height to be about 5-10% of the device's height. I've tried expanding upon:

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center|bottom"

        android:gravity="center_horizontal"

to no avail.

Any help would be appreciated. I'm running Android Studio 1.4.0.

-A

2 Answers2

0

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>
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • Being new to Android Studio, how do I get Percent Relative Layout into my app? Do I create a new layout resource file? If yes, how do I incorporate that into activity_main.xml? –  Jul 06 '18 at 15:20
  • @Aneyth Yes it will require gradle dependancy. Hope you found that. I have updated answer also. – Pankaj Kumar Jul 07 '18 at 02:56
0

Try to do it programatically from java

1.get the device screen dimensions, and compute what you need.

2.call setHeight and setWidth on the target views, or through layout params.

Abaqus
  • 49
  • 1
  • 6