3

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> 

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

try that

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

Comments

0

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

Comments

0

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.

1 Comment

Yes you are right. If you target <21 then you have to do what Vucko proposed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.