Showing posts with label Application. Show all posts
Showing posts with label Application. Show all posts

Saturday, 18 October 2014

Capturing image in Android

Hi friends let us learn how to create a camera based application in android.

Basics for capturing a image using existing camera in our mobile

You will use MediaStore.ACTION_IMAGE_CAPTURE to launch an existing camera application installed on your phone. Its syntax is given below
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Apart from the above , there are other availaible Intents provided by MediaStore. They are listed as follows
Sr.NoIntent type and description
1ACTION_IMAGE_CAPTURE_SECURE
It returns the image captured from the camera , when the device is secured
2ACTION_VIDEO_CAPTURE
It calls the existing video application in android to capture video
3EXTRA_SCREEN_ORIENTATION
It is used to set the orientation of the screen to vertical or landscape
4EXTRA_FULL_SCREEN
It is used to control the user interface of the ViewImage
5INTENT_ACTION_VIDEO_CAMERA
This intent is used to launch the camea in the video mode
6EXTRA_SIZE_LIMIT
It is used to specify the size limit of video or image capture size
Now you will use the function startActivityForResult to launch this activity and wait for its result. Its syntax is given below
startActivityForResult(intent,0)
This method has been defined in the activity class. We are calling it from main activity. There are methods defined in the activity class that does the same job , but used when you are not calling from the activity but from somewhere else. They are listed below

Sr.NoActivity function description
1startActivityForResultIntentintent,intrequestCode,Bundleoptions
It starts an activity , but can take extra bundle of options with it
2startActivityFromChildActivitychild,Intentintent,intrequestCode
It launch the activity when your activity is child of any other activity
3startActivityFromChildActivitychild,Intentintent,intrequestCode,Bundleoptions
It work same as above , but it can take extra values in the shape of bundle with it
4startActivityFromFragmentFragmentfragment,Intentintent,intrequestCode
It launches activity from the fragment you are currently inside
5startActivityFromFragmentFragmentfragment,Intentintent,intrequestCode,Bundleoptions
It not only launches the activity from the fragment , but can take extra values with it
No matter which function you used to launch the activity , they all return the result. The result can be obtained by overriding the function onActivityResult.

Example

Here is an example that shows how to launch the exisitng camera application to capture an image and display the result in the form of bitmap
To experiment with this example , you need to run this on an actual device on which camera is supported.
StepsDescription
1You will use Eclipse IDE to create an Android application and name it as Camera under a package com.example.camera. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/MainActivity.java file to add intent code to launch the activity and result method to recieve the output.
3Modify layout XML file res/layout/activity_main.xml add any GUI component if required. Here we add only imageView and a textView.
4Modify res/values/strings.xml to define required constant values
5Run the application and choose a running android device and install the application on it and verify the results.
Following is the content of the modified main activity file src/com.example.camera/MainActivity.java.
package com.example.camera;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView imgFavorite;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      imgFavorite = (ImageView)findViewById(R.id.imageView1);
      imgFavorite.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            open();
         }
      });
   }
   public void open(){
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(intent, 0);
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);
      Bitmap bp = (Bitmap) data.getExtras().get("data");
      imgFavorite.setImageBitmap(bp);
   }
   @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;
   }
}
Following will be the content of res/layout/activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"> 

   <ImageView
   android:id="@+id/imageView1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_marginLeft="34dp"
   android:layout_marginTop="36dp"
   android:contentDescription="@string/hello_world"
   android:src="@drawable/ic_launcher" />

   <TextView
   android:id="@+id/textView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_alignRight="@+id/imageView1"
   android:text="@string/tap"
   android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Following will be the content of res/values/strings.xml to define one new constants
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Camera</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="tap">Tap the image to open the camera!!</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.camera"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
         <activity
            android:name="com.example.camera.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>
Let's try to run your Camera application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar.

Friday, 9 May 2014

Toast - Special Feature in Android

xample,Android Toast,Toast Image,Toast notification,android examples

Hi Guys already we have seen a little introduction about Android.Lets move to some of the features provided by Android. Firstly we are going to see about a concept of feedback or notification alike mechanism Toast.

A Toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, After sending a message it shows message send . Toasts automatically disappear after a timeout.

General procedure  to create a toast,
First, instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context, your message, and the duration for the toast. It returns a properly  Toast object. You can display it by toast followed by show(),as shown in the following example:


 


you may also write it as,



 In our next post we can see about how to create a custom toast alert etc.

Toast,toast alert,toast example,Android Toast,Toast Image,Toast notification,android examples

 
 

Wednesday, 23 April 2014

To create a First Android App



To create a First Android App
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse


Step 1: Open Eclipse Application in your Android SDK bundle

Android,Tikshniyas,SimpleApp,Basic features,Ecllipse
Step 2: Select a workspace where ADT stores your projects
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse

Step 3: Choose New ----> Android Application project

Android,Tikshniyas,SimpleApp,Basic features,Ecllipse
Step 4:  A popup window creates, in which you describe your Application name, Project Name, Package Name(You can specify any common name for your App collection), and other details etc and press next.
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse

Step 5: Configure your project similarly and press next.
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse


Step 6: Configure your launcher icon which you can also able to change later. You can set your custom image by choosing a image by browsing
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse

Step 7: select the mode of Activity for your App

Android,Tikshniyas,SimpleApp,Basic features,Ecllipse
Step 8: Set your Activity name and choose your Navigation type
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse

Step 9: This is the how the Application looks. The red color marked region is where you need to design

Step 10: Below Marked region is the Tool palette which consist of tools which you need to drag and drop in your design region
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse

Step 11: Run the emulator which acts as a virtual device for android
Android,Tikshniyas,SimpleApp,Basic features,Ecllipse

Step 12: In case of any advanced program you may include your coding here

Android,Tikshniyas,SimpleApp,Basic features,Ecllipse
Step 13: Finally the output of this first simple hello world is displayed in emulator as,

Android,Tikshniyas,SimpleApp,Basic features,Ecllipse