Home > database >  Is there a windowMaxWidthMajor and windowMaxWidthMinor for Android dialogs?
Is there a windowMaxWidthMajor and windowMaxWidthMinor for Android dialogs?

Time:03-16

For custom Android dialog boxes:

The attribute android:windowMinWidthMajor defines the minimum size for a dialog's width when it is along the major axis (that is the screen is landscape).

The attribute android:windowMaxWidthMinor defines the minimum size for a dialog's width when it is along the minor axis (that is the screen is portrait).

Can we also define maximum sizes for a dialog's width?

CodePudding user response:

I would wrap your dialog in a LinearLayout and set the max width on it.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:maxWidth="350dp" >

    <Rest of the XML for your dialog layout>

</LinearLayout>

Here is an example of how to line up a view using percentages for the min (10%) and max (90%). It will set the left edge of the TextView or your dialog code to 10% of the screen width and the right edge at 90% of the screen width. It usages a ConstraintLayout with Guidelines. The TextView can be replaced with your dialog code.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@ id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <androidx.constraintlayout.widget.Guideline
        android:id="@ id/guideline2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.10" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@ id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.90" />

    <TextView
        android:id="@ id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintEnd_toStartOf="@ id/guideline3"
        app:layout_constraintStart_toStartOf="@ id/guideline2"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • Related