Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - set imageView inside DialogFragment

I have an application wherein I have a custom listView, upon clicking any item in the listView, a DialogFramgent is shown. Now, what I want to do is to set the imageView I have inside that DialogFramgent to an image in the external storage. I know that I can set the source of the imageView as such:

File imgFile = new  File(“/sdcard/Images/test_image.jpg”);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageViewItem);
    myImage.setImageBitmap(myBitmap);

}

However, I couldn't initialize my ImageView variable properly because findViewById gives an error: `The method findViewById(int) is undefined for type ItemDialog.

Here is the part in my custom listView onClickLisenter where I call my DialogFragment:

resultListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, final View v, final int position, long id) {

        positionSelected = position;

        SearchResultListViewItem selectedRow = results.get(position);
        String selectedItem = selectedRow.getItemName();
        itemBarcode = selectedRow.getBarcode();

        itemListDB.open();

        ItemDialog itemDialog = new ItemDialog();

        itemDialog.itemName = selectedItem;
        itemDialog.brandName = itemListDB.getBrandFromItemBarcode(itemBarcode);
        itemDialog.UOMName = itemListDB.getUOMFromItemBarcode(itemBarcode);
        itemDialog.priceName = itemListDB.getPriceFromItemBarcode(itemBarcode);
        itemDialog.discountName = itemListDB.getDiscountFromItemBarcode(itemBarcode);
        itemDialog.mListener = SearchResult.this;

        itemDialog.qtyFromList = qtyInput.get(position);
        itemDialog.discFromList = qtyInput.get(position);

        itemListDB.close();
        itemDialog.show(getFragmentManager(), "");

    }
});

and here is my ItemDialog class:

public class ItemDialog extends DialogFragment implements OnClickListener{

    //my variables go over here

    public interface onSubmitListener {
        void setOnSubmitListener(String qty, String disc);  
    }  

    @Override  
    public Dialog onCreateDialog(Bundle savedInstanceState) {  

        final Dialog dialog = new Dialog(getActivity());  
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  

        dialog.setContentView(R.layout.fragment_item_dialog);  
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);

        File imgFile = new  File("/sdcard/Images/test_image.png");
        if(imgFile.exists()){

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

            ImageView myImage = (ImageView) findViewById(R.id.imageViewItem);
            myImage.setImageBitmap(myBitmap);

        }

        dialog.setTitle("Item Details");
        dialog.show();  
        //I set more variables here       
        return dialog;  
    }       

}

So, does anyone have an idea on how to set the source image of an imageView that's inside a DialogFragment? I'm stuck here and frankly, I have no idea how to proceed and I'll be really grateful if anyone can help me.

Thank you.

like image 972
Razgriz Avatar asked May 09 '26 01:05

Razgriz


1 Answers

You need to inflate layout in your Dialog Fragment under onCreateDialog(...)

  LayoutInflater layoutInflater = (LayoutInflater) getActivity()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View layout2 = layoutInflater.inflate(R.layout.contact_filter_popup,null);

  dialog.setView(layout2);

and then reference your Imageview with that particular layout

   ImageView img=(ImageView)layout2.findViewById(R.id.imageViewItem);

Update:

@Override  
public Dialog onCreateDialog(Bundle savedInstanceState) {  

    final Dialog dialog = new Dialog(getActivity());  

    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
            WindowManager.LayoutParams.FLAG_FULLSCREEN);  


     LayoutInflater layoutInflater = (LayoutInflater) getActivity()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout2 = layoutInflater.inflate(R.layout.contact_filter_popup,null);
    dialog.setContentView(layout2);  

    setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);

    ImageView myImage = (ImageView) layout2.findViewById(R.id.imageViewItem);

    File imgFile = new  File("/sdcard/Images/test_image.png");

    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        myImage.setImageBitmap(myBitmap);

    }

    dialog.setTitle("Item Details");
    dialog.show();  

    return dialog;  
}       
like image 127
M D Avatar answered May 11 '26 14:05

M D