Mode77
Mode77

Reputation: 1041

AlertDialog with custom layout not showing up

I'm trying to create an alert dialog with a custom view layout, but the screen just dims with no dialog appearing.

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(findViewById(R.id.system_profile_dialog));
AlertDialog setSysProfileDialog = builder.create();
setSysProfileDialog.show();

XML:

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="@string/system_profile_hint"
        android:inputType="number" />
</LinearLayout>

Upvotes: 3

Views: 6222

Answers (4)

colens
colens

Reputation: 487

Just replace builder.setView(findViewById(R.id.system_profile_dialog)); with builder.setView(R.id.system_profile_dialog). If you check the official documentation you will find that setView(View) is just settting the view to be shown , but setView(int layoutResourceId) is inflating the view as well.

Upvotes: 0

Vucko
Vucko

Reputation: 7479

The problem is this line:

builder.setView(findViewById(R.id.system_profile_dialog));

Change it to:

builder.setView(LayoutInflater.from(this).inflate(R.layout.layout_name, null, false));

You're supposed to pass an inflated view there, not an id of a view in current activity.

Upvotes: 3

Cristian
Cristian

Reputation: 389

Try instantiating the builder with the actual Context object, calling getApplicationContext()

Upvotes: 0

yazan
yazan

Reputation: 23

try that

Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.YourFile);
        EditText editText = (EditText)dialog.findViewById(R.id.YourEditText);  
        dialog.show();

Upvotes: 1

Related Questions