Dynamically tune custom content AlertDialog

I have a custom dialog that I configured as a function:

public void customDialog(Bitmap bm, String title){ TextView dialogtext = (TextView)findViewById(R.id.layout_root); //For above also tried r.id.popup which is the containing file for the layout ImageView dialogimage = (ImageView)findViewById(R.id.popupimage); dialogtext.setText(title); dialogimage.setImageBitmap(bm); LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.layout_root)); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(layout); builder.show(); } 

The dialog fails when I try to set the XML fields dynamically using the Null Pointer exception. Am I at a dead end, any ideas? Should I add something to the manifest for a custom dialog?

+4
source share
1 answer

do it first:

View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.layout_root));

Then, after defining layout , do the following:

ImageView dialogimage = (ImageView) layout.findViewById(R.id.popupimage);

Call findViewByID() on the new layout you created, and not on the parent content view.

So, two changes: Ordering and layout.findView not findView

+2
source

Source: https://habr.com/ru/post/1397136/


All Articles