Custom Calendar in Android

Hello dear friends,  Today I am going to share very Important code for custom calender in Android. Actually android not provide any calender view so its a very big draw back for developer but we can create a custom calender using grid-view.

Important step is given below-

1- Create new project e.g.-CustomCalenderAndroid

2- Create an activity MyCalendarActivity.java

3- Create layout my_calendar_view.xml

4- Create another xml file screen_gridcell.xml

5- Add styles_calendar_events.xml in values folder

6- Add Images for calendar in drawable folder

1-Print Screen:

2

 

MyCalendarActivity.java

package com.examples;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

@TargetApi(3)
public class MyCalendarActivity extends Activity implements OnClickListener {
 private static final String tag = "MyCalendarActivity";

 private TextView currentMonth;
 private Button selectedDayMonthYearButton;
 private ImageView prevMonth;
 private ImageView nextMonth;
 private GridView calendarView;
 private GridCellAdapter adapter;
 private Calendar _calendar;
 @SuppressLint("NewApi")
 private int month, year;
 @SuppressWarnings("unused")
 @SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
 private final DateFormat dateFormatter = new DateFormat();
 private static final String dateTemplate = "MMMM yyyy";

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.my_calendar_view);

  _calendar = Calendar.getInstance(Locale.getDefault());
  month = _calendar.get(Calendar.MONTH) + 1;
  year = _calendar.get(Calendar.YEAR);
  Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
    + year);

  selectedDayMonthYearButton = (Button) this
    .findViewById(R.id.selectedDayMonthYear);
  selectedDayMonthYearButton.setText("Selected: ");

  prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
  prevMonth.setOnClickListener(this);

  currentMonth = (TextView) this.findViewById(R.id.currentMonth);
  currentMonth.setText(DateFormat.format(dateTemplate,
    _calendar.getTime()));

  nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
  nextMonth.setOnClickListener(this);

  calendarView = (GridView) this.findViewById(R.id.calendar);

  // Initialised
  adapter = new GridCellAdapter(getApplicationContext(),
    R.id.calendar_day_gridcell, month, year);
  adapter.notifyDataSetChanged();
  calendarView.setAdapter(adapter);
 }

 /**
  * 
  * @param month
  * @param year
  */
 private void setGridCellAdapterToDate(int month, int year) {
  adapter = new GridCellAdapter(getApplicationContext(),
    R.id.calendar_day_gridcell, month, year);
  _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
  currentMonth.setText(DateFormat.format(dateTemplate,
    _calendar.getTime()));
  adapter.notifyDataSetChanged();
  calendarView.setAdapter(adapter);
 }

 @Override
 public void onClick(View v) {
  if (v == prevMonth) {
   if (month <= 1) {
    month = 12;
    year--;
   } else {
    month--;
   }
   Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
     + month + " Year: " + year);
   setGridCellAdapterToDate(month, year);
  }
  if (v == nextMonth) {
   if (month > 11) {
    month = 1;
    year++;
   } else {
    month++;
   }
   Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
     + month + " Year: " + year);
   setGridCellAdapterToDate(month, year);
  }

 }

 @Override
 public void onDestroy() {
  Log.d(tag, "Destroying View ...");
  super.onDestroy();
 }

 // Inner Class
 public class GridCellAdapter extends BaseAdapter implements OnClickListener {
  private static final String tag = "GridCellAdapter";
  private final Context _context;

  private final List<String> list;
  private static final int DAY_OFFSET = 1;
  private final String[] weekdays = new String[] { "Sun", "Mon", "Tue",
    "Wed", "Thu", "Fri", "Sat" };
  private final String[] months = { "January", "February", "March",
    "April", "May", "June", "July", "August", "September",
    "October", "November", "December" };
  private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
    31, 30, 31 };
  private int daysInMonth;
  private int currentDayOfMonth;
  private int currentWeekDay;
  private Button gridcell;
  private TextView num_events_per_day;
  private final HashMap<String, Integer> eventsPerMonthMap;
  private final SimpleDateFormat dateFormatter = new SimpleDateFormat(
    "dd-MMM-yyyy");

  // Days in Current Month
  public GridCellAdapter(Context context, int textViewResourceId,
    int month, int year) {
   super();
   this._context = context;
   this.list = new ArrayList<String>();
   Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
     + "Year: " + year);
   Calendar calendar = Calendar.getInstance();
   setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
   setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
   Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
   Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
   Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());

   // Print Month
   printMonth(month, year);

   // Find Number of Events
   eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
  }

  private String getMonthAsString(int i) {
   return months[i];
  }

  private String getWeekDayAsString(int i) {
   return weekdays[i];
  }

  private int getNumberOfDaysOfMonth(int i) {
   return daysOfMonth[i];
  }

  public String getItem(int position) {
   return list.get(position);
  }

  @Override
  public int getCount() {
   return list.size();
  }

  /**
   * Prints Month
   * 
   * @param mm
   * @param yy
   */
  private void printMonth(int mm, int yy) {
   Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
   int trailingSpaces = 0;
   int daysInPrevMonth = 0;
   int prevMonth = 0;
   int prevYear = 0;
   int nextMonth = 0;
   int nextYear = 0;

   int currentMonth = mm - 1;
   String currentMonthName = getMonthAsString(currentMonth);
   daysInMonth = getNumberOfDaysOfMonth(currentMonth);

   Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
     + daysInMonth + " days.");

   GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
   Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

   if (currentMonth == 11) {
    prevMonth = currentMonth - 1;
    daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
    nextMonth = 0;
    prevYear = yy;
    nextYear = yy + 1;
    Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
      + prevMonth + " NextMonth: " + nextMonth
      + " NextYear: " + nextYear);
   } else if (currentMonth == 0) {
    prevMonth = 11;
    prevYear = yy - 1;
    nextYear = yy;
    daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
    nextMonth = 1;
    Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
      + prevMonth + " NextMonth: " + nextMonth
      + " NextYear: " + nextYear);
   } else {
    prevMonth = currentMonth - 1;
    nextMonth = currentMonth + 1;
    nextYear = yy;
    prevYear = yy;
    daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
    Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
      + prevMonth + " NextMonth: " + nextMonth
      + " NextYear: " + nextYear);
   }

   int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
   trailingSpaces = currentWeekDay;

   Log.d(tag, "Week Day:" + currentWeekDay + " is "
     + getWeekDayAsString(currentWeekDay));
   Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
   Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

   if (cal.isLeapYear(cal.get(Calendar.YEAR)))
    if (mm == 2)
     ++daysInMonth;
    else if (mm == 3)
     ++daysInPrevMonth;

   // Trailing Month days
   for (int i = 0; i < trailingSpaces; i++) {
    Log.d(tag,
      "PREV MONTH:= "
        + prevMonth
        + " => "
        + getMonthAsString(prevMonth)
        + " "
        + String.valueOf((daysInPrevMonth
          - trailingSpaces + DAY_OFFSET)
          + i));
    list.add(String
      .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
        + i)
      + "-GREY"
      + "-"
      + getMonthAsString(prevMonth)
      + "-"
      + prevYear);
   }

   // Current Month Days
   for (int i = 1; i <= daysInMonth; i++) {
    Log.d(currentMonthName, String.valueOf(i) + " "
      + getMonthAsString(currentMonth) + " " + yy);
    if (i == getCurrentDayOfMonth()) {
     list.add(String.valueOf(i) + "-BLUE" + "-"
       + getMonthAsString(currentMonth) + "-" + yy);
    } else {
     list.add(String.valueOf(i) + "-WHITE" + "-"
       + getMonthAsString(currentMonth) + "-" + yy);
    }
   }

   // Leading Month days
   for (int i = 0; i < list.size() % 7; i++) {
    Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
    list.add(String.valueOf(i + 1) + "-GREY" + "-"
      + getMonthAsString(nextMonth) + "-" + nextYear);
   }
  }

  /**
   * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve
   * ALL entries from a SQLite database for that month. Iterate over the
   * List of All entries, and get the dateCreated, which is converted into
   * day.
   * 
   * @param year
   * @param month
   * @return
   */
  private HashMap<String, Integer> findNumberOfEventsPerMonth(int year,
    int month) {
   HashMap<String, Integer> map = new HashMap<String, Integer>();

   return map;
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   View row = convertView;
   if (row == null) {
    LayoutInflater inflater = (LayoutInflater) _context
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.screen_gridcell, parent, false);
   }

   // Get a reference to the Day gridcell
   gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
   gridcell.setOnClickListener(this);

   // ACCOUNT FOR SPACING

   Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
   String[] day_color = list.get(position).split("-");
   String theday = day_color[0];
   String themonth = day_color[2];
   String theyear = day_color[3];
   if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
    if (eventsPerMonthMap.containsKey(theday)) {
     num_events_per_day = (TextView) row
       .findViewById(R.id.num_events_per_day);
     Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
     num_events_per_day.setText(numEvents.toString());
    }
   }

   // Set the Day GridCell
   gridcell.setText(theday);
   gridcell.setTag(theday + "-" + themonth + "-" + theyear);
   Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-"
     + theyear);

   if (day_color[1].equals("GREY")) {
    gridcell.setTextColor(getResources()
      .getColor(R.color.lightgray));
   }
   if (day_color[1].equals("WHITE")) {
    gridcell.setTextColor(getResources().getColor(
      R.color.lightgray02));
   }
   if (day_color[1].equals("BLUE")) {
    gridcell.setTextColor(getResources().getColor(R.color.orrange));
   }
   return row;
  }

  @Override
  public void onClick(View view) {
   String date_month_year = (String) view.getTag();
   selectedDayMonthYearButton.setText("Selected: " + date_month_year);
   Log.e("Selected date", date_month_year);
   try {
    Date parsedDate = dateFormatter.parse(date_month_year);
    Log.d(tag, "Parsed Date: " + parsedDate.toString());

   } catch (ParseException e) {
    e.printStackTrace();
   }
  }

  public int getCurrentDayOfMonth() {
   return currentDayOfMonth;
  }

  private void setCurrentDayOfMonth(int currentDayOfMonth) {
   this.currentDayOfMonth = currentDayOfMonth;
  }

  public void setCurrentWeekDay(int currentWeekDay) {
   this.currentWeekDay = currentWeekDay;
  }

  public int getCurrentWeekDay() {
   return currentWeekDay;
  }
 }
}

