Panda
Panda

Reputation: 152

TextView in dialog not getting aligned at top?

I have a dialog which has a header - textview. But the problem is the textview is getting aligned at the bottom of the layout. I want it to be center_vertical.

Any idea how to solve this?

<?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="wrap_content"
    android:clickable="true"
    android:minWidth="400dp"
    android:orientation="vertical">
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeLayout"
    android:layout_gravity="top"
    android:orientation="vertical"
    android:gravity="top"
    android:textAlignment="viewStart">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvHeader"
        android:text="Enter OTP"
        android:layout_gravity="center_horizontal|top"
        android:textStyle="bold"
        android:textSize="20sp"
        style="@style/AlertDialog.AppCompat"
        android:gravity="top" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_gravity="top"
        android:layout_below="@+id/tvHeader"/>
  </LinearLayout>
</LinearLayout>

Upvotes: 0

Views: 141

Answers (3)

Panda
Panda

Reputation: 152

The problem was with the title of the dialog was taking up the space. Even if I was not setting up any text for it. I removed the title by: thisDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

before setContentView and the extra space got removed.

Upvotes: 1

Lakhwinder Singh
Lakhwinder Singh

Reputation: 7209

<?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="wrap_content"
    android:clickable="true"
    android:minWidth="400dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvHeader"
        style="@style/AlertDialog.AppCompat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Enter OTP"
        android:textSize="20sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/tvHeader"
        android:layout_gravity="top"
        android:background="@android:color/darker_gray" />
</LinearLayout>

please try this

Upvotes: 0

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

You need to remove the nested LinearLayout and format your xml file properly.

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvHeader"
        style="@style/AlertDialog.AppCompat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Enter OTP"
        android:textSize="20sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_gravity="top"
        android:background="@android:color/darker_gray" />

</LinearLayout>

Upvotes: 0

Related Questions