Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:33:28+00:00 2026-06-17T14:33:28+00:00

I have a Flashlight code that use two buttons (on_btn and off_btn) to turn

  • 0

I have a Flashlight code that use two buttons (on_btn and off_btn) to turn on and turn off the Flashlight.

How can I associate them in a single button?

Very novice can you give elaborate suggestion please?

The code below is found from an answer: And it works for my mobile. But on the emulator it crashs while clicking on the button. Here is the cat-log https://dl.dropbox.com/u/15065300/logcat1.png

The line number 74 is: Parameters params = mCamera.getParameters();

Can anybody any suggestion please?

public class FlashLight extends Activity {


    private final static String LOG_TAG = "FlashLight";  
    private Button mOnBtn;   
    private Camera mCamera;    
    private boolean isActive;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      //this will be inside your onCreate...
        mOnBtn = (Button) findViewById(R.id.on_btn);
        mOnBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                flipSwitch();
                processClick();

            }
        });
   }

    //these will be outside your onCreate
    public void flipSwitch() {
        isActive = !isActive;
    }

    @Override
    protected void onResume() {
        super.onResume();
        try{
            mCamera = Camera.open();
            mCamera.startPreview();
            Toast.makeText(getApplicationContext(),"Camera is  present", Toast.LENGTH_LONG).show();

        } catch( Exception e ){
            Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
        }
    }

    @Override
    protected void onPause() {
        if( mCamera != null ){
            mCamera.release();
            mCamera = null;
        }
        super.onPause();
    }

    public void processClick() {
        if(isActive) {
            Parameters params = mCamera.getParameters();
            params.set("flash-mode", "torch");
            mCamera.setParameters( params );
            mCamera.startPreview();
        }

        else { 

             Parameters params = mCamera.getParameters();
             params.set("flash-mode", "off");
             mCamera.setParameters( params );
             mCamera.stopPreview();

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:33:29+00:00Added an answer on June 17, 2026 at 2:33 pm

    use the button to flip a boolean value, and change your flashlight to go on and off accordingly.

    private boolean isActive;
    
    //this will be inside your onCreate...
    button.setOnClickListener(new View.onClickListener() {
        public void onClick(View v) {
            flipSwitch();
            processClick();
        }
    }
    
    //these will be outside your onCreate
    public void flipSwitch() {
        isActive = !isActive;
    }
    
    public void processClick() {
        if(isActive) {
            //button is clicked on
        }
        else {
            //button is clicked off
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have an app that can use tts to read text messages. It can also
Have a photography site that I want to prevent image copying from. How can
I have created a flashlight application...Everything is working fine...except that whenever the flashlight is
In my application i have an image button that basically switches on and off
As I was reading RedBook I stayed quite confused, that openGL can have maximum
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
Hi I have find a way to switch on and off the led flashlight
Have deployed numerous report parts which reference the same view however one of them
I have a Flash/Flex object (Flashlight-VNC), which I would like to dynamically resize to
I'm trying to make a flashlight app for my iPhone. I have an iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.