Adsense Ad

Tuesday, 12 September 2017

Simple Torch App for Android

How to create simple torch application in android


First create simple app with empty activity, clear all padding and set all default thing.
Secondly create two identified buttons or Image Buttons is up to development requirement.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/off"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:id="@+id/off"
        android:onClick="Torch"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/on"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:id="@+id/on"
        android:onClick="Torch"
        android:visibility="gone"

        />

</RelativeLayout>

Now, to create torch app, we required permissions and access to control camera and its feature flash light.
For permission go to manifest.xml and change in existing code for permissions.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.example.user.mesale">
    <
uses-permission android:name="android.permission.CAMERA"/>
    <
uses-permission android:name="android.permission.FLASHLIGHT"/>
    <
uses-feature android:name="android.hardware.camera"/>
    <
uses-feature android:name="android.hardware.flash"/>
    <
application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:supportsRtl="true"
       
android:theme="@style/AppTheme">
        <
activity android:name=".MainActivity"
           
android:theme="@style/AppTheme">
            <
intent-filter>
                <
action android:name="android.intent.action.MAIN" />

                <
category android:name="android.intent.category.LAUNCHER" />
            </
intent-filter>
        </
activity>
    </
application>
</
manifest>


No go to activity’s java class and create following code
public class MainActivity extends AppCompatActivity {

ImageButton on,off;
Camera camera;
Camera.Parameters parameters;
boolean isFlash = false;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
on = (ImageButton) view.findViewById(R.id.on);
       
off = (ImageButton) view.findViewById(R.id.off);
       
if (view.getContext().getApplicationContext().getPackageManager().hasSystemFeature(view.getContext().getPackageManager().FEATURE_CAMERA_FLASH)){
           
isFlash=true;
           
camera=Camera.open();
           
parameters = camera.getParameters();
           
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
           
camera.setParameters(parameters);
           
camera.startPreview();
        }
else {
            Toast.makeText(getActivity(),
"Please Install Camera",Toast.LENGTH_LONG).show();
        }

       
on.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
               
camera.setParameters(parameters);
               
camera.startPreview();
               
on.setVisibility(View.GONE);
               
off.setVisibility(View.VISIBLE);
            }
        });

       
off.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
               
camera.setParameters(parameters);
               
camera.stopPreview();
               
on.setVisibility(View.VISIBLE);
               
off.setVisibility(View.GONE);
            }
        });

    }

   
@Override
   
public void onStop() {
       
super.onStop();
       
if(camera!=null){
           
camera.release();
        }
    }

   }
}


No comments: