First App dev. help

Search This thread

natan.p

Member
May 28, 2014
12
0
Hello I'm Trying to learn my firsy app (Flash Lifght app) ..

I understand that i need to give app permissions etc.
Can some one expaine me how do i need to do that when im opening new project.
I what is code or the file i need to paste the code..

Sorry for bad english and thanks for helping!
 

MasterAwesome

Recognized Developer
Jun 23, 2013
2,019
3,985
OnePlus 6
Hello I'm Trying to learn my firsy app (Flash Lifght app) ..

I understand that i need to give app permissions etc.
Can some one expaine me how do i need to do that when im opening new project.
I what is code or the file i need to paste the code..

Sorry for bad english and thanks for helping!

You need to set the permissions in the AndroidManifest.xml file add a uses permission and under that class an android:name= *camera*

Regards
MasterAwesome
 

natan.p

Member
May 28, 2014
12
0
You need to set the permissions in the AndroidManifest.xml file add a uses permission and under that class an android:name= *camera*

Regards
MasterAwesome

I have an error i found tutorial how create flash light app i cop all codes ti manifest etc files but i have error with the java file........
the error: The public type FlashlightActivity must be defined in its own file.

the code: Press Control + F and search this line public class FlashlightActivity extends Activity {
PHP:
package com.natan.flashlight;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.widget.ToggleButton;

/**
 * @author Prabu 
 * July 30 2013
 * @version 1.0
 * 
 */

public class FlashlightActivity extends Activity { // <--------------------------------------- There are the error..
	 private Camera camera;
	 private ToggleButton button;
	 private final Context context = this;

     //@see android.app.Activity#onStop()
	 @Override
	 protected void onStop() {
	  super.onStop();
	  if (camera != null) {
	   camera.release();
	  }
	 }

	 /**
	  * @see android.app.Activity#onCreate(android.os.Bundle)
	  */
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  //setContentView(R.layout.flashlight);
	  button = (ToggleButton) findViewById(R.id.togglebutton);
	  
	  final PackageManager pm = context.getPackageManager();
	  if(!isCameraSupported(pm)){
	   AlertDialog alertDialog = new AlertDialog.Builder(context).create();
	   alertDialog.setTitle("No Camera");
	      alertDialog.setMessage("The device's doesn't support camera.");
	      alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
	          public void onClick(final DialogInterface dialog, final int which) { 
	           Log.e("err", "The device's doesn't support camera.");
	          }
	       });
	   alertDialog.show();
	  }
	  camera = Camera.open();
	 }
	 public void onToggleClicked(View view) {
	  PackageManager pm=context.getPackageManager();
	  final Parameters p = camera.getParameters();
	  if(isFlashSupported(pm)){
	   boolean on = ((ToggleButton) view).isChecked();
	   if (on) {
	    Log.i("info", "torch is turn on!");
	    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
	    camera.setParameters(p);
	    camera.startPreview();
	   } else {
	    Log.i("info", "torch is turn off!");
	    p.setFlashMode(Parameters.FLASH_MODE_OFF);
	    camera.setParameters(p);
	    camera.stopPreview();
	   }
	  }else{
	   button.setChecked(false);
	   AlertDialog alertDialog = new AlertDialog.Builder(context).create();
	   alertDialog.setTitle("No Camera Flash");
	      alertDialog.setMessage("The device's camera doesn't support flash.");
	      alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
	          public void onClick(final DialogInterface dialog, final int which) { 
	           Log.e("err", "The device's camera doesn't support flash.");
	          }
	       });
	   alertDialog.show();
	  }
	 }
	 /**
	  * @param packageManager
	  * @return true <b>if the device support camera flash</b><br/>
	  * false <b>if the device doesn't support camera flash</b>
	  */
	 private boolean isFlashSupported(PackageManager packageManager){ 
	  // if device support camera flash?
	  if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
	   return true;
	  } 
	  return false;
	 }

	 /**
	  * @param packageManager
	  * @return true <b>if the device support camera</b><br/>
	  * false <b>if the device doesn't support camera</b>
	  */
	 private boolean isCameraSupported(PackageManager packageManager){
	  // if device support camera?
	  if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
	   return true;
	  } 
	  return false;
	 }
	}

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}
 

MasterAwesome

Recognized Developer
Jun 23, 2013
2,019
3,985
OnePlus 6
I have an error i found tutorial how create flash light app i cop all codes ti manifest etc files but i have error with the java file........
the error: The public type FlashlightActivity must be defined in its own file.

the code: Press Control + F and search this line public class FlashlightActivity extends Activity {
PHP:
package com.natan.flashlight;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.widget.ToggleButton;

/**
 * @author Prabu 
 * July 30 2013
 * @version 1.0
 * 
 */

public class FlashlightActivity extends Activity { // <--------------------------------------- There are the error..
 private Camera camera;
 private ToggleButton button;
 private final Context context = this;

     //@see android.app.Activity#onStop()
 @Override
 protected void onStop() {
  super.onStop();
  if (camera != null) {
   camera.release();
  }
 }

 /**
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.flashlight);
  button = (ToggleButton) findViewById(R.id.togglebutton);
  
  final PackageManager pm = context.getPackageManager();
  if(!isCameraSupported(pm)){
   AlertDialog alertDialog = new AlertDialog.Builder(context).create();
   alertDialog.setTitle("No Camera");
      alertDialog.setMessage("The device's doesn't support camera.");
      alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
          public void onClick(final DialogInterface dialog, final int which) { 
           Log.e("err", "The device's doesn't support camera.");
          }
       });
   alertDialog.show();
  }
  camera = Camera.open();
 }
 public void onToggleClicked(View view) {
  PackageManager pm=context.getPackageManager();
  final Parameters p = camera.getParameters();
  if(isFlashSupported(pm)){
   boolean on = ((ToggleButton) view).isChecked();
   if (on) {
    Log.i("info", "torch is turn on!");
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(p);
    camera.startPreview();
   } else {
    Log.i("info", "torch is turn off!");
    p.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(p);
    camera.stopPreview();
   }
  }else{
   button.setChecked(false);
   AlertDialog alertDialog = new AlertDialog.Builder(context).create();
   alertDialog.setTitle("No Camera Flash");
      alertDialog.setMessage("The device's camera doesn't support flash.");
      alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
          public void onClick(final DialogInterface dialog, final int which) { 
           Log.e("err", "The device's camera doesn't support flash.");
          }
       });
   alertDialog.show();
  }
 }
 /**
  * @param packageManager
  * @return true <b>if the device support camera flash</b><br/>
  * false <b>if the device doesn't support camera flash</b>
  */
 private boolean isFlashSupported(PackageManager packageManager){ 
  // if device support camera flash?
  if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
   return true;
  } 
  return false;
 }

 /**
  * @param packageManager
  * @return true <b>if the device support camera</b><br/>
  * false <b>if the device doesn't support camera</b>
  */
 private boolean isCameraSupported(PackageManager packageManager){
  // if device support camera?
  if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
   return true;
  } 
  return false;
 }
}

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

You can't have 2 public class in the same file.. Create a new class

Regards
MasterAwesome