my_calendar_view.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:background="@color/lightgray"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/buttonlayout"
        android:layout_width="fill_parent"
        android:layout_height="60sp"
        android:background="@drawable/topbar"
        android:gravity="left|top"
        android:height="60sp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/settings"
            android:layout_width="54sp"
            android:layout_height="60sp"
            android:background="@drawable/meenu" />

        <ImageView
            android:id="@+id/prevMonth"
            android:layout_width="20sp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="10sp"
            android:src="@drawable/calendar_left_arrow_selector" >
        </ImageView>

        <TextView
            android:id="@+id/currentMonth"
            android:layout_width="fill_parent"
            android:layout_height="60sp"
            android:layout_weight="0.6"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF" >
        </TextView>

        <ImageView
            android:id="@+id/nextMonth"
            android:layout_width="20sp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_marginRight="10sp"
            android:src="@drawable/calendar_right_arrow_selector" >
        </ImageView>

        <Button
            android:id="@+id/addEvent"
            android:layout_width="54sp"
            android:layout_height="60sp"
            android:background="@drawable/plus" />
    </LinearLayout>

    <Button
        android:id="@+id/selectedDayMonthYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/calendar_top_header"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" >
    </Button>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <ImageView
            android:id="@+id/calendarheader"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/calendar_days" >
        </ImageView>
    </LinearLayout>

    <GridView
        android:id="@+id/calendar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="7" >
    </GridView>

</LinearLayout>

screen_gridcell.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/calendar_button_selector"
    android:orientation="vertical" >

    <Button
        android:id="@+id/calendar_day_gridcell"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/calendar_button_selector"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" >
    </Button>

    <TextView
        android:id="@+id/num_events_per_day"
        style="@style/calendar_event_style"
        android:layout_width="10dip"
        android:layout_height="10dip"
        android:layout_gravity="right" >
    </TextView>

</RelativeLayout>

If you are looking for zip project please comment ...
Hope it will help you...

Picker Views Example in Android

This example will show you how you can combine and handle two picker views in a layout.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it PickerViewsExample.

2.) Write following into main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:text="@string/date_text"
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:typeface="sans"></TextView>
<Button android:text="@string/date_button"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></Button>
    
<TextView android:text="@string/time_text"
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:typeface="sans"></TextView>
<Button android:text="@string/time_button"
    android:id="@+id/Button02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></Button>
</LinearLayout>
3.) Create and write following into layout/datepickerlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<DatePicker
    android:id="@+id/DatePicker01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></DatePicker>
</LinearLayout>

4.) Run for output.

Steps:

1.) Create a project named PickerViewsExample and set the information as stated in the image.

Build Target: Android 4.2
Application Name: PickerViewsExample
Package Name: com.example.PickerViewsExample
Activity Name: PickerViewsExample
Min SDK Version: 4.2

2.) Open PickerViewsExample.java file and write following code there:

package com.example.pickerviewexample;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class PickerViewExample extends Activity {
    
    static final int DATE_DIALOG_ID = 1;
    static final int TIME_DIALOG_ID = 2;
    private TextView dateDisplay;
    private Button pickDate;
    private int year, month, day;
    private TextView timeDisplay;
    private Button pickTime;
    private int hours, min;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        dateDisplay = (TextView)findViewById(R.id.TextView01);
        pickDate = (Button)findViewById(R.id.Button01);
        
        pickDate.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
            
        });
        
        final Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);
        month = cal.get(Calendar.MONTH);
        day = cal.get(Calendar.DAY_OF_MONTH);
        
        updateDate();
        
        timeDisplay = (TextView)findViewById(R.id.TextView02);
        pickTime = (Button)findViewById(R.id.Button02);
        
        pickTime.setOnClickListener( new OnClickListener () {
            @Override
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
                
            }
            
        });
        
        hours = cal.get(Calendar.HOUR);
        min = cal.get(Calendar.MINUTE);
        
        updateTime();
    }
    private void updateTime() {
        timeDisplay.setText(new StringBuilder().append(hours).append(':')
                .append(min));
        
    }
    private void updateDate() {
        dateDisplay.setText(new StringBuilder().append(day).append('-')
                .append(month + 1).append('-').append(year));
        
    }
    
    private DatePickerDialog.OnDateSetListener dateListener =
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int yr, int monthOfYear,
                    int dayOfMonth) {
                year = yr;
                month = monthOfYear;
                day = dayOfMonth;
                updateDate();
            }
    };
    
    private TimePickerDialog.OnTimeSetListener timeListener =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                hours = hourOfDay;
                min = minute;
                updateTime();
            }
        
    };
    protected Dialog onCreateDialog(int id){
        switch(id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, dateListener, year, month, day);
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this, timeListener, hours, min, false);
        }
        return null;
        
    }
}

3.) Compile and build the project.

Output

Image

Image

 

If you are looking for zip project please comment …
Hope it will help you…
Thanks…

Remote Service Client Example

This example will show you how to handle and invoke remote service from client.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it RemoteServiceClientExample.

2.) Write following into main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/welcome"
    />
<Button android:id="@+id/startButton"
    android:text="@string/startButton"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    </Button>
<Button android:id="@+id/stopButton"
    android:text="@string/stopButton"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    </Button>
<Button android:id="@+id/bindButton"
    android:text="@string/bindButton"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    </Button>
<Button android:id="@+id/releaseButton"
    android:text="@string/releaseButton"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    </Button>
<Button android:id="@+id/invokeButton"
    android:text="@string/invokeButton"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    </Button>
<TextView android:id="@+id/notApplicable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/notApplicable" /> 
<TextView android:id="@+id/serviceStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/serviceStatus" />
</LinearLayout>

3.) Import com.example.remoteserviceexample into src. (check another blogpost RemoteServiceExample for this)
4.) Run for output.

Steps:

1.) Create a project named RemoteServiceClientExample and set the information as stated in the image.

Build Target: Android 4.2
Application Name: RemoteServiceClientExample
Package Name: com. RemoteServiceClientExample
Activity Name: RemoteServiceClientExample
Min SDK Version: 4.2

2.) Open RemoteServiceClientExample.java file and write following code there:

package com.example.remoteserviceclient;
import com.example.remoteserviceexample.IMyRemoteService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class RemoteServiceClientExample extends Activity {
    private IMyRemoteService remoteService;
    private boolean started = false;
    private RemoteServiceConnection conn = null;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button start = (Button)findViewById(R.id.startButton);
        Button stop = (Button)findViewById(R.id.stopButton);
        Button bind = (Button)findViewById(R.id.bindButton);
        Button release = (Button)findViewById(R.id.releaseButton);
        Button invoke = (Button)findViewById(R.id.invokeButton);
        
        start.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                startService();
            }
        });
        
        stop.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                stopService();
            }
        });      
        
        bind.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                bindService();
            }
        }); 
        
        release.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                releaseService();
            }
        });         
        
        invoke.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                invokeService();
            }
        });         
   
    }
    
    private void startService(){
            if (started) {
                Toast.makeText(RemoteServiceClientExample.this, "Service already started", Toast.LENGTH_SHORT).show();
            } else {
                Intent i = new Intent();
                i.setClassName("com.example.remoteserviceexample", "com.example.remoteserviceexample.RemoteService");
                startService(i);
                started = true;
                updateServiceStatus();
                Log.d( getClass().getSimpleName(), "startService()" );
            }
            
    }
       
    private void stopService() {
            if (!started) {
                Toast.makeText(RemoteServiceClientExample.this, "Service not yet started", Toast.LENGTH_SHORT).show();
            } else {
                Intent i = new Intent();
                i.setClassName("com.example.remoteserviceexample", "com.example.remoteserviceexample.RemoteService");
                stopService(i);
                started = false;
                updateServiceStatus();
                Log.d( getClass().getSimpleName(), "stopService()" );
            }
    }
      
    private void bindService() {
                if(conn == null) {
                    conn = new RemoteServiceConnection();
                    Intent i = new Intent();
                    i.setClassName("com.example.remoteserviceexample", "com.example.remoteserviceexample.RemoteService");
                    bindService(i, conn, Context.BIND_AUTO_CREATE);
                    updateServiceStatus();
                    Log.d( getClass().getSimpleName(), "bindService()" );
                } else {
                    Toast.makeText(RemoteServiceClientExample.this, "Cannot bind - service already bound", Toast.LENGTH_SHORT).show();
                }
    }
        
    private void releaseService() {
                if(conn != null) {
                    unbindService(conn);
                    conn = null;
                    updateServiceStatus();
                    Log.d( getClass().getSimpleName(), "releaseService()" );
                } else {
                    Toast.makeText(RemoteServiceClientExample.this, "Cannot unbind - service not bound", Toast.LENGTH_SHORT).show();
                }
    }
        
    private void invokeService() {
                if(conn == null) {
                    Toast.makeText(RemoteServiceClientExample.this, "Cannot invoke - service not bound", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        int counter = remoteService.getCounter();
                          TextView t = (TextView)findViewById(R.id.notApplicable);
                          t.setText( "Counter value: "+Integer.toString( counter ) );
                          Log.d( getClass().getSimpleName(), "invokeService()" );
                    } catch (RemoteException re) {
                        Log.e( getClass().getSimpleName(), "RemoteException" );
                    }
                }
        }              
      
      class RemoteServiceConnection implements ServiceConnection {
          public void onServiceConnected(ComponentName className,
            IBinder boundService ) {
            remoteService = IMyRemoteService.Stub.asInterface((IBinder)boundService);
            Log.d( getClass().getSimpleName(), "onServiceConnected()" );
          }
          public void onServiceDisconnected(ComponentName className) {
            remoteService = null;
           updateServiceStatus();
           Log.d( getClass().getSimpleName(), "onServiceDisconnected" );
          }
      };
      
      private void updateServiceStatus() {
          String bindStatus = conn == null ? "unbound" : "bound";
          String startStatus = started ? "started" : "not started";
          String statusText = "Service status: "+
                                bindStatus+
                                ","+
                                startStatus;
          TextView t = (TextView)findViewById( R.id.serviceStatus);
          t.setText( statusText );   
        }
      
      protected void onDestroy() {
          super.onDestroy();
          releaseService();
          Log.d( getClass().getSimpleName(), "onDestroy()" );
      }
 }

5.) Compile and build the project.

Output

Image

 

If you are looking for zip project please comment …
Hope it will help you…
Thanks…

Numberpicker in listview

I have facing one problem with numberpicker in listview.
I have a custom numberpicker in my listview. I add quantity in numberpicker for first item in my list but when I scroll through the list then without adding the same quantity appears in random items numberpicker. How to solve that?
if you have any suggestion plz give me.
my email id -ashutosh3ojha2007@gmail.com
device-2013-05-10-100357 device-2013-05-10-101105 device-2013-05-10-101513