ActionBarDrawerToggle doesn't open when I use below this code?

17 Views Asked by At

java class code:

public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    private ZXingScannerView mScannerView;
    private ImageView image1;
    private MaterialToolbar materialToolbar;
    private DrawerLayout drawerLayout;

    NavigationView navigationView;

    private static final int PICK_IMAGE_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mScannerView = new ZXingScannerView(this);

        // Initialize MaterialToolbar
        materialToolbar = new MaterialToolbar(this);
        //setSupportActionBar(materialToolbar);
        materialToolbar.setId(View.generateViewId());
        RelativeLayout.LayoutParams toolbarParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        toolbarParams.addRule(RelativeLayout.ALIGN_PARENT_START);
        toolbarParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        materialToolbar.setLayoutParams(toolbarParams);

        materialToolbar.setNavigationIcon(R.drawable.ic_menu_open);


        image1 = new ImageView(this);
        image1.setId(View.generateViewId());
        image1.setImageResource(R.drawable.ic_image_24);
        RelativeLayout.LayoutParams image1Params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        image1Params.addRule(RelativeLayout.END_OF, materialToolbar.getId());
        image1.setLayoutParams(image1Params);
        image1Params.setMargins(50,35,0,0);

        // Add views to a RelativeLayout
        RelativeLayout layout = new RelativeLayout(this);
        layout.addView(mScannerView);
        layout.addView(materialToolbar);
        layout.addView(image1);


        // Initialize DrawerLayout and NavigationView
        drawerLayout = new DrawerLayout(this);
        navigationView = new NavigationView(this);
        navigationView.setNavigationItemSelectedListener(item -> {
            // Handle navigation item clicks here
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        });
        drawerLayout.addView(navigationView, new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, GravityCompat.START));

        // Set DrawerToggle
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, materialToolbar, R.string.drawer_close, R.string.drawer_open);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pickImageFromGallery();
            }
        });

        setContentView(layout);

    }
    @Override
    public void onResume() {
        super.onResume();
        if (mScannerView != null) {
            mScannerView.setResultHandler(this);
            mScannerView.startCamera();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mScannerView != null) {
            mScannerView.stopCamera();
        }
    }

    public void handleResult(Result rawResult) {
        String scanResult = rawResult.getText();
        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("SCAN_RESULT", scanResult);
        startActivity(intent);

        // Vibrate to indicate successful scan
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        if (vibrator != null) {
            vibrator.vibrate(100);
        }

        // Continue scanning after a short delay (2 seconds)
        mScannerView.postDelayed(() -> mScannerView.resumeCameraPreview(ScanActivity.this), 2000);
    }

    private void pickImageFromGallery() {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        galleryIntent.setType("image/*");
        startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
    }
}

I tried to try this more and more time. Please fixed my problem. I want when I click ActionBarDrawerToggle then open drawerLayout ,but doesn't open drawerLayout and not any error message in logcat .

0

There are 0 best solutions below