Jasper Hsieh
Jasper Hsieh

Reputation: 3

android custom dialog view not fit in the dialog layout

I create a alertdialog dynamically, it seems okay except that there are some space between the top of dialog view and the top of dialog layout margin. Check the pic would be more clear.

dialog layout margin problem

Is there some problem when I inflate the view? I just google it, but I cannot find any clues. Please help to modify the layout.

The dialog.xml is as following

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">
    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="20sp"
        android:background="@color/light_blue"
        android:gravity="center"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        android:paddingTop="5sp"
        android:paddingBottom="5sp"
        android:textColor="@color/white"
        />
    <TextView
        android:id="@+id/dialog_msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/hello"
        android:textSize="17sp"
        android:paddingTop="15sp"
        android:paddingBottom="10sp"
        android:textColor="@color/black"
        />
    <Button
        android:id="@+id/dialog_button"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_centerInParent="true"
        android:text="@string/Ok"
        android:textColor="@color/light_blue"
        android:textSize="18sp"
        android:background="@color/white"
        />
</LinearLayout>

And the code I dynamic create dialog

private AlertDialog createDialog(String title, String msg){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //AlertDialog dialog = builder.create();

    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
        (Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = inflater.inflate(R.layout.dialog, null);
    ((TextView)dialogView.findViewById(R.id.dialog_title)).setText(title);
    ((TextView)dialogView.findViewById(R.id.dialog_msg)).setText(msg);
    builder.setView(dialogView);
    final AlertDialog dialog = builder.create();
    ((Button)dialogView.findViewById(R.id.dialog_button)).setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            dialog.dismiss();
        }
    });
    return dialog;
}

Upvotes: 0

Views: 1214

Answers (2)

SripadRaj
SripadRaj

Reputation: 1735

Change your LinearLayout's width and height from wrap_content to match_parent and try.

Also try this

dialog.setView(dialogView,0,0,0,0); after calling

final AlertDialog dialog = builder.create(); in your method

Upvotes: 1

Andolasoft Inc
Andolasoft Inc

Reputation: 1296

You have missed to show the bulider before return dialog.

builder.show();

Upvotes: 0

Related Questions