0

I want to show a dialog with a message and some buttons at the bottom of the dialog.

I intent to add more controls to dialog so I use a custom view.

Here is the code:

My hv_popup.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:background="#ffff00">

    <LinearLayout
        android:id="@+id/hvBottomBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"> 

        <Button
            android:id="@+id/btnSwitch"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="MTD/HV" />

        <Button
            android:id="@+id/btnEDIT"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="EDIT" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CLOSE" />

    </LinearLayout>

    <TextView
        android:id="@+id/etHV"
        android:background="#000000"
        android:textColor="#ffffff"
        android:textIsSelectable="true"
        android:textSize="12sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/hvBottomBar"/>
</RelativeLayout>

Java code:

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

dialog.setContentView(R.layout.hv_popup);

final TextView tv = (TextView) dialog.findViewById(R.id.etHV);
tv.setText("text");
dialog.show();

Result:

The problem:

  • When etHV have a short text, dialog take full space of the screen, while I only want it wrap to content height.
  • When etHV have a long text, it take all space available, it push the button out of screen

My expected result:

  • the dialog align to bottom of the screen
  • the dialog wrap to its content height
  • the buttons must always be visible, even when I set long text to etHV.
  • when etHV have long text, it only take the remain space of visible dialog, which become scrollable and user still see the buttons at the bottom of the dialog.
double-beep
  • 5,031
  • 17
  • 33
  • 41
NoName
  • 7,940
  • 13
  • 56
  • 108
  • u need to change your layout..what do u want in dialog? textveiw and 3 button correct? – sravs Apr 07 '17 at 06:01
  • im not sure what is causing problems, but: https://github.com/WithoutCaps/DialogsCheatSheet check it out, may help. (there are quite a few dialog samples) (for you "custom dialog" or "fragment dialog" should get the job done, i guess "fragment dialog" would be better in your case) –  Apr 21 '17 at 19:35

1 Answers1

0

You need to change the layout to wrap the content. Try the following layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffff00">
    <TextView
        android:id="@+id/etHV"
        android:background="#000000"
        android:textColor="#ffffff"
        android:textIsSelectable="true"
        android:textSize="12sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:id="@+id/hvBottomBar"
        android:layout_below="@id/etHV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnSwitch"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="MTD/HV" />

        <Button
            android:id="@+id/btnEDIT"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="EDIT" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CLOSE" />

    </LinearLayout>


</RelativeLayout>
sravs
  • 330
  • 2
  • 14
  • Problem with your layout is when i set a long text to `etHV`, the buttons being hidden completely. I want those button always show when I set text to the textView – NoName Apr 07 '17 at 11:03
  • I am not getting any problem in setting big text – sravs Apr 07 '17 at 13:05
  • Have you tried with very long text? (> 300 lines). Yes still I can scroll up down to read full text, but the button being push away of the screen. – NoName Apr 07 '17 at 13:57