How to get access of oncreate variable accesss with in the ActivityResultLauncher Method Android

54 views Asked by At

I am using this Barcode Scanner Library Dependenceny in my code.

 implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

I am working in fragment and in fragment when i click on button there is a dialog appears and in the dialog i have an scan button to can the barcode and want to show the same text inside the the dialog's Textview. My dialog code is inside the oncreate but the ActivityResultLauncher in out of scope so how can i make my dialog variable to be in scope of ActivityResultLauncher. I am very thankful to all of you developer to answer this query.

here is the oncreate code :

fab_addproduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheetDialog = new BottomSheetDialog(getContext(), R.style.AppBottomSheetDialogTheme);
                bottomSheetDialog.setContentView(R.layout.productadd_bottom_sheet);
                TextInputEditText product_id = bottomSheetDialog.findViewById(R.id.product_id);
                TextInputEditText product_name = bottomSheetDialog.findViewById(R.id.product_name);
                TextInputEditText product_quantity = bottomSheetDialog.findViewById(R.id.product_quantity);
                MaterialButton add_btn = bottomSheetDialog.findViewById(R.id.add_btn);
                ImageView barcode_scanner = bottomSheetDialog.findViewById(R.id.barcode_Scanner);
                TextInputEditText product_price = bottomSheetDialog.findViewById(R.id.product_price);


                barcode_scanner.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        ScanOptions options = new ScanOptions();
                        options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
                        options.setPrompt("Scan a barcode");
                        options.setCameraId(0);  // Use a specific camera of the device
                        options.setBeepEnabled(true);
                        options.setBarcodeImageEnabled(true);
                        barcodeLauncher.launch(options);
                    }

                });


                bottomSheetDialog.show();


            }
        });


here is the ActivityResultLauncher code where the barcode result data is avai

 public final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
            result -> {
                if(result.getContents() == null) {
                    Toast.makeText(getContext(), "Cancelled", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getContext(), "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
**                    product_id.settext(result.getContents());// this is not accessable i want to make it accessable. here product_id is red in the code.
**                }
            });

I have tried many ways by using the varaiable static its also show error. i also use binding to make it accessable but i am not able to achieve my goal.

0

There are 0 answers