How to build torch light application in Android Studio :-

To build torch light application in android studio first we need to create new project in android studio, to know how to create project in android studio you can check from here 


Lets add flashlight feature in manifest file:

<uses-feature android:name="android.hardware.camera.flash" />

 

Adding permission in manifest file 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.torch">
<uses-feature android:name="android.hardware.camera.flash" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Create Layout: activity_main.xml

torch light application layout 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Switch
android:id="@+id/switch_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerInParent="true" />
</RelativeLayout>

        
Switch view after creating layout
    

 Now working on java part of the application:

  public class MainActivity extends AppCompatActivity {
    TextView textView;
Switch aSwitch;
CameraManager cameraManager;
String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
aSwitch = findViewById(R.id.switch_);

//check flash is available or not...
boolean isFlashAvailable = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

//this give true or false
//if true falsh is present if false device has no flash.... :)

//now we can show an alert dialog if there is no flash in the device..

//cheking,...


// !isFlashAvailable it means if flash is not available '!' refers not in java
if (!isFlashAvailable) {
showAlertDialog();

}
// since flash is from camera class we have to import camera class

cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
//it may returns an exception so we should write in try and catch blocks..

id = cameraManager.getCameraIdList()[0];

} catch (CameraAccessException e) {
e.printStackTrace();
}

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//here b value maybe true or false..
// handles the switch on off cases here,for example...
if (b) {
//if b is true
//if switch is on we have to turn on flash
//it may also return exception so surround with try and catch blocks..
//press alt+enter
try {
textView.setText("Torch ON");
cameraManager.setTorchMode(id, b);
} catch (CameraAccessException e) {
e.printStackTrace();
}
} else {
//if b is false...
// if switch if off we have to turn of the flash light
try {
textView.setText("Torch OFF");
cameraManager.setTorchMode(id, b);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
});
}

//if there is not flash this method calls..
private void showAlertDialog() {
//creating alert dialog...

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert");
builder.setMessage("Sorry this device has no flash light..");
// handling buttons

//it creates a button in alert dialog
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//now we can close the app be cause there is no flash in the device.. this happens when user
//clicks OK button
// to close the app completely
// use inbuilt method finish();
finish();
}
});
//we have created alert dialog we have to show the dialog ...
builder.show();
}
.
}

 Run the app
          
     
        
            







Comments

Popular posts from this blog