Reputation: 10226
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();
}
}
}
Upvotes: 2
Views: 1918
Reputation: 3730
You could always use the Toggle Button instead of two buttons. It's very "Android formal".
//Create a toggle button
ToggleButton tg = (ToggleButton) findviewbyId(R.id.togbut);
//Implement onClickListener
tg.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v){
//Flip on or off
}
});
In the onClick()
method, you can do what you need to do according to what your app is suppose to do when the button is on or off.
In the xml layout, you say what the toggle button says when it's on and off.
<ToggleButton
android:id="@+id/togbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Button On"
android:textOff="Button Off"
android:checked="true" />
When you click the toggle button, it will automatically change the text and toggle.
Here's a good example: http://www.mkyong.com/android/android-togglebutton-example/
Hope that helps!
Upvotes: 2
Reputation: 6085
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
}
}
Upvotes: 2