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.