3

I have a Button inside an Activity with Dialog style. Here is xml file of my Activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button" />

</RelativeLayout>

Here is the style of my Activity which causes to show it as a Dialog

<style name="Modal" parent="Theme.AppCompat.Light.Dialog">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

This Activity show as full screen even though the android:windowFullscreen is false! and when I remove the android:layout_alignParentBottom="true" from the Activity xml file, the height of Activity changed to wrap_content and there is no problem. I want to know how can I solve this problem without removing the attribute of Button.

Here you can see the result of showing Activity

with android:layout_alignParentBottom="true"

enter image description here

and with out android:layout_alignParentBottom="true".

enter image description here

Nava
  • 368
  • 2
  • 11
  • 1
    Can you elaborate the exact problem? – M.Waqas Pervez Jun 25 '16 at 06:17
  • I want to have an Activity with Dialog style. it's OK and I can start Activity with dialog style successfully as you can see in my screenshots. My problem is height of this activity which is match_parent instead of wrap_content! I want the second screenshot with setting android:layout_alignParentBottom to "true". because in another situation I want to start this Activity without Dialog style and if I remove this attribute my Button doesn't locate at the bottom of screen. Is it clear enough? @M.WaqasPervez – Nava Jun 25 '16 at 10:52

3 Answers3

2

You can achieve this by setting attribute

 android:layout_alignParentBottom="true"

programmatically using the below code

    RelativeLayout relativeLayout = view.findViewById(R.id.relative_layout);
    Button button = relativeLayout.findViewById(R.id.button);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, view.getId());
    button.setLayoutParams(params);

in your xml file you need to set a id attribute and remove android:layout_alignParentBottom="true"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/relative_layout"
android:layout_height="match_parent">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Button" />

</RelativeLayout>
Aman Jain
  • 2,975
  • 1
  • 20
  • 35
  • You are right. It seems this is the better way to deal with this problem! But I wished the `RelativeLayout` handle this problem itself in some way. – Nava Jun 28 '16 at 12:07
  • I will try to figure it out and will let u know. – Aman Jain Jun 28 '16 at 15:48
1

Why don't you use the FrameLayout instead?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Button" />

</FrameLayout>
x0r
  • 1,271
  • 11
  • 13
  • Thanks to your guidance. I have a complex layout and I want to solve this problem with RelativeLayout rather than change the layout root. – Nava Jun 25 '16 at 10:59
0

Try to add this runtime when you create dialog...

mDialog.show();
Window window = mDialog.getWindow();
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

As i did in my case... May it help @Nava

Kush
  • 1,080
  • 11
  • 15
  • As I said I have an Activity with Dialog style. I tested your code after setContentView() method in my Activity class. unfortunately this didn't work for me! – Nava Jun 25 '16 at 11:03