I want to set max height in dialog fragment
Asked Answered
R

3

9
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:minWidth="@dimen/dialog_min_width"
    android:padding="@dimen/dialog_padding"
    android:background="@drawable/dialog_background" >

    <TextView android:id="@+id/base_dialog_title"
        style="@style/DialogTitleTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:gravity="center" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="16dp" >
    </FrameLayout>

<!-- *********************** HERE ************************* -->

    <FrameLayout android:id="@+id/base_dialog_content"
        android:background="@drawable/dialog_description_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>

<!-- *********************** HERE ************************* -->

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="16dp" >
    </FrameLayout>

    <LinearLayout android:id="@+id/base_dialog_button_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal">

    <Button
        android:id="@+id/base_dialog_button_negative"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="gone"/>

    <Button
        android:id="@+id/base_dialog_button_neutral"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="gone"/>

    <Button
        android:id="@+id/base_dialog_button_positive"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="gone"/>

    </LinearLayout>
</LinearLayout>

This is dialog fragment layout.

I add content view (TextView or ListView) into FrameLayout (@id/base_dialog_content)

When ListView has many Items, Dialog is full height in window.

I want to set maximum height dialog or content view (or list view)


Edit:

I solve the problem using OnLayoutChangeListener.

But I need same function in lower version (below HoneyComb)

In DialogFragment.onCreateView()

FrameLayout contentContainer = (FrameLayout) view.findViewById(R.id.base_dialog_content);       
final View contentView = createContentView(inflater, contentContainer);
contentContainer.addView(contentView);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    // TODO Check content view height and change height
} else {
    view.addOnLayoutChangeListener(new OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right,
                int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            int height = v.getHeight();
            int contentHeight = contentView.getHeight();
            int winHeight = ActivityUtil.getDisplayHeight(getSherlockActivity());

            LogUtils.pe(height+" / "+winHeight+" / "+contentHeight);

            int needHeight = height - winHeight*8/10;
            if (needHeight > 0) {
                contentView.setLayoutParams(
                        new LayoutParams(LayoutParams.MATCH_PARENT, contentHeight-needHeight));
            }
        }
    });
}
Rouble answered 28/5, 2013 at 4:32 Comment(2)
how long is the maximum height you want?is it defined in dp or based on the screen size?Harrar
@NAYOSO "based on screen size" is better. but "constant value in dp" is good for me, too.Rouble
H
2

to control the maximum height you have to put the content view in one layout container first (Linear/Relative) let's say in my example I'll use linearlayout

to measure the screen height (I put it in Util.java ):

public static int getDisplayHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay(); 
        int height = display.getHeight(); 

        return height;
    }

after that you create a container :

LinearLayout llContainerContent = new LinearLayout(context);
LayoutParams lpContainerContent = new LayoutParams(LayoutParams.FILL_PARENT, (Util.getDisplayHeight(context) * 8) /10);
llContainerContent.setLayoutParams(lpContainerContent);

after that you add the textview/listview to the container then you add the container to the framelayout. I hope my answer is clear enough for you but if you have another question please feel free to ask in the comment :)

Harrar answered 28/5, 2013 at 4:46 Comment(3)
Thanks. But It is not to set MAXIMUM height. I want to set maximum height not just height... It cannot check whether content view height is bigger than maximum height.Rouble
ah sorry I misunderstand what you mean, please wait I'll change the code laterHarrar
@Rouble but I think with my code the view won't be filling up the screen again, or do you want to measure all the child height and prevent the program to add more view if the sum height of the children is more than the parent?Harrar
A
1

Use this code for full screen height and width for the dialog

    WindowManager window = (WindowManager)context

    .getSystemService(Context.WINDOW_SERVICE);

    Display display = window.getDefaultDisplay();

    displayheight = display.getHeight();

    displaywidth = display.getWidth();
    dialog.getWindow().setLayout(displaywidth ,
                displayheight );
Automatic answered 28/5, 2013 at 5:56 Comment(0)
P
0

what about using the onResume to get it done:

override fun onResume() {
    super.onResume()
    val width = (resources.displayMetrics.widthPixels * 0.95).toInt()
    val height = (resources.displayMetrics.heightPixels * 0.60).toInt()
    dialog?.window?.setLayout(width, height)
}
Preraphaelite answered 24/11, 2021 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.