October 25, 2013

Android TimePickerDialog

In this tutorial I want to explain about creating a  TimePickerDialog like,



In this I am having One Button and One EditText. On this example first it will load the current time to the EditText, While Tap on the button it will display the TimePickerDialog with the current time after tap on Done button TimePickerDialog it will update in the EditText.
 


activity_main.xml

This is a Layout of main page. This layout contain One Button and one EditText.



<LinearLayout 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:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Time" />

    <EditText
         android:id="@+id/EdtTxtTime"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:ems="10"
         android:focusableInTouchMode="false"
         android:inputType="text" />

</LinearLayout>



MainActivity.java
 
public class MainActivity extends Activity {
       Button BtnSelectTime;
   private EditText EdtTxtTime;
   private static Context context;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           context=MainActivity.this;
           EdtTxtTime = (EditText) findViewById(R.id.EdtTxtTime);

            String Time = getCurrentTime();
            if(Integer.parseInt(Time.substring(0, 2).toString())>12){
                Time = (Integer.parseInt(Time.substring(0, 2).toString())-12)+Time.substring(2);              
            }
            EdtTxtTime.setText(Time);
       
        BtnSelectTime = (Button) findViewById(R.id.BtnSelectTime);
        BtnSelectTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
              
                             Calendar mcurrentTime = Calendar.getInstance();
                              int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                              int minute = mcurrentTime.get(Calendar.MINUTE);
                              TimePickerDialog mTimePicker;
                              mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                                  @Override
                                  public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                                      EdtTxtTime.setText( selectedHour + ":" + selectedMinute);
                                  }
                              }, hour, minute, true);//Yes 24 hour time
                              mTimePicker.setTitle("Select Time");
                              mTimePicker.show();

            }
        });
       
    }

//Function to get current time
    public static String getCurrentTime() {
            String DATE_FORMAT_NOW = "HH:mm";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            return sdf.format(new Date());
     }
   
}

Android DatePickerDialog

In this tutorial I want to explain about creating a  DatePickerDialog like,



In this I am having One Button and One EditText. On this example first it will load the current date to the EditText, While Tap on the button it will display the DatePickerDialog after tap on Done button DatePickerDialog it will update in the EditText.
 


activity_main.xml

This is a Layout of main page. This layout contain One Button and one EditText.



<LinearLayout 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:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Date" />

    <EditText
         android:id="@+id/EdtTxtDate"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:ems="10"
         android:focusableInTouchMode="false"
         android:inputType="text" />

</LinearLayout>



MainActivity.java
 
public class MainActivity extends Activity {
       Button BtnSelectImage;
   private EditText EdtTxtDate;
   private static Context context;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           context=MainActivity.this;
           EdtTxtDate = (EditText) findViewById(R.id.EdtTxtDate);

            String Date = getCurrentDate();
            EdtTxtDate.setText(Date);
       
        BtnSelectDate = (Button) findViewById(R.id.BtnSelectDate);
        BtnSelectDate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
             
                              int monthOfYear, dayOfMonth, year;
                              String DateString = EdtTxtDate.getText().toString().trim();
                              if(DateString.equalsIgnoreCase("")){
                                  DateString = getCurrentDate();
                              }
                              dayOfMonth = Integer.parseInt(DateString.substring(0,DateString.indexOf("-")));
                              monthOfYear =    Integer.parseInt(DateString.substring(DateString.indexOf("-")+1,DateString.lastIndexOf("-")))-1;
                              year = Integer.parseInt(DateString.substring(DateString.lastIndexOf("-")+1));
                             
                              DatePickerDialog datePickerDialog;
                              datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                               
                                @Override
                                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                        int dayOfMonth) {
                                    monthOfYear = monthOfYear+1;
                                    String Month="", Day ="";
                                    if(monthOfYear<10)
                                        Month = "0"+ monthOfYear;
                                    else
                                        Month = monthOfYear+"";
   
                                    if(dayOfMonth<10)
                                        Day = "0"+ dayOfMonth;
                                    else
                                        Day = dayOfMonth+"";
                                   
                                    String AlertDate =  Day+"-"+Month+"-"+year;
                                    EdtTxtDate.setText(AlertDate);
                                }
                            }, year, monthOfYear, dayOfMonth);
                              datePickerDialog.setTitle("Select Date");
                              datePickerDialog.show();

            }
        });
       
    }

//Function to get current date
     public static String getCurrentDate() {
            String DATE_FORMAT_NOW = "dd-MM-yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            return sdf.format(new Date());
      }
   
}

October 24, 2013

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout 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:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        android:id="@+id/ImgPhoto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

MainActivity.java


import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
   
    private static final int CAMERA_PIC_REQUEST = 22;

    Uri cameraUri;
   
    Button BtnSelectImage;
    private ImageView ImgPhoto;
    private String Camerapath ;
   
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        ImgPhoto = (ImageView) findViewById(R.id.ImgPhoto);
       
        BtnSelectImage = (Button) findViewById(R.id.BtnSelectImg);
        BtnSelectImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                try {
                       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                       startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
                }
            }
        });
       
    }
   
   
    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        try {
            switch (requestCode) {
           case CAMERA_PIC_REQUEST:
                if (resultCode == RESULT_OK) {
                    try {
                          Bitmap photo = (Bitmap) data.getExtras().get("data");
                         
                          ImgPhoto.setImageBitmap(photo);   
                         
                    } catch (Exception e) {
                        Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
                    }
                }
                break;
              default:
                break;
            }
        } catch (Exception e) {
        }
    }
    
   
}

custom listview with separate headers




Here is the  custom listview with separate headers Blog. This Blog will help you to create seperate header for a list link,

October 10, 2013

Create Android Application

                                          After installing Eclipse and all the components required for android development (You can use This Tutorial for to knew How to start Android Development). Now you can create your first Hello World Android Application.

Create Hello World Application.

You can create your Android application through two ways.

1. In Eclipse File menu -> New -> Android Application Project

2.  Right click on your Package Explorer -> New ->Android Application Project

Enter the Application Name, Project Name, Package Name, Minimum Required SDK, Target SDK, Compile with, Theme for the new android application on the Window.


Application name is the name of the application
Project name is the name of the project directory which is displayed on the project explorer and also the folder name of your application created in your android workspace.
Package name is the namespace of your application. which is unique for all the packages installed on your android system.
Minimum Required SDK is the lowest version of android which supports your application.
Target SDK is the highest version of android which supports your application.

After Filling all the values press Next Button

Next screen is Configure project screen. 
      You just skip it by press Next Button.

Next screen is Configure Launcher icon Screen.

     Launcher icon is the icon of your application. Press Next Button

Next screen is Create Activity screen.
 
     You can create the type of activity screen on here. Press Next Button

Next screen is Activity Creation screen.

    On this screen you can enter the name of the Activity also the Layout name.Then Press Finish Button