Animesh Jena Animesh Jena - 3 days ago 5
Android Question

Add the flashlight button above the Zxing Scanner page

I have been developing an scanner application where I have been implementing the Zxing bar code scanner library. I am trying to add a new button(flashlight button) on the scanner page above the camera view but failing. Can anyone help me in putting a button above the scanner view of Zxing? Below I am posting my code. Please help.

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{

Integer response = 0 ;
int currentIndex=0;

private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("onCreate", "onCreate");
mScannerView = new ZXingScannerView(this);
Button scanButton= new Button(this);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));
scanButton.setText("Flash Light");
scanButton.setLayoutParams(params);
mScannerView.addView(scanButton);
setContentView(mScannerView);

}
private void checkPermissions() {
try {
for (int i = currentIndex; i < permissions.length; i++) {
currentIndex = currentIndex + 1;
int result = ContextCompat.checkSelfPermission(context, permissions[i]);
if (result == PackageManager.PERMISSION_GRANTED) {
} else {
requestPermission(permissions[i]);
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

Activity activity = this;
Context context = this;

String[] permissions = new String[]{Manifest.permission.CAMERA};


private void requestPermission(String permission) {

// if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
// Toast.makeText(context,
// "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",
// Toast.LENGTH_LONG).show();
// } else {
ActivityCompat.requestPermissions(activity, new String[]{permission},
101);
// }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Snackbar.make(view, "Permission Granted, Now you can access location data.", Snackbar.LENGTH_LONG)
// .show();
checkPermissions();
} else {
try {
// FuncUtils.showToast(context, permissions[0] + " Denied!!!");
} catch (Exception e) {
e.printStackTrace();
}
// onBackPressed();
//// Snackbar.make(view, "Permission Denied, You cannot access location data.", Snackbar.LENGTH_LONG).show();
}
break;
}
}


@Override
public void onResume() {
super.onResume();

if(response == 1) {
mScannerView = null;
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
response = 0;

}

mScannerView.setResultHandler(this);
mScannerView.startCamera();

}

@Override
public void onDestroy() {
super.onDestroy();


mScannerView.stopCamera();

}

@Override
protected void onRestart() {
super.onRestart();
Log.d("ani","onrestart");
}

@Override
public void handleResult(Result rawResult) {



final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_screen);
dialog.getWindow().setLayout(700, 850);
TextView text = (TextView) dialog.findViewById(R.id.CouponCode);
Button dialogButton = (Button) dialog.findViewById(R.id.save_code);
// if button is clicked, close the custom dialog
text.setText(rawResult.getText());
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
dialog.dismiss();
MainActivity.this.finish();
}
});

dialog.show();
Log.e("Ani", rawResult.getText());
Log.e("Ani", rawResult.getBarcodeFormat().toString());



}
}

Answer

Add your button and ZxingView to a parent and remove the first call to setContentView

super.onCreate(savedInstanceState);
  FrameLayout frameLayout = new FrameLayout(this, new 
                new ViewGroup.LayoutParams(
                        ViewGroup.MATCH_PARENT,
                        ViewGroup.MATCH_PARENT))
  mScannerView = new ZXingScannerView(this);
        Button scanButton= new Button(this);
        FrameLayoutparams =
                new FrameLayout.LayoutParams(
                        FrameLayout.WRAP_CONTENT,
                        FrameLayout.WRAP_CONTENT);
        scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));
        scanButton.setText("Flash Light");
        scanButton.setLayoutParams(params);
 frameLayout .addView(mScannerView );
        frameLayout .addView(scanButton);
        setContentView(frameLayout );
Comments