# Smartphone lantern flashlight on/off

**URL:** <https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324>\
**Category:** Processing for Android\
**Tags:** homework\
**Created:** [April 2, 2020, 3:59am UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324 "2020-04-02T03:59:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinoelectro](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@dinoelectro](https://discourse.processing.org/u/dinoelectro)\
**Post date:** [April 2, 2020, 3:59am UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/1 "2020-04-02T03:59:06Z")

</div>

Hello, dear friends, I am getting started in the programming for android, and I want to do something simple, by pressing the screen to turn on / off the flashlight of the smartphone.

Does anyone know how to do it?

Cheers

```auto
int uX, uY;
color bgColor;
  void setup(){
  size(400,500);
  uX=width/14;
  uY=height/14;

}

  void draw(){
  background(bgColor);
  if(mousePressed)bgColor=255;//ON
  else bgColor=127;//OFF
  }

```

---

<div class="post-metadata">

**Author:** ![dinoelectro](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@dinoelectro](https://discourse.processing.org/u/dinoelectro)\
**Post date:** [April 2, 2020, 4:24am UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/2 "2020-04-02T04:24:26Z")

</div>

![Captura](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/39e7abe597614dcfbb2898593051513cb97c1aca.png)

I tell you that I am improving the “GUI” for the flashlight, I want to contribute with the code of this “toogle button” in case someone helps you

```auto
verticalToggle VT;
int uX, uY;
color bgColor;
  void setup(){
  size(400,500);
  uX=width/14;
  uY=height/14;
 //verticalToggle(Xpos, Ypos, width, height, backColor, buttonColor, textColor, TextWhenOFF, textWhenOn);
  VT = new verticalToggle(4*uX,uY,6*uX,12*uY,color(127,127,0),color(255,255,0),color(0),"OFF","ON");
}

void draw(){
  background(bgColor);
  VT.drawToggle();
  if(mousePressed || VT.value)bgColor=255;//ON
  else bgColor=127;//OFF
  }
  
class verticalToggle{
  String lbl1, lbl2;
  int x, y, w, h;
  float slvalue;
  boolean value;
  color col, txtCol, color1, color2, textColor;
   verticalToggle(int _x, int _y, int _w, int _h, color _color1, color _color2, color _textColor, String _lbl1, String _lbl2){
    x=_x;
    y=_y;
    w=_w;
    h=_h;
    color1=_color1;
    color2=_color2;
    textColor=_textColor;
    textSize(h/8);
    textAlign(CENTER);
    lbl1=_lbl1;
    lbl2=_lbl2;
  }
  
    
  void drawToggle() {
  fill(color1);  
  rect(x,y,w,h,30);
      if(value){
      pushMatrix();
      translate(x,y);
      fill(color2);
      rect(0,0,w,h/2,30);
      fill(textColor);
      textSize(h/8);
      textAlign(CENTER);      
      text(lbl2, (w)/2, (1.3*h)/2);
      popMatrix();
    }else{
      pushMatrix();
      translate(x,y);
      fill(color2);
      rect(0,h/2,w,h/2,30);
      fill(textColor);
      textSize(h/8);
      textAlign(CENTER);
      text(lbl1, (w)/2, (1.3*h)/2);
      popMatrix();
    }
  if (mouseX>x && mouseX<x+w && mouseY>y && mouseY<y+h/2) {
    if (mousePressed) {
      value=true;
  }
  }else if (mouseX>x && mouseX<x+w && mouseY>y+h/2 && mouseY<y+h) {
        if (mousePressed) {
      value=false;
  }
  }
}
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 2, 2020, 8:55am UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/3 "2020-04-02T08:55:19Z")

</div>

Hi @dinoelectro Welcome to the forum.  
I think you can find something useful [here,](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android)

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [April 2, 2020, 1:40pm UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/4 "2020-04-02T13:40:56Z")

</div>

@dinoelectro , @noel ===

The code you have linked has some problems:

- As it is it will not work while targetting more than Lollypop; after you have to ask for permissions on runtime and not in the Manifest.
- In the Manifest you have to add an uses feature : uses-feature android:name=“android.hardware.camera” - This is mandatory for a play release
- Before trying tolaunch the camera and put the flash light on you have to verify that the flash light is available on the phone: in a lot of cases (tablets like nexus7 or 10) that is not true in order to do that you create a boolean isFlashAvailable, setting it to false, then:

```auto
Activity act = this.getActivity();
isFlashAvailable = act.getApplicationContext().getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        
        if(!isFlashAvailable){
       String leTxt = "Cet appareil n'a pas de flash!"; 
///here do what you want: finish or return or...
        }

```

- You have also to turn off the flash using the onPause() and not only with onStop();
- And finally you have to set the orientation: see what happens if you dont and turn the phone…

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 2, 2020, 1:57pm UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/5 "2020-04-02T13:57:33Z")

</div>

Thanks @akenaton for revising.  
I will correct that.  
If you have some time it would be great if you could revise the others as well. (once in a while 😄)

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [April 2, 2020, 3:44pm UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/6 "2020-04-02T15:44:03Z")

</div>

@noel ===  
ok; this repo is a good idea (i never saw it before, excuse me!)…  
As for the torch i found another problem with this code (perhaps related to API???): the getParameters() method from the camera is not called at the good moment and it returns empty parameters; you have to call it overriding onStart(); if it is usefull i can put the whole code, tested with Huawei nougat; but as it is for homework i dont like to do that…

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 2, 2020, 5:50pm UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/7 "2020-04-02T17:50:02Z")

</div>

Yes I saw that it is homework, and he already did his part, but I think that you can’t expect one can write the demanded android code programmatically right in the beginning. And that is why I did the repro. I don’t see the harm in that. But I’m glad you liked the idea.

---

<div class="post-metadata">

**Author:** ![dinoelectro](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@dinoelectro](https://discourse.processing.org/u/dinoelectro)\
**Post date:** [April 3, 2020, 12:11am UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/8 "2020-04-03T00:11:06Z")

</div>

> [@noel](#):
>
> at is why I did the repro

thanks, I’m experimenting with the code you shared … I don’t have a final result, once I get it I share it.

Also I will try to turn on / off the flashlight using voice commands. Do you have any suggestion?

regards!

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 3, 2020, 4:33am UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/9 "2020-04-03T04:33:42Z")

</div>

> [@dinoelectro](#):
>
> Also I will try to turn on / off the flashlight using voice commands.

You don’t need to write code for that. Just say “Ok Google. Turn on flashlight” e voila! 🙂  
But you can open an app and write a code to execute whatever you want. But you have to prepare your app, because Ok Google will not open any third party app. Maybe [this](https://developers.google.com/assistant/app) is a place to start.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [April 3, 2020, 8:56am UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/10 "2020-04-03T08:56:34Z")

</div>

@noel ===  
I must say that i am always very disturbed with “homework”; you wrote that it is impossible to ask a beginner to write android code, but in this case why the teacher has choosen to ask for???  
As for voice commands i think that the forum has to avoid “cascade” problems; when the first and main one is solved, open a new one!

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 3, 2020, 12:39pm UTC](https://discourse.processing.org/t/smartphone-lantern-flashlight-on-off/19324/11 "2020-04-03T12:39:02Z")

</div>

@akenaton  
I didn’t say that it’s impossible. It’s just hard because P4A has no documentation  
for working with views etc. You yourself have pointed out many times to use Android Studio instead for such tasks, and use Processing as an excellent graphics lib. I don’t think that a teacher will use A4P for actually teaching Android.  
Another reason why I would like to facilitate P4A with some ready code is that instead of using P4A with the Arduino Ide (which were born to live together); P4A has totally lost the battle against software like “MIT App Inventor” because it seems more simple. And that’s really a pity.  
I think many people come here to build an app for IoT purposes and they can’t even find a decent Bluetooth communication code. So you have to find a balance between giving a code to start with or trying to force someone to learn things bit by bit and taking the risk of just scare them away.
