17 May 2012

Get Started and Tutorials in android.


What is Android?
Android is an operating system based on Linux with a Java programming interface. It provides tools, e.g. a compiler, debugger and a device emulator as well as its own Java Virtual machine (Dalvik Virtual Machine – DVM).

Android is officially guided by the Open Handset Alliance but in reality Google leads the project.

Android supports 2-D and 3-D graphics using the OpenGL libraries and supports data storage in a SQLite database.

Every Android applications runs in its own process and under its own user id which is generated automatically by the Android system during deployment. Therefore the application is isolated from other running applications and a misbehaving application cannot easily harm other Android applications.

The Android platform is a software stack for mobile devices including an operating system, middleware and key applications. Developers can create applications for the platform using the Android SDK. Applications are written using the Java programming language and run on Dalvik, a custom virtual machine designed for embedded use which runs on top of a Linux kernel.

An early look at the the Android SDK is also available. It includes sample projects with source code, development tools, an emulator, and of course all the libraries you’ll need to build an Android application.

Version history

Platform Version
API Level
VERSION_CODE
9
8
7
6
5
4
3
2
Android 1.0
1

Understanding Structure of android Application.
Android Layouts:

Android Tutorials:

Android Sample Code tutorials:
http://wptrafficanalyzer.in/blog/

All In One:
https://github.com/survivingwithandroid/Surviving-with-android
https://github.com/weixiao1984/Android-Infinite-Scroll-Listview
https://github.com/sgolivernet/curso-android-src/tree/develop

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)

9 May 2012

Underline a textview in android.


SpannableString contentUnderline = new SpannableString("http://hasmukhbhadani.blogspot.in/");
        contentUnderline.setSpan(new UnderlineSpan(), 0, contentUnderline.length(), 0);
        TextView mPage = (TextView) findViewById(R.id.page);
        mPage.setText(contentUnderline);


I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

4 May 2012

Crop image in android.

Sometimes when creating an Android app that includes user profile picture or avatar, we need to include a feature that enables users to select and crop image to update their profile picture. OnAndroid we can accomplish that by using intent to open image cropper app. To select an image from files, we can pass an intent to image gallery or file manager app then pass the selected image path to camera app to crop the image. It is also the same if we want to take a picture from camera, by passing an intent to camera app to open the camera, take a picture than save it to specified Uri then crop it.





main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Select and crop image"
        android:textSize="17sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_crop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select image" />

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

</LinearLayout>


crop_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:padding="10dp"
       android:gravity="center_vertical">
      
       <ImageView
              android:id="@+id/iv_icon"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
             
       <TextView
              android:id="@+id/tv_name"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text=""/>
</LinearLayout>


MainActivity:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.view.View;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.ImageView;

public class MainActivity extends Activity
{
       private Uri mImageCaptureUri;
       private ImageView mImageView;

       private static final int PICK_FROM_CAMERA = 1;
       private static final int CROP_FROM_CAMERA = 2;
       private static final int PICK_FROM_FILE = 3;

       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
              super.onCreate(savedInstanceState);

              setContentView(R.layout.main);

            final String [] items= new String [] {"Take from camera", "Select from gallery"};                       
              ArrayAdapter<String> adapter= new ArrayAdapter<String> (this,       android.R.layout.select_dialog_item,items);
              AlertDialog.Builder builder= new AlertDialog.Builder(this);

              builder.setTitle("Select Image");
              builder.setAdapter( adapter, new DialogInterface.OnClickListener()
              {
                     public void onClick( DialogInterface dialog, int item )
                     {
                           //pick from camera
                           if (item == 0)
                           {
                                  Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                           mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                     "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

                 intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

                                  try
                                  {
                                         intent.putExtra("return-data", true);

                                         startActivityForResult(intent, PICK_FROM_CAMERA);
                                  }
                                  catch (ActivityNotFoundException e)
                                  {
                                         e.printStackTrace();
                                  }
                           }
                           else
                           { //pick from file
                                  Intent intent = new Intent(); 
                                  intent.setType("image/*");
                                  intent.setAction(Intent.ACTION_GET_CONTENT);

           startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
                           }
                     }
              } );

              final AlertDialog dialog = builder.create();

              Button button= (Button) findViewById(R.id.btn_crop);
              mImageView = (ImageView) findViewById(R.id.iv_photo);

              button.setOnClickListener(new View.OnClickListener()
              {     
                     @Override
                     public void onClick(View v)
                     {
                           dialog.show();
                     }
              });
       }

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data)
       {
              if (resultCode != RESULT_OK) return;

              switch (requestCode)
              {
              case PICK_FROM_CAMERA:
                     doCrop();

                     break;

              case PICK_FROM_FILE:
                     mImageCaptureUri = data.getData();
                     doCrop();

                     break;       

              case CROP_FROM_CAMERA:           
                     Bundle extras = data.getExtras();

                     if (extras != null)
                     {                   
                           Bitmap photo = extras.getParcelable("data");

                           mImageView.setImageBitmap(photo);
                     }

                     File f = new File(mImageCaptureUri.getPath());           

                     if (f.exists()) f.delete();

                     break;

              }
       }

       private void doCrop()
       {
              final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

              Intent intent = new Intent("com.android.camera.action.CROP");
              intent.setType("image/*");

              List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

              int size = list.size();

              if (size == 0)
              {              
                     Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
                     return;
              }
              else
              {
                     intent.setData(mImageCaptureUri);

                     intent.putExtra("outputX", 200);
                     intent.putExtra("outputY", 200);
                     intent.putExtra("aspectX", 1);
                     intent.putExtra("aspectY", 1);
                     intent.putExtra("scale", true);
                     intent.putExtra("return-data", true);

                     if (size == 1) {
                           Intent i = new Intent(intent);
                           ResolveInfo res= list.get(0);

               i.setComponent( new ComponentName(res.activityInfo.packageName,  res.activityInfo.name));

                       startActivityForResult(i, CROP_FROM_CAMERA);
                     }
                     else
                     {
                           for (ResolveInfo res : list)
                           {
                                  final CropOption co = new CropOption();

                                  co.title      = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                                  co.icon              = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                                  co.appIntent= new Intent(intent);

                                  co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                                  cropOptions.add(co);
                           }

                           CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

                           AlertDialog.Builder builder = new AlertDialog.Builder(this);
                           builder.setTitle("Choose Crop App");
                            builder.setAdapter( adapter, new DialogInterface.OnClickListener()
                           {
                                  public void onClick( DialogInterface dialog, int item )
                                  {
                                         startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                                  }
                           });

                           builder.setOnCancelListener( new DialogInterface.OnCancelListener()
                           {
                                  @Override
                                  public void onCancel( DialogInterface dialog )
                                  {

                                         if (mImageCaptureUri != null )
                                         {
                                                getContentResolver().delete(mImageCaptureUri, null, null );
                                                mImageCaptureUri = null;
                                         }
                                  }
                           } );

                           AlertDialog alert = builder.create();

                           alert.show();
                     }
              }
       }
}


CropOptionAdapter:

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import java.util.ArrayList; 

public class CropOptionAdapter extends ArrayAdapter<CropOption>
{
       private ArrayList<CropOption> mOptions;
       private LayoutInflater mInflater;
      
       public CropOptionAdapter(Context context, ArrayList<CropOption> options)
       {
              super(context, R.layout.crop_selector, options);
             
              mOptions= options;        
              mInflater= LayoutInflater.from(context);
       }
      
       @Override
       public View getView(int position, View convertView, ViewGroup group)
       {
              if (convertView == null)
                     convertView = mInflater.inflate(R.layout.crop_selector, null);
             
              CropOption item = mOptions.get(position);
             
              if (item != null)
              {
                     ((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
                     ((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
                    
                     return convertView;
              }
             
              return null;
       }
}


CropOption:

import android.content.Intent;
import android.graphics.drawable.Drawable;

public class CropOption
{
       public CharSequence title;
       public Drawable icon;
       public Intent appIntent;
}


Download Source Code:

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)