开发者

custom alert dialog not getting displayed

开发者 https://www.devze.com 2023-02-07 01:31 出处:网络
I want to display a custom alert dialog whose structure has been defined in an XML file. This is the code I used.

I want to display a custom alert dialog whose structure has been defined in an XML file. This is the code I used.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(findViewById(R.layout.inputform));
builder.show();

It is fired when a button is clicked. However, the window was not getting displayed. I got this transparent grey layer on top of the screen and could not click on anything. Some basic codes like using setMessage or displaying checkboxes work well, though. Can anyone point out where I do wrong?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"开发者_高级运维 android:layout_height="fill_parent">

<TableLayout android:id="@+id/TableLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

<TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="Room name" android:id="@+id/TextView01"
        android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <EditText android:hint="Enter room name" android:id="@+id/EditText01"
        android:layout_width="0dp" android:layout_height="wrap_content"
        android:layout_weight="1"/>
</TableRow>

<TableRow android:id="@+id/TableRow02" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="Point id" android:id="@+id/TextView02"
        android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <EditText android:hint="Enter point id" android:id="@+id/EditText02"
        android:layout_width="0dp" android:layout_height="wrap_content"
        android:layout_weight="1"/>
</TableRow>

<TableRow android:id="@+id/TableRow03" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="OK" android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</TableRow>

</TableLayout>
</LinearLayout>


This is because findViewById is for finding a specific view in the current activity´s layout, not for loading a whole layout.

You can use @Matt's answer or maybe you can inflate the layout with a LayoutInflater like this:

LayoutInflater li = getLayoutInflater();
View v = li.inflate(R.layout.inputform);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(v);
builder.show();

(Not tried it but I think it should work).


try this:

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.inputform);
dialog.show();


Try adding builder.create(); before builder.show().


You can use the following class

import android.app.Dialog;
import android.content.Context;

public class CustomDialog extends Dialog {


    public CustomDialog(Context context) {
        super(context);

        }


    public static CustomDialog show(Context context, CharSequence title,
            CharSequence message) {
        return show(context, title, message, false);
    }

    public static CustomDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate) {
        return show(context, title, message, indeterminate, false, null);
    }

    public static CustomDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate, boolean cancelable) {
        return show(context, title, message, indeterminate, cancelable, null);
    }

    public static CustomDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate,
            boolean cancelable, OnCancelListener cancelListener) {
        CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(null);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);


        dialog.setContentView(R.layout.test);
        dialog.show();

        return dialog;
    }
            }


Simple way to represent dialog...

AlertDialog dialog ;

AlertDialog.Builder builder=new AlertDialog.Builder(this);

builder.setTitle("Your Title Here");

builder.setView(view);//view is your inflate xml layout

dialog = builder.create();

    dialog.show();


dialog.dismiss();//whenever you want to dismiss ,put this line

simple dialog here

0

精彩评论

暂无评论...
验证码 换一张
取 消