1

I faced a problem with DialogFragment. The layout is changing depending on the API. I use a PercentRelativeLayout.

  • For API >= 21, the sizes are percentages of the screen and there is no title.
  • Pof API < 21, the sizes change (I guess they are percentages of the Dialog's size) and there is a title.

There are 2 pictures below to show the problem.

With API < 23

With API >= 23

It was easy to find how to delete the title but for the percentage I have no idea how to do. Do you know an easy way to solve it? Additive question: in my research I read PercentRelativeLayout will be deprecated. is there a better way for doing my layout?

Here is my code:

<?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"
    android:id="@+id/layout_dialog2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/round">

    <TextView
        android:id="@+id/text"
        app:layout_marginTopPercent="3%"
        app:layout_heightPercent="26%"
        app:layout_widthPercent="100%"
        android:text="@string/rate"
        android:textSize="@dimen/button"
        android:textColor="#1565c0"
        android:gravity="center_horizontal"/>

    <Button
        android:text="@string/yes"
        android:background="@drawable/button"
        android:textColor="#FFFFFF"
        android:textAllCaps="false"
        android:textSize="@dimen/button"
        app:layout_widthPercent="20%"
        app:layout_heightPercent="6%"
        android:id="@+id/buttonY"
        app:layout_marginTopPercent="20%"
        app:layout_marginLeftPercent="27%"/>

    <Button
        android:text="@string/no"
        android:background="@drawable/button"
        android:textColor="#FFFFFF"
        android:textAllCaps="false"
        android:textSize="@dimen/button"
        app:layout_widthPercent="20%"
        app:layout_heightPercent="6%"
        android:id="@+id/buttonN"
        app:layout_marginTopPercent="20%"
        app:layout_marginLeftPercent="53%"/>
</android.support.percent.PercentRelativeLayout>
Onik
  • 19,396
  • 14
  • 68
  • 91
  • "in my research I read PercentRelativeLayout will be deprecated" -- it was deprecated a few weeks ago, with the formal release of version `26.0.0` of the support libraries (now up to `26.0.2`). "is there a better way for doing my layout?" -- you can accomplish percentage-based sizing using `ConstraintLayout` or nested weighted `LinearLayouts`. However, I do not know why you are using a percentage system for this dialog. – CommonsWare Sep 04 '17 at 22:05
  • Thank you for you answer. I found PercentageRelative layout really practical to adapt my layouts nicely to every phone or tab. This Dialog is just an exemple. I have more complicated ones. – Jean-Philippe Mary Sep 04 '17 at 22:11
  • I don't think you should derive height of text views (TextView and Button) from their parents' dimensions. As you can see, 6% of Button's parent's height does not allow the entire button text to be seen, and results in poor user experience. – Eugen Pechanec Sep 04 '17 at 22:15

0 Answers0