Androwed
Androwed

Reputation: 443

Android activity as Dialog: Doesn't display layout content properly

I have an DialogActivity with an EditText and a Button. Here's the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/colorPrimaryLight2">

    <EditText
        android:id="@+id/input_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:inputType="textEmailAddress"
        android:textColor="@color/colorPrimary"
        android:textColorHint = "@android:color/secondary_text_light_nodisable"
        android:hint="Contact ID"
        android:padding="15dp"/>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input_username"
        android:layout_centerHorizontal="true"
        android:padding="12dp"
        android:background="@drawable/elevation"
        android:textColor="@color/colorPrimary"
        android:text="Add Contact"/>

</RelativeLayout>

Activity in manifest :

<activity android:name=".AddContactActivity"
    android:screenOrientation="portrait"
    android:label="Add Contact"
    android:theme="@style/Theme.AppCompat.Light.Dialog">
</activity>

What my Activity looks like :

Dialog Activity

Problem is that activity contents of layout are overlapping. What I need is the height of the dialog should wrap the contents of Activity.

Any help would be great !! Thanks.

Upvotes: 0

Views: 119

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Just remove attributes android:layout_centerHorizontal="true" and android:layout_centerVertical="true" from your EditText.

Update your dialog layout as below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/colorPrimaryLight2">

    <EditText
        android:id="@+id/input_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:textColor="@color/colorPrimary"
        android:textColorHint = "@android:color/secondary_text_light_nodisable"
        android:hint="Contact ID"
        android:padding="15dp"/>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input_username"
        android:layout_centerHorizontal="true"
        android:padding="12dp"
        android:background="@drawable/elevation"
        android:textColor="@color/colorPrimary"
        android:text="Add Contact"/>

</RelativeLayout>

OUTPUT:

enter image description here

Hope this will help~

Upvotes: 1

Reaz Murshed
Reaz Murshed

Reputation: 24211

Remove the android:layout_centerVertical="true" from the EditText. The final layout should look like this.

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

    <EditText
        android:id="@+id/input_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:hint="Contact ID"
        android:inputType="textEmailAddress"
        android:padding="15dp"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@android:color/secondary_text_light_nodisable" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_below="@+id/input_username"
        android:layout_centerHorizontal="true"
        android:padding="12dp"
        android:text="Add Contact"
        android:textColor="@color/colorPrimary" />

</RelativeLayout>

Upvotes: 1

Related Questions