0

I want to trigger an imagebutton according to the value that gets stored in a variable.
For eg: Let variable be amount. Then if amount<10 and amount>50 then the imagebutton should be triggered.
Here, through imagebutton I'm turning on and off the flashlight. So, if the
amount>10 and <30 then flashlight turns on
amount>30 and <50 then flashlight turns off

Secondly, I'm getting my value in string form from a function, which is to be converted into integer and 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 the 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();
                }
            }
        });
    }
Sugandha
  • 59
  • 7
  • I don't get the problem ? What's the issue ? – HelloSadness Nov 13 '16 at 11:50
  • 1
    Any particular reason you can't just directly call the code that would normally be called in response to the click? – clownba0t Nov 13 '16 at 11:56
  • 1
    Why dont you refactor the code to separate method and call it whenever necessary instead of creating additional overhead of button click and performing the same task – Mohammed Atif Nov 13 '16 at 12:35
  • 1
    `mTorchOnOffButton.performClick()` – Reaz Murshed Nov 13 '16 at 12:48
  • Possible duplicate of [Can i click a button programmatically for a predefined intent?](http://stackoverflow.com/questions/5701666/can-i-click-a-button-programmatically-for-a-predefined-intent) – Reaz Murshed Nov 13 '16 at 12:49
  • As being a newbie to android studio, I'm unable to use performClick() perfectly. Can you please provide me with were and how can I apply it. And also the conditions of amount variable can be placed. – Sugandha Nov 13 '16 at 20:23

2 Answers2

1

mTorchOnOffButton.callOnClick()

Pein
  • 1,059
  • 3
  • 10
  • 31
0

You can call click event on a button in two ways:

mTorchOnOffButton.performClick();

This will call click event just like you have clicked a button yourself.

mTorchOnOffButton.callOnClick();

This will call just the OnClickListener method of button and unlike performClick() it wont report any accessibility event.

Rafay Ali
  • 723
  • 7
  • 15