Monday, April 20, 2015

Flashlight / Torch function using Camera instead of Camera2 in the Android 5.0 environment

1. Edit the AndroidManifest.xml file at app/src/main/res/AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kiosk.torch" >
    <uses-permission android:name="android.permission.CAMERA" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Note: Since the uses-permission has been modified, please ignore the warning that camera has been deprecated in Android 5.0 or above.

2. Edit MainActivity.java as below:

package com.example.kiosk.torch;

import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import static android.widget.ToggleButton.OnCheckedChangeListener;


public class MainActivity extends ActionBarActivity {

    ToggleButton myToggleButton;
    private Camera myCamera;
    Parameters myParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
        {
            Toast.makeText(getApplicationContext(), "Flash Detected!!", Toast.LENGTH_LONG).show();
            try {
                myCamera = Camera.open();
                myParams = myCamera.getParameters();
            } catch (RuntimeException e) {
                Log.e("Camera error: ", e.getMessage());
            }
        }
        else
        {
            Toast.makeText(getApplicationContext(), "No Flash Detected!!", Toast.LENGTH_LONG).show();
        }

        if ((myCamera == null) || (myParams == null)) {
            return;
        }

        myToggleButton = (ToggleButton)findViewById(R.id.toggleButton);

        myToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    myParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
                } else {
                    myParams.setFlashMode(Parameters.FLASH_MODE_OFF);
                }
                myCamera.setParameters(myParams);
                myCamera.startPreview();
            }
        });
    }

========================

Saturday, April 18, 2015

ToggleButton

1. Drag a toggle button to activity_main.xml and the XML code becomes:

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
        android:id="@+id/toggleButton"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="35dp"
        android:checked="false" />

2. Write the code as below to inform the user the toggle button is ON/OFF with toast:

package com.example.kiosk.togglebutton;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import static android.widget.ToggleButton.*;


public class MainActivity extends ActionBarActivity {

    ToggleButton myToggleButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myToggleButton = (ToggleButton)findViewById(R.id.toggleButton);

        myToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(getApplicationContext(), "ON!!", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "OFF!!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

============================
Toggle Button in iOS Swift

Thursday, April 9, 2015

How to remove Android Studio?

In case that you want to remove and reinstall Android Studio, you may try these steps:

1. Open the terminal. (Click the spotlight search / magnifying glass icon at the upper right corner of the screen)

2. Type in commands in the link.

Note 1. You may type a few letters such as rm ~/L and hit the tab button, then you'll see the content of the terminal becomes rm ~/Library/

Note 2. Wait patiently if your mac takes a while to run the below deletion command:


rm -Rf ~/Library/Android*

3. Install the Android Studio again. See the instructions.