Wednesday, 17 February 2016

Camera in android Studio

Today I'am learning about using camera in android and its attributes and codes, lets get started.
There are two main methods that are used while using a camera in app.
1). Direct approach (i.e image is directly saved to a image view or a  image button or background without saving it to the internal or external storage in the phone or external storage).
2). Indirect approach ( in which the image is firstly stored in internal or external storage and then acceded by the app contents).

The first approach is very simple and easy to implement which is as follows:-
Firstly set a button or any other onCLick attribute to any thing that may open a camera like I'am using onClick attribute in an Button. e.g

<ImageView
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/imageView"
          android:src="@drawable/icon"/>

<Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/button"
          android:onClick="camera"
          android:text="camera"/>

After the button is set and is accessed in java activity the following code can be applied to it.

//Declare the int variable in the top
final static int cameraData = 1999;

// declare the imageView or any other thing that may contain image.
ImageView iv;

//button on clicked
public void camera(View v) {
     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(intent, cameraData);

//accessing the data
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);

       if(requestCode == RESULT_OK) {
           Bundle extras = data.getExtras();
           Bitmap bmp = (Bitmap) extras.get("data");

           iv  = (ImageView) findViewById(R.id.imageView)
          iv.setImageBitmap(bmp);
          iv.setScaleType(ImageView.ScaleType.FIT_XY);
}

The second approach is similar to first one but includes some additional steps like:

//declaring image path
String cameraPath= Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";


public void camera(View v) {
        File cameraFile = new File(imageFilePath);
        Uri cameraUri= Uri.fromFile(imageFile);

       Intent in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
       in.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, cameraUri);
      startActivityForResult(in, cameraData);

// and receive the image as

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);

       if(requestCode == RESULT_OK) {
         BitmapFactory.Options bmpFactory = new BitmapFactory.Options();
         bmpFactory.inJustDecodeBounds = false;
         Bitmap bmp = BitmapFactory.decodeFile(ImageFilePath, bmpFactory);

           iv  = (ImageView) findViewById(R.id.imageView)
          iv.setImageBitmap(bmp);
          iv.setScaleType(ImageView.ScaleType.FIT_XY);
}


and this is how it is done.


       
         

Sunday, 14 February 2016

Dialog and Alert Dialog's

One of the Important things in android that one uses are the Alert Dialog, and these are also very easy to use and make.
Some very basic codes for building an Alert Dialog is.

AlertDialog.Builder builder = new AlertDialog.builder(this);
builder.setMessage(R.string.Tittle)  // title for the alert dialog 
                 .setPositiveButton(R.string.Button1, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id)  {
                         // set a action or Intent   
                          }
                 .setNegetiveButton(R.string.Button2, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                        // set a cancal button
                         }
                 .setNeturalButton(R.string.Button3, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                         // set another action or Intent
                        });
           builder.show();


and this code can be used with any button which may have onClick, or by simply using the onClick with a button or any thing that you may feel apropriate in the onCreate method in java class.
also any one button can also be skipped while placing the alert dialog. but not two buttons of same functions can be placed i.e two positive or two negative buttons simultaneously.

Apart from this the custom Dialog are also very useful which can be used as custom button and text view as one may wish. For this first the main thing that one has to build is a custom layout xml file which can be used as alert dialog, like as below.

custom_dialog.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">
    
       <Button
              android:layout_height="wrap_content"
              android:layout_weight="wrap_content"
              android:id="@+id/button1"
              android:text="First Button"/>
     <Button
              android:layout_height="wrap_content"
              android:layout_weight="wrap_content"
              android:id="@+id/button2"
              android:text="Second Button"/>

     <Button
              android:layout_height="wrap_content"
              android:layout_weight="wrap_content"
              android:id="@+id/cancel"
              android:text="Cancel"/>
</LinearLayout>


After the custom dialog is build  the next step is to build the dialog in java.

MainActivity.java

final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Tittle")
Button bCancel = (Button) dialog.findViewById(R.id.cancel);
            bCancel.setOnClickListener(new View,OnClickListener(){
                   @Override
                            public void onClick(View v) {
                                             dialog,dismiss();
                            }
                     });
dialog.findViewById(R.id.button1)
           .setOnClickListener(new View.OnClickListener(){
                 @Override
                 public void onClick(View v) {
                     // Do anything
                });
dialog.findViewById(R.id.button2)
            .setOnClickListener(new View.OnClickListener(){
                  @Override
                  public void onClick(View v) {
                       // Do some other thing or same thing depends on you
                  });

dialog.show();

And this code can be putted into any button that may be clicked or any other thing which has onClick property. And this one is custom build.


         

Back button pressed

Today I'am learning the back key function in android which can include going back to previous activity. This can be achieved by following code.

@Override
public void onBackPressed(){
       // do something on back or go to previous activity via intent
}

Or this code can also be used instead of using intent.

@Override
public void onBackPressed(){
       super.onBackPressed();
}

or on the hard way one can use this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
   if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() ==0){
         finish();
         return true;
  }
 return super.onKeyDown(keyCode, event);
}


I have done the first one and is working for me perfectly.