java - how to trigger an imagebutton programmatically in android studio -
i want trigger imagebutton according value gets stored in variable.
eg: let variable amount. if amount<10 , amount>50 imagebutton should triggered.
here, through imagebutton i'm turning on , off flashlight. so, if the
amount>10 , <30 flashlight turns on
amount>30 , <50 flashlight turns off
secondly, i'm getting value in string form function, converted integer , stored in amount variable.
java code:
integer amount; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d("bluetooth_torch", "oncreate()"); setcontentview(r.layout.activity_bluetooth_torch); mtorchonoffbutton = (imagebutton)findviewbyid(r.id.button_on_off); istorchon = false; boolean isflashavailable = getapplicationcontext().getpackagemanager() .hassystemfeature(packagemanager.feature_camera_flash); if (!isflashavailable) { alertdialog alert = new alertdialog.builder(bluetooth_torch_activity.this) .create(); alert.settitle("error !!"); alert.setmessage("your device doesn't support flash light!"); alert.setbutton(dialoginterface.button_positive, "ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // closing application finish(); system.exit(0); } }); alert.show(); return; } mcameramanager = (cameramanager)getsystemservice(context.camera_service); try { mcameraid = mcameramanager.getcameraidlist()[0]; } catch (cameraaccessexception e) { e.printstacktrace(); } amount = integer.parseint(data); mtorchonoffbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try { if (istorchon) { turnoffflashlight(); istorchon = false; } else { turnonflashlight(); istorchon = true; } } catch (exception e) { e.printstacktrace(); } } }); }
mtorchonoffbutton.callonclick()
Comments
Post a Comment