Category Archives: ANDROID

Google Maps in iOS – Search your location

Here is a simple tutorial that helps you to start with Google Maps for iOS.

Google Maps iOS

Make sure that you have a textbox and a button and a view just below them for placing the Google Map in your interface builder.
There is no control for adding Google Maps, the Map is added dynamically in the code.

Here is the Google Documentation to start with Google Maps.
https://developers.google.com/maps/documentation/ios/start

To start with Google Maps you need the following

The Google Maps API key (this key is used in the Code for initializing maps, Make sure that you API and your bundle identifier matches when you register for maps with Google, otherwise the map will not load.)

If you have downloaded the Google Maps SDK, then you have to integrate it in your project.

Follow the below steps.

Launch Xcode and either open an existing project, or create a new project.
If you’re new to iOS, create a Single View Application, and ensure that Use Automatic Reference Counting is on.
Drag the GoogleMaps.framework bundle to your project. When prompted, select Copy items if needed.
Right-click GoogleMaps.framework in your project, and select Show In Finder.
Drag the GoogleMaps.bundle from the Resources folder to your project. When prompted, ensure Copy items into destination group’s folder is not selected.
Select your project from the Project Navigator, and choose your application’s target.
Open the Build Phases tab, and within Link Binary with Libraries, add the following frameworks:
AVFoundation.framework
CoreData.framework
CoreLocation.framework
CoreText.framework
GLKit.framework
ImageIO.framework
libc++.dylib
libicucore.dylib
libz.dylib
OpenGLES.framework
QuartzCore.framework
SystemConfiguration.framework
Choose your project, rather than a specific target, and open the Build Settings tab.
In the Other Linker Flags section, add -ObjC. If these settings are not visible, change the filter in the Build Settings bar from Basic to All.
Finally, add your API key to your AppDelegate.
#import
Add the following to your application:didFinishLaunchingWithOptions: method, replacing API_KEY with your API key.

[GMSServices provideAPIKey:@”API_KEY”];

After that Clean and build your project.

Please don’t forget to add “-ObjC” in Other Linked Flags, otherwise it will result in an error.

Now Lets go to our Coding part.

AppDelegate.h

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end


AppDelegate.m

MAKE SURE YOU CHANGE THE API KEY WITH YOURS IN THE BELOW CODE.


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"my bundle id is: %@", [[NSBundle mainBundle] bundleIdentifier]);
    [GMSServices provideAPIKey:@"YOUR API KEY"];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

This is your main view controller that contains the maps.
ViewController.h


#import <UIKit/UIKit.h>
#import "GetSearchData.h"
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<ServiceListener, GMSMapViewDelegate, CLLocationManagerDelegate>
{
      CLLocationManager *locationManager;
}
@property (weak, nonatomic) IBOutlet UITextField *searchTxt;
@property (weak, nonatomic) IBOutlet UIView *mapParentView;

- (IBAction)search:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController (){
    GMSMapView *mapView_;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
        [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse
        //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways
        ) {
        // Will open an confirm dialog to get user's approval
        [locationManager requestWhenInUseAuthorization];
        //[_locationManager requestAlwaysAuthorization];
    } else {
       
    }
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    
    [locationManager startUpdatingLocation]; //Will update location immediately
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

// Wait for location callbacks
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"%@", [locations lastObject]);
    
    CLLocation *currentLocation = [locations lastObject];
    
    [locationManager stopUpdatingLocation];
    
    [mapView_ clear];
    
    [mapView_ animateToLocation:CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude)];
    
     [self setMarkerWithLatitiude:currentLocation.coordinate.latitude Longitude:currentLocation.coordinate.longitude andTitle:self.searchTxt.text];
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;
    
    if (currentLocation != nil) {
        
        [mapView_ clear];
        
        [mapView_ animateToLocation:CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude)];
        
        
        [self setMarkerWithLatitiude:currentLocation.coordinate.latitude Longitude:currentLocation.coordinate.longitude andTitle:self.searchTxt.text];
       
    }
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

#pragma mark - mapview events
-(BOOL) mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{
    NSLog(@"%@", marker.description);
    //    show info window
    [mapView_ setSelectedMarker:marker];
    return YES;
}

- (void)viewDidAppear:(BOOL)animated{
    
    CLLocationCoordinate2D myLocation = mapView_.myLocation.coordinate;
    
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.latitude
                                                            longitude:myLocation.longitude
                                                                 zoom:6];
    mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.mapParentView.frame.size.width, self.mapParentView.frame.size.height) camera:camera];
    mapView_.myLocationEnabled = YES;
    [self.mapParentView  addSubview:mapView_];
    
    [self setMarkerWithLatitiude:myLocation.latitude Longitude:myLocation.longitude andTitle:self.searchTxt.text];
    
    mapView_.delegate = self;
    
    self.mapParentView.backgroundColor = [UIColor whiteColor];
}


-(void) onSuccess :  (NSMutableArray *) jsonArray
{
    
    NSDictionary *results = [jsonArray valueForKey:@"results"];
    NSDictionary *geometry = [results valueForKey:@"geometry"];
    
    NSArray *location = [geometry valueForKey:@"location"];
    NSDictionary *vals = [location objectAtIndex:0];
    float lat = [[vals objectForKey:@"lat"] floatValue];
    float lng = [[vals objectForKey:@"lng"] floatValue];

    NSLog(@"Lat %@",[vals objectForKey:@"lat"]);
    NSLog(@"lng %@",[vals objectForKey:@"lng"]);
    
    [mapView_ animateToLocation:CLLocationCoordinate2DMake(lat, lng)];
    
    // Creates a marker in the center of the map.
    [self setMarkerWithLatitiude:lat Longitude:lng andTitle:self.searchTxt.text];
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    
}

-(void) setMarkerWithLatitiude : (float) lat Longitude : (float) lng andTitle : (NSString *) title
{
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(lat, lng);
    marker.title =  title;
    marker.snippet =  title;
    marker.map = mapView_;
}

- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker {
    //Your code goes here
}

-(void) onError : (NSString *) message
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (IBAction)search:(id)sender {
   
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    
    [mapView_ clear];
    NSString *searchStr = [self.searchTxt.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if(searchStr.length > 0){
        [self.searchTxt resignFirstResponder];
        GetSearchData *gsd = [GetSearchData new];
        [gsd setServiceListener:self];
        [gsd getCoordinates : self.searchTxt.text];
    }
}
@end


Below is the code that searches for location in the Google Maps and return the latitude and longitude.

GetSearchData.h

#import <Foundation/Foundation.h>

// Delegate

@protocol ServiceListener<NSObject>

@optional

-(void) onSuccess :  (NSMutableArray *)jsonArray;
-(void) onError : (NSString *) message;

@end

@interface GetSearchData : NSObject


@property (nonatomic, retain) id<ServiceListener> listener;
@property (nonatomic, retain) NSDictionary *searchDic;

-(void) setServiceListener : (id <ServiceListener>) listener;
-(void) getCoordinates : (NSString *) searchLocation;

@end

GetSearchData.m

#import "GetSearchData.h"

@implementation GetSearchData

@synthesize listener, searchDic;

-(void) getCoordinates : (NSString *) searchLocation
{
    NSMutableURLRequest *request = [NSMutableURLRequest new];
    NSString *url = [@"https://maps.googleapis.com/maps/api/geocode/json?address=" stringByAppendingString:searchLocation];
    
    request.timeoutInterval = 100.0;
    [request setURL:[NSURL URLWithString:url]];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    // Loads the data for a URL request and executes a handler block on an operation queue
    // when the request completes or fails.
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
       completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error){
           
           if(nil != data)
           {

               NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data
                                                                    options:NSJSONReadingMutableContainers
                                                                      error:&error];
               NSLog(@"loans: %@", jsonArray);
               
               dispatch_async(dispatch_get_main_queue(), ^{
                  [self.listener onSuccess:jsonArray];
               });
               
           }
           if (error) {
               NSLog(@"Response %@",error.localizedDescription);
               dispatch_async(dispatch_get_main_queue(), ^{
                   [self.listener onError:error.localizedDescription];
               });
           }
       }];

}

-(NSString *) getValueWithKey :(NSString *) key fromDictionary:(NSDictionary *) dict
{    
    return [[dict objectForKey:key] objectForKey:@"text"];
}

//This function will set the delegate
-(void) setServiceListener : (id <ServiceListener>) theListener
{
    self.listener = theListener;
}


@end

Compile it and run…

Please leave your valuable comments below.

Microsoft’s Brave Move – Universal Windows Platform Bridges

The Universal Windows Platform is a single app platform that is available across all Windows device families, enabling you to reach a new user base and open up new revenue opportunities. From PCs to tablets, phones, HoloLens, Surface Hub, and soon to include Xbox and Raspberry Pi – you can reach all these devices and look great on them all using one store and one codebase.

Following the launch of Windows 10, we are also releasing four Universal Windows Platform Bridge toolkits that enable you to quickly bring your existing code base to the Universal Windows Platform and add capabilities such as live tiles, active notifications, and Windows Store in-app purchases. Each toolkit provides you with tools and run-time technologies that are tailored for your codebase.

Android Runtime

For existing Android™ developers, ”Project Astoria” will enable you to build apps using Android code to target Windows 10 phones without having to leave your Android IDE.

In addition to extending the IDE, ”Project Astoria” will include a Windows phone emulator and interop capabilities that help your app (including UI and services) to run and look great on the Windows platform.

More information about ”Project Astoria” will be made available in Summer 2015, but in the meantime we are offering an invite-only preview for those interested in giving early feedback.

iOS

For existing iOS® developers, ”Project Islandwood” will enable you to build a universal Windows app from within Visual Studio 2015 using your existing Objective-C® code.

You will be able to import your Xcode® project into Visual Studio, tailor the app experience to run on each Windows device family, and extend your iOS code to take advantage of Universal Windows Platform capabilities.

More information about ”Project Islandwood” will be made available in Summer 2015, but in the meantime we are offering an invite-only preview for those interested in giving early feedback.

You can read more from here…

How to use Download manager in Android -A Simple Example.

The download manager is a system service that handles long-running HTTP
downloads. Clients may request that a URI be downloaded to a particular
destination file. The download manager will conduct the download in the
background, taking care of HTTP interactions and retrying downloads
after failures or across connectivity changes and system reboots.

Download Manager in Android

For starting a download you can use the below code.

mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
   Request mRequest = new Request(Uri.parse(URL));
   mRequest.setDescription("This is a demo File");
// mRequest.setDestinationUri(Uri.parse("give your local path"));
   idDownload = mManager.enqueue(mRequest);
		
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {		mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE 
				| DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
   }
   else
   {
    	mRequest.setShowRunningNotification(true);
   }


To remove a download…


	mManager.remove(idDownload);

Note that you can have your custom downloads folder using the below code


	mRequest.setDestinationUri(Uri.parse("give your local path"));

To open the Downloads folder.

	public void viewDownload() {
		Intent mView = new Intent();
		mView.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
		startActivity(mView);
	}

To Listen to the Download Status, You will need a BroadcastReceiver.
So create a java file that extends BroadcastReceiver and name it “DownloadStatusReceiver”.

package com.coderzheaven.downloadmanagerdemo;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class DownloadStatusReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equalsIgnoreCase(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            Toast.makeText(context, "Download Complete", Toast.LENGTH_LONG)
                    .show();
        }
        if (intent.getAction().equalsIgnoreCase(
                DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
            Toast.makeText(context, "Download Notification Clicked", Toast.LENGTH_LONG)
                    .show();
            Intent i = new Intent(context,MainActivity.class);
            i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
            context.startActivity(i);    
        }
    }
}

This is how you register the receiver in activity.


	@Override
	protected void onStart() {
		super.onStart();
		mDownloadReceiver = new DownloadStatusReceiver();
		registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mDownloadReceiver);
	}

This is the Complete source code of the MainActivity.

package com.coderzheaven.downloadmanagerdemo;

import java.io.FileNotFoundException;

import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {

	Button btnStartDwnld, btnRemoveDownload, btnOpenDownloads;
	long idDownload;
	DownloadManager mManager;
	DownloadStatusReceiver mDownloadReceiver;
	public static final String URL = "link to download file";

	@Override
	protected void onStart() {
		super.onStart();
		mDownloadReceiver = new DownloadStatusReceiver();
		registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mDownloadReceiver);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnStartDwnld = (Button) findViewById(R.id.button1);
		btnRemoveDownload = (Button) findViewById(R.id.button2);
		btnOpenDownloads = (Button) findViewById(R.id.button4);

		btnStartDwnld.setOnClickListener(this);
		btnRemoveDownload.setOnClickListener(this);
		btnOpenDownloads.setOnClickListener(this);
	}

	@SuppressWarnings("deprecation")
	void startDownload() {
		mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
		Request mRequest = new Request(Uri.parse(URL));
		mRequest.setDescription("This is a demo File");
		// mRqRequest.setDestinationUri(Uri.parse("give your local path"));
		idDownload = mManager.enqueue(mRequest);
		
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
			mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
       }
       else
       {
    	   mRequest.setShowRunningNotification(true);
       }
	}

	public void viewDownload() {
		Intent mView = new Intent();
		mView.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
		startActivity(mView);
	}

	void openDownloadedFile() {
		try {
			mManager.openDownloadedFile(idDownload);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			showToast("File has been removed");
		}
	}

	void removeDownload() {		
		mManager.remove(idDownload);
        // This will result in error because we have removed the file.
		openDownloadedFile();
	}

	private void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

	@Override
	public void onClick(View v) {
		if (v == btnStartDwnld) {
			startDownload();
		} else if (v == btnRemoveDownload) {
			removeDownload();
		} else {
			viewDownload();
		}
	}
}

You can download the source code from here.

Please leave your valuable comments…

How to download files from your FTP to your Android Device?

Hi developers,

This example shows how to download files from your FTP server to your Android device using ApacheFTP Library.

Initially we will be downloading the FTP folder structure.

After FTP structure Download, all the file urls for download will be available in the “DownloadedFileModel” model class, which you can use to
download files individually and save to your desired location. Here I am saving the files to the “files” folder inside the application sandbox.

I have created this project as a library which you can download from this link.
You need to add this project into your project as a library to work.

The usage in your activity would be like this.


	Listener metadataDownloadListener;
	
	//Set the listener first
	setMetaDataDownloadListener();
	
	//Start FTP Structure Download
	FTPService ftp = new FTPService(this, metadataDownloadListener);
	ftp.setCredentials(getString(R.string.ftp_username), getString(R.string.ftp_password));
	ftp.setParentServerFolder(getString(R.string.ftp_parent_folder));
	ftp.start();
	

“ftp_parent_folder” will be something like “/public_html/Your_Folder/Your_Contents”
Also replace with your domain name in the FTPService.java class

	public static final String server = "YOUR_SERVER_DOMAIN";

and the Listener would be


void setMetaDataDownloadListener() {
	metadataDownloadListener = new Listener() {

		@Override
		public void onSuccess(String message) {

		}

		@Override
		public void onError(String message) {
			Logger.logError(TAG, "FTP ERROR");
			tvLoading.setVisibility(View.GONE);
		}

		@Override
		public void onDataReceived(List<DownloadFile> allDownloadedFilesList) {
			Logger.logInfo(TAG, "Download files count : " + allDownloadedFilesList.size());

			Downloader downloader = new Downloader(HomePage.this, allDownloadedFilesList, tvLoading, new DownloadListener() {

				@Override
				public void onError(String message) {
					Logger.logError(TAG, "Error Downloader");						
				}

				@Override
				public void onDownloadComplete() {
					Logger.logInfo(TAG, "All Downloads are complete.");						
				}
			});
			downloader.startDownload();
		}
	};
}
	
	

Article Writers Wanted

Hi all,


Ever since I had started my website (coderzheaven.com), I had got very good response from the Visitors and I am happy that my website has helped a lot of people in their day to day work.

Now I am inviting you to write you own articles in my website about any mobile technology you want. You can send your articles to coderzheaven@gmail.com along with your details you want to be published under the post.

This website is purely created to shared knowledge among the programming enthusiasts. So I am expecting that everyone will cooperate and help me in attaining it.

Thanks a lot
James.

Pull-to-refresh libraries in Android.

Hi all…

Today’s article is about pull to refresh libraries for Android.
I think everyone has “GMAIL” app in your phone…So all may have seen a pull to refresh functionality in Gmail application
for Android.

So today I will introduce some of those libraries in Android here…

1. erikwt/PullToRefresh-ListView

Download it from here
https://github.com/erikwt/PullToRefresh-ListView

2. Yalantis/Phoenix

Download from here
https://github.com/Yalantis/Phoenix

3. chrisbanes/Android-PullToRefresh

Download from here
https://github.com/chrisbanes/Android-PullToRefresh

4. fredericojssilva/PullToUpdate-ListView

Get it from here
https://github.com/fredericojssilva/PullToUpdate-ListView

5. TuteCentral

View it here
http://www.tutecentral.com/android-pull-to-refresh/

https://dl.dropboxusercontent.com/u/91846918/CoderzHeaven%20images/PullToRefreshAndroid/pull1.png

https://dl.dropboxusercontent.com/u/91846918/CoderzHeaven%20images/PullToRefreshAndroid/pull2.png

Please leave your valuable comments here

Thankyou.

A Simple Chat Application in Android Using GCM and Server Side – PHP.

Hi all,

In Today’s Tutorial I will help you to make a simple chat application in Android using Google Cloud Messaging. I am using PHP as server side script.

Here are the things you need to follow to make this application.

1. You need to register to Google for GCM.
2. You need a server where your script resides.[We will use our own system as Server in this application].

Please read this before starting with the application
After reading the above link, you will get a API key and Sender ID , which distinguishes your application.
So you will need it in the code, keep it somewhere safe.

You need to change the API Key inside Android source code in the sample Provided below.
Your API Key and SENDER ID will look something similar to this.

 
 String SENDER_ID = "243750827028";
 String API_KEY   = "AIzaSyCBI_c2izVrEPcJ509uZGVLdfWUAW-rg48";
	

Next Go through this link, till step 2 in that page because I have provided the source code below.

The usual flow of a simple GCM application is like this

1. User launches your application for the first time.
2. Your application will call “register” to register with GCM.
3. Google will send you back a “Registration ID” which will be unique for that device.
But beware these “Registration ID’s” will change when you update the application or reinstall it after uninstalling. So don’t use it to uniquely identify a user. A username/password combination or a Phone Number usually does the trick.

4. The device gets the “Registration ID” and store it locally, so that the device don’t need to register again each time the user run the
application.
5. Send this registration ID to your server and Store it there along with a unique identifier for the user. [Here we will use ‘username’ for the time being].
6. When the user taps send message button you need to call a server side script that will communicate with GCM server which will
send this message to corresponding device.
ie. User taps the button -> send the device_reg_id and message to your server -> send these parameters from your server to GCM
-> GCM send the message to Device identified by reg id.

So I am not going to paste the whole code here.
You can directly download it from below links

Client Complete Android source code.

PHP server side source code.

Here I have used a simple database structure for simplicity.
I am using username for identifying each user.

You need to make the same structure for the below sample to work.

GCM Sample DB structure

GCM Sample DB structure

For Server Side I am using XAMPP for Mac.
You can download the Server side source code and put it inside htdocs folder inside XAMPP/Applications.

Start your servers.

Please check the Screenshot below.

GCM Sample DB structure

Then Go to your browser and type “localhost”. if your server is running, then you will see “phpMyAdmin” Page.

For testing it in your system and in your device, Both should be in the same network, for example : both should be in the same WIFI netwrok of your home.
Please type “ifconfig” for Mac in terminal and check inside “en0″ or “en1″ to get your sytem IP. For Windows type “ipconfig”.

Before running the application change the “IP” in the Utils file.

I am not going to the Android source code because Complete source is already provided.

How to test?

1. Change the API Key and Sender ID in the application.

2. Make sure server side is set up. copy php code to your local server as described above.
Make sure you have sql tables set up.

3. Make sure your servers are running.

4. Your testing devices and Your server should be on the same network, otherwise you have to host an online server. You can use the Emulator also as one device, but it should be an emulator with “Google Play Services”. Android 5.0 is good to go.

5. Change the IP address in “Utils.java” to your system IP address.

Please leave your comments if you have problems understanding the above article.

How to connect Localhost in your MAC or Windows to your Android Device OR Android Emulator ?

The name “localhost” is just a loopback to your own computer.

1. ACCESSING LOCALHOST ON ANDROID DEVICE.

To access it on your Android Device, you need to find the computer’s IP address.

  • The most general way of finding this info, which works on both Mac and Linux, is to go into the Terminal and type “ifconfig” AND for Windows type “ipconfig” in the command prompt.
  • Look for either “en0″ or “en1″, and under that entry look for the “inet” listing. It will be something along the lines of “192.168.1.100”.
  • When you find that address, that’s what you’ll want to put in your browser’s address bar or the URL in the code you want to call your MAC’s/Windows’ localhost.
  • On a Mac specifically,

    Go to System preferences/sharing OR just search on right top – remote management

    Make sure remote management is checked

Note : MAKE SURE THAT YOUR ANDROID DEVICE AND YOUR MAC/WINDOWS ARE ON THE SAME NETWORK OTHERWISE IT WILL NOT WORK. [EG : SAME WIFI NETWORK].

For Example in the Android code you can use like below and call a webservice that is residing in your local server, i.e your localhost.


 new HttpPost("http://192.168.0.101/your_localhost_folder/your_localhost_file.php");

2. ACCESSING LOCALHOST ON ANDROID EMULATOR.

You can have the same thing as described above for Android Emulator also as long as they are in same network.

But there are other options too…

You can check it in the post here.

IN App Purchase – Android – Simplified source code

Hi all,

I have already showed you how to implement in app purchase in Android.
Please go through this post before reading this article.

Android IN App Purchase

This is how the MainActivity source code will look like.

package com.coderzheaven.inappdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.coderzheaven.android.util.IabHelper;
import com.coderzheaven.inapp.InAppPurchase;
import com.coderzheaven.inapp.InAppStateListener;

public class MainActivity extends Activity implements OnClickListener,
		InAppStateListener {

	static final String TAG = "INAPPTESTING";

	// INAPP
	IabHelper mHelper;
	InAppPurchase inapp;
	static String SKU_INAPPITEM_ID = "android.test.purchased";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	@Override
	public void onClick(View v) {

		if (v.getId() == R.id.button_query) {

			showToast("Querying Items");
			inapp = new InAppPurchase(this, this, SKU_INAPPITEM_ID);
			mHelper = inapp.QueryInventry();

		} else {

			if (inapp != null)
				inapp.initiatePurchase();
			else
				showToast("Please click query items before purchasing");

		}

	}

	public void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
		;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + ","
				+ data);

		// Pass on the activity result to the helper for handling
		if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
			// not handled, so handle it ourselves (here's where you'd
			// perform any handling of activity results not related to in-app
			// billing...
			super.onActivityResult(requestCode, resultCode, data);
		} else {
			Log.d(TAG, "onActivityResult handled by IABUtil.");
		}
	}

	// IN APP PURCHASE LISTENERS

	@Override
	public void onInAppInitialiseError() {
		System.out.println("onInAppInitialiseError");
		showToast("In App initialise Error.");
	}

	@Override
	public void onInAppInitialiseSuccess() {
		System.out.println("onInAppInitialiseSuccess");
		showToast("In App initialise Success.");
	}

	@Override
	public void onQueryInventryCompleted() {
		System.out.println("onQueryInventryCompleted");
		//showToast("Querying Items Completed.");
	}

	@Override
	public void onQueryInventryError(String message) {
		System.out.println("onQueryInventryError : ");
		showToast("Querying Items Error.");
	}

	@Override
	public void onAlreadyPurchased(String inappItemID) {
		System.out.println("onAlreadyPurchased");
		showToast("Item already Purchased.");
	}

	@Override
	public void onItemNotPurchased(String inappItemID) {
		System.out.println("onItemNotPurchased : " + inappItemID);
		showToast("Item not purchased");
	}

	@Override
	public void onPurchaseSuccessfull(String inappItemID) {
		System.out.println("onPurchaseSuccessfull : " + inappItemID);
		showToast("Purchase Successful");
	}

	@Override
	public void onPurchaseError(String inappItemID, String message) {
		System.out.println("onPurchaseError : " + inappItemID);
		showToast("Purchase Error");
	}

}

You can download the complete source code from here itself.

What are static variables? Why it is used? What is it’s use? – A Common Interview Question.

The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

The static can be:

  • variable (also known as class variable)
  • method (also known as class method)
  • block
  • nested class
  • Static variable

  • If you declare any variable as static, it is known static variable.
  • The static variable can be used to refer the common property of all objects (that is not unique for each object)
  • The static variable gets memory only once in class area at the time of class loading.
  • For eg:

    public class Test {
    	public static String st_var = "I'm a static variable";
    }
    

    We can use this in another java class like below

    public class Application {
    	
    	public static void main(String[] args) {
    		System.out.println(Test.st_var); // call using Class name itself.
    	}
    
    }
    

    One common use of static is to create a constant value that’s attached to a class. The only change we need to make to the above example is to add the keyword final in there, to make ‘st_var’ a constant

    Another classic use of static is to keep count of how many objects are created from a given class. Since Memory is allocated only once for a static variable, If you create any number of objects ,then all objects will have only one common static variable. In this way we can count the number of objects created by incrementing the static variable inside that class

    If you make it Final, then it is unchangable after initial value.

    static method

  • If you apply static keyword with any method, it is known as static method
  • A static method belongs to the class rather than object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • static method can access static data member and can change the value of it.
  • For Example

    Class Test{
     static void change(){  
         st_var = "CHANGED";  
     }  
     }
     and call Like this 
     
     Test.change(); // no need of object creation
     
    

    Restrictions for static method

    There are two main restrictions for the static method. They are:

  • The static method can not use non static data member or call non-static method directly.
  • this and super cannot be used in static context.
  • For Example

    class A{  
     int num=50;//non static  
       
     public static void main(String args[]){  
      System.out.println(num);  
     }  
    }  
    

    The output of above program will be a Compile Time Error.

    Simple way to Animating Layout Changes in Android.

    A layout animation is a pre-loaded animation that the system runs each time you make a change to the layout configuration. All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you.

      Create the Layout

    In your activity’s layout XML file, set the android:animateLayoutChanges attribute to true for the layout that you want to enable animations for. For instance:

    <LinearLayout android:id="@+id/container"
        android:animateLayoutChanges="true"
        ...
    />
    
      Add, Update, or Remove Items from the Layout

    Now, all you need to do is add, remove, or update items in the layout and the items are animated automatically:

    private ViewGroup mContainerView;
    ...
    private void addItem() {
        View newView;
        ...
        mContainerView.addView(newView, 0);
    }
    
    

    Here is the complete sample layout file.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:id="@+id/container"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity" 
        android:animateLayoutChanges="true">
    
        <Button
            android:id="@+id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Views" />
    
         <Button
            android:id="@+id/buttonRemove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remove Views" />
         
    </LinearLayout>
    

    The MainActivity file.

    package com.coderzheaven.demo1;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.LinearLayout;
    
    public class MainActivity extends Activity {
    
    	LinearLayout mContainerView;
    	int id = 0;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		mContainerView = (LinearLayout) findViewById(R.id.container);
    		Button click = (Button) findViewById(R.id.buttonAdd);
    		click.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View arg0) {
    				addView();
    			}
    		});
    		Button remove = (Button) findViewById(R.id.buttonRemove);
    		remove.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View arg0) {
    				removeView();
    			}
    		});
    	}
    
    	void addView() {
    		Button b = new Button(this);
    		b.setId(id++);
    		b.setText("Button " + id);
    		mContainerView.addView(b, 0);
    	}
    
    	void removeView() {
    
    		View v = (View) findViewById(id - 1);
    		if (v != null) {
    			id--;
    			v.setVisibility(View.GONE);
    		}
    	}
    }
    
    

    Just try with a sample layout and see the magic.

    Check if Connected to Internet & if isConnected to Wifi in Android.

    This sample code checks whether the android device is connected to Internet and
    if it is connected to internet, it is connected to Wifi or Not?

    package com.coderzheaven.demo1;
    
    import android.app.Activity;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
    	NetworkInfo activeNetwork;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		if (connectedToInternet())
    			System.out.println("Connected to Internet.");
    		else
    			System.out.println("Not Connected to internet");
    
    		if (isWifi())
    			System.out.println("Connected to Wifi.");
    		else
    			System.out.println("Not Connected to Wifi");
    	}
    
    	Boolean connectedToInternet() {
    		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
    		activeNetwork = cm.getActiveNetworkInfo();
    		return (activeNetwork != null && activeNetwork
    				.isConnectedOrConnecting());
    
    	}
    
    	Boolean isWifi() {
    		return (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI);
    	}
    }
    

    MAKE SURE YOU ADD BELOW PERMISSION TO THE ANDROID MANIFEST

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    

    Localhost for Android Emulator and iPhone Simulator.

    If you are trying to access a file (PHP or java) residing in your server in your own machine, then
    you have to use a specific IP to access it. If it is public, then use the other System’s IP

    1. If it is localhost, In Android you use it as

    10.0.2.2

    The localhost refers to the device on which the code is running, in this case the emulator.

    If you want to refer to the computer which is running the Android simulator, use the IP address 10.0.2.2 instead. You can read more from here.

    2. If you are trying to access from iOS Simulator use can use

    http://localhost:8000/your_path

    The iOS Simulator uses the host machine network so you should be able to just use localhost or your machines IP address, whichever IP your web service is listening on.

    Building Custom Components in Android.

    Hi everyone,

    In this tutorial we will be building a Custom TextView.
    ie. this TextView will be extending the inBuil TextView and have its own custom attributes
    along with the default attributes in a TextView in Android.

    So we will start.

    We will be making a custom textview and presenting to the UI using

    1. XML
    2. Java Code

    1. Using Java Code

    Create a class named CustomTV and extend TextView.

    CustomTV.java

    package com.coderzheaven.customcomponent;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Color;
    import android.util.AttributeSet;
    import android.widget.TextView;
    import com.coderzheaven.customcomponent.R;
    
    public class CustomTV extends TextView {
    
    	public CustomTV(Context context) {
    		super(context);
    	}
    
    	public CustomTV(Context context, AttributeSet attrs) {
    	    super(context, attrs);
    	}
    
    	public CustomTV(Context context, AttributeSet attrs, int defStyle) {
    	    super(context, attrs, defStyle);
    	    
    	}
    	   
    }
    

    Now we go to the MainActivity and place the Custom textview using java code.

    package com.coderzheaven.customcomponent;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.widget.LinearLayout;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		LinearLayout lin = (LinearLayout) findViewById(R.id.lin);
    
    		CustomTV tv = new CustomTV(this);
    		tv.setText("I am a custom TextView from java code With Custom Attributes");
    		tv.setPadding(20, 20, 20, 20);
    		lin.addView(tv);
    
    	}
    
    }
    

    activity_main.xml

    <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:id="@+id/lin"
        android:orientation="vertical"
        tools:context=".MainActivity" >
      
    </LinearLayout>
    

    1. Using XML

    There is no change in the java code above. Instead we will be adding one more TextView in the
    activity_main.xml which is our custom one.

    <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:id="@+id/lin"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <com.coderzheaven.customcomponent.CustomTV
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="I am Custom TextView in the XML with Custom Attributes" />
    
    </LinearLayout>
    

    NOW WE WILL SEE HOW CAN APPLY OUR OWN ATTRIBUTES TO OUR CUSTOM TEXTVIEW.

    Let’s say my custom textview should have an attribute “my_color” to which I can apply my own color as string.
    [We know there is already a method for setting color in a TextView, but this is only for illustration purpose]

    Create an xml named “attrs.xml” inside res/values and copy this code into it.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="CustomTV">
            <attr name="my_color" format="string" />
        </declare-styleable>
    
    </resources>
    

    Now go to activity_main.xml and change xml code like this.
    Make sure to add your custom namespace [xmlns:custom=”http://schemas.android.com/apk/res/com.coderzheaven.customcomponent”]

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:custom="http://schemas.android.com/apk/res/com.coderzheaven.customcomponent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lin"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <com.coderzheaven.customcomponent.CustomTV
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            custom:my_color="#FF0000"  
            android:text="I am Custom TextView in the XML with Custom Attributes" />
    
    </LinearLayout>
    

    Now if you run, you can see your custom color to your custom TextView.

    Our CustomTV.java will change like this inorder to get the custom attributes and apply
    our own modifications.

    package com.coderzheaven.customcomponent;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Color;
    import android.util.AttributeSet;
    import android.widget.TextView;
    import com.coderzheaven.customcomponent.R;
    
    public class CustomTV extends TextView {
    
    	public CustomTV(Context context) {
    		super(context);
    	}
    
    	public CustomTV(Context context, AttributeSet attrs) {
    		super(context, attrs);
    
    		TypedArray a = context.obtainStyledAttributes(attrs,
    				R.styleable.CustomTV);
    
    		final int N = a.getIndexCount();
    		for (int i = 0; i < N; ++i) {
    			int attr = a.getIndex(i);
    			switch (attr) {
    			case R.styleable.CustomTV_my_color:
    				String my_color = a.getString(attr);
    				setCustomColor(my_color);
    				break;
    			}
    		}
    		a.recycle();
    
    	}
    
    	public void setCustomColor(String color) {
    		this.setTextColor(Color.parseColor(color));
    	}
    
    	public CustomTV(Context context, AttributeSet attrs, int defStyle) {
    		super(context, attrs, defStyle);
    
    	}
    
    }
    
    

    Apply your custom attribute in Java code also.

       tv.setCustomColor("#00FF00");
    

    Nexus 6 Came…

    Nexus 6

    Google Nexus 6

    Specifications

    GENERAL 2G Network GSM 850 / 900 / 1800 / 1900 – all models
      CDMA 800 / 1900 – XT1103 US model
    3G Network HSDPA 800 / 850 / 1700 / 1900 / 2100 / 900 – XT1100 Global model
      HSDPA 850 / 900 / 1700 / 1900 / 2100 – XT1103 US model
    4G Network LTE 700 / 800 / 900 / 1800 / 2100 / 2600 / 850 / 2500 – XT1100 Global model
      LTE 700 / 850 / 900 / 1800 / 1900 / 2100 / 2600 / 2500 – XT1103 US model
    SIM Nano-SIM
    Announced 2014, October
    Status Coming soon. Exp. release 2014, October
    BODY Dimensions 159.3 x 83 x 10.1 mm (6.27 x 3.27 x 0.40 in)
    Weight 184 g (6.49 oz)
    DISPLAY Type AMOLED capacitive touchscreen, 16M colors
    Size 1440 x 2560 pixels, 5.96 inches (~493 ppi pixel density)
    Multitouch Yes
    Protection Corning Gorilla Glass 3
    SOUND Alert types Vibration; MP3, WAV ringtones
    Loudspeaker Yes, with stereo speakers
    3.5mm jack Yes
    MEMORY Card slot No
    Internal 32/64 GB, 3 GB RAM
    DATA GPRS Yes
    EDGE Yes
    Speed DC-HSDPA, 42 Mbps; HSDPA, 21 Mbps; HSUPA, 5.76 Mbps; LTE, Cat6, 50 Mbps UL, 300 Mbps DL
    WLAN Wi-Fi 802.11 a/b/g/n/ac, dual-band, Wi-Fi Direct, DLNA, Wi-Fi hotspot
    Bluetooth v4.0, A2DP
    NFC Yes
    USB microUSB v2.0 (SlimPort), USB Host, USB On-the-go
    CAMERA Primary 13 MP, 4128 x 3096 pixels, autofocus, optical image stabilization, dual-LED flash
    Features Geo-tagging, touch focus, face detection, photo sphere, HDR, dual recording
    Video 2160p@30fps, optical stabilization
    Secondary 2 MP
    FEATURES OS Android OS, v5.0 (Lollipop)
    Chipset Qualcomm Snapdragon 805
    CPU Quad-core 2.7 GHz Krait 450
    GPU Adreno 420
    Sensors Accelerometer, gyro, proximity, compass, barometer
    Messaging SMS(threaded view), MMS, Email, Push Mail, IM
    Browser HTML5
    Radio No
    GPS Yes, with A-GPS, GLONASS
    Java Yes, via Java MIDP emulator
    Colors Midnight Blue, Cloud White
      – Wireless charging
    – Active noise cancellation with dedicated mic
    – MP4/H.264/H.263 player
    – MP3/WAV/eAAC+ player
    – Organizer
    – Photo/video editor
    – Document editor
    – Voice memo/dial/commands
    – Predictive text input
    BATTERY   Non-removable Li-Po 3220 mAh battery
    Stand-by Up to 330 h
    Talk time Up to 24 h

    How to change title and Subtitle text color and size of ActionBar in Android?

    Here is a simple example that shows how you can change title and subtitle textcolor and its size in Android

    Method 1
    ——–

    Changing the styles [For support Library]

    Go to Values/styles.xml copy this code to change the textcolor and actionbar background color.

    
    <style name="CustomActivityTheme" parent="@style/Theme.AppCompat.Light">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
            <item name="actionBarStyle">@style/MyActionBar</item>
             <item name="android:windowActionBarOverlay">true</item>
            <!-- other activity and action bar styles here -->
    </style>
          <!-- style for the action bar backgrounds -->
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
            <item name="android:background">@drawable/actionbar_background</item>
            <item name="background">@drawable/actionbar_background</item>
            <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
            <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
            <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
            <item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
         </style>
    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance">
            <item name="android:textColor">@color/color_title</item>
            <item name="android:textSize">12sp</item>
    </style>
    	  
    	  
    

    Note : For applying theme if you are not using the Support Library then change the AppCompat to Holo.

    i.e make the Below changes in the parent

    parent="@android:style/Theme.Holo.Light"
    

    AND

    parent="@android:style/Widget.Holo.Light.ActionBar"
    

    AND for title

    parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title"
    

    Now Apply this theme in the AndroidManifest.xml, either for the entire application or a particular activity.

    For example

    
     <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/CustomActivityTheme" >
            <activity
                android:name="com.example.actionbarcustomize.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>
    	
    

    Make Sure you add the colors in the strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">ActionBarCustomize</string>
        <string name="hello_world">Hello world!</string>
        <string name="menu_settings">Settings</string>
    
        <drawable name="actionbar_background">#FF0000</drawable>
        <color name="color_title">#00FF00</color>
    	
    </resources>
    

    Method 2
    ———

    You can use a simple java code to so this.

    getActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));
    

    Note : Change getActionBar() to getSupportActionBar() if you are using the support library for ActionBar.

    That’s all.. Now Run the application and see the changes.

    Android In App Purchased Demo V3.

    Hi all,

    This demo shows how to do in app purchase for managed products in Android using Version 3 in-app Billing API.

    Please Go through this before trying out this Demo

    http://developer.android.com/google/play/billing/index.html

    Android In App Purchase

    These are the important steps in to remember for doing in app purchase.

    1. You have to download the Google In App Billing Library from Google.

    For that you have to open the Android SDK Manager and Look for “Google Play Billing Library”.

    2. You have to make changes in the Android Manifest first.

    Add this permission.

         <!-- Permission for In App Purchase -->
        <uses-permission android:name="com.android.vending.BILLING" />
         

    The above permission will detect that your app contains “In App Purchases” in Google Play Store.

    3. Now add the .aidl file that you get while downloading the library from Google.

    [You can find this library in the Sample Project at the end of this post].

    4. Now let’s assume that we have done the coding part by giving a demo in_app item.

    5. You can test the application in two ways either using STATIC RESPONSES from Google Play

    OR real purchases with users but without using any money.

    STATIC TESTING :

    READ THIS LINK : http://developer.android.com/google/play/billing/billing_testing.html#billing-testing-static

    REAL TESTING – AFTER UPLOADING TO GOOGLE PLAY (IN ALPHA OR BETA MODE)

    READ THIS LINK : http://developer.android.com/google/play/billing/billing_testing.html#testing-purchases.

    NOTE : If you publish your application through Alpha or Beta mode, the users who are downloading from Google Play will not see it or get it. In that way Google ensures that it is not published before testing.

    For REAL TESTING AFTER UPLOADING TO GOOGLE PLAY you must be adding some users as Test Users who can test this app that you have
    You can add test users while you upload the apk with in app Billing in Alpha or Beta Mode.

    For adding test users you can see a link in the APK page of the particular app in Alpha or Beta Mode.[This is usually a Google community or Groups]

    For a user to be a Test account you have to go to Settings > Gmail accounts with testing access > add the Gmail account to test.

    This means that these users can buy this in app Item without loosing any money in alpha or Beta Mode.

    Important Note :

    In Google In App Billing V3, all purchases are managed that means you can even consume managed products.

    PRODUCTS ONCE CONSUMED WILL BE A AVAILABLE FOR PURCHASE AGAIN.

    THAT MEANS IF YOU PURCHASE A MANAGED PRODUCT AND IF THE USER CONSUMES IT, THEN IT WILL BE AGAIN AVAILABLE FOR PURCHASE.

    IF THE USER CONSUMES IT GOOGLE WILL REMOVE THAT PURCHASED ITEM FROM IT’S SEVER AND MARK IT AS ‘UN-OWNED’.

    SO WHEN YOU QUERY THE PURCHASED ITEMS YOU WILL NOT BE GETTING THE PURCHASED ITEM.

    SO IF YOU WANT THE USER TO PURCHASE THE ITEM ONLY ONCE AND AVAILABLE ALL THE TIME DON’T CONSUME IT

    THAT MEANS YOU HAVE TO COMMENT OUT THIS CODE FROM THE SAMPLE CODE PROVIDED.

       	mHelper.consumeAsync(purchase, mConsumeFinishedListener);
       

    That is you can directly write your code inside this if the purchase is sucessful

       if (purchase.getSku().equals(SKU_INAPPITEM)) {
       }
       

    OK Done.

    You can download the sample application from this link

    Download In App Billing Source Code

    You can also check this post for more simplified example.

    Yota Phone – The Android Smartphone From Russia With Two Screens

    Yota Phone

    Let’s start with the underlying hardware. Compared to some of the flagship and high-end Android devices launched in 2013, the Yota Phone is decidedly mid-range. The Dual-Core 1.7 GHz Krait CPU has the speed and capability to run Android comfortably, but the handset doesn’t stretch the specs in the current market. It’s nice to see it comes with 2 GB of RAM, and when it was announced at CES 2013 these were cutting-edge specs, but the Android world has moved on since then.

    The handset comes in just one storage memory configuration (32 GB) and unfortunately there is no SD card expansion port. Given 16 GB feels a bit tight on Android handsets today, the 32 GB option should be good for the life of the handset, and with smart use of cloud based services for storage and streaming it should suffice for the majority of use cases.

    Read More from here.
    http://www.forbes.com/sites/ewanspence/2014/01/04/yota-phone-review-the-android-smartphone-from-russia-with-two-screens/

    App Translation Service Now Available to All Developers

    To help developers reach users in other languages, Google launched the App Translation Service, which allows developers to purchase professional app translations through the Google Play Developer Console. This is part of a toolbox of localization features you can (and should!) take advantage of as you distribute your app around the world through Google Play.

    You’ll find the App Translation Service in the Developer Console at the bottom of the APK section — you can start a new translation or manage an existing translation here. You’ll be able to upload your app’s file of string resources, select the languages you want to translate into, select a professional translation vendor, and place your order. Pro tip: you can put your store listing text into the file you upload to the App Translation Service. You’ll be able to communicate with your translator to be sure you get a great result, and download your translated string files. After you do some localization testing, you’ll be ready to publish your newly translated app update on Google Play — with localized store listing text and graphics. Be sure to check back to see the results on your user base, and track the results of marketing campaigns in your new languages using Google Analytics integration.

    You can read more from here

    Intel ready to turn Android into Once Windows were…

    Yes

    The chip maker is now ready for making android scale to even bigger screen that is dominated by the Windows platform..
    Intel is reportedly thinking beyond Microsoft when it comes to a full-fledged ‘client’ operating system, and the best bet today for it is Google’s Android.

    The chipmaker’s general manager, Kirk Skaugen said that for the last decade they have been essentially 100 percent Microsoft but now with the emerging markets demand for Android is seen.

    More about this news from here.
    http://www.dnaindia.com/scitech/report-intel-ready-to-turn-android-into-windows-like-os-1924236

    How to work in ActionBar support Library.

    Hi all

    Today’s post is all about working with ActionBar support libraries from Google.
    These libraries support from Android 2.1 (API 7).

    ActionBar Support Custom

    For working with these libraries we have to follow some instructins.

    Please follow these steps exactly for setting up a Actionbar support library.

    1. Start the Android SDK Manager.
    In the SDK Manager window, scroll to the end of the Packages list, find the Extras folder and, if necessary, expand to show its contents.
    2. Select the Android Support Library item.
    Note: If you’re developing with Android Studio, select and install the Android Support Repository item instead.
    3. Click the Install packages… button.

    ActionBar Support Installation

    After downloading, the tool installs the Support Library files to your existing Android SDK directory. The library files are located in the following subdirectory of your SDK: /extras/android/support/ directory.

    Now Open Eclipse and do the following.

    Create a library project based on the support library code:

    Make sure you have downloaded the Android Support Library using the SDK Manager.
    Create a library project and ensure the required JAR files are included in the project’s build path:
    Select File > Import.
    Select Existing Android Code Into Workspace and click Next.

    Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/.

    Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.

    In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.

    Right-click the project and select Build Path > Configure Build Path.
    In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
    Uncheck Android Dependencies.

    Click OK to complete the changes.

    AndroidManifest.xml changes
    android:minSdkVersion="7"
    android:targetSdkVersion="17">

    Important
    1. To work with support libraries of actionbar you should also have to change the theme to “Theme.AppCompat”.
    2. Also your activity should extend ActionBarActivity.

    This is all that needed.
    After that you can add this actionbar compat library for any number of projects.

    Usage.

    Instead of using getActionBar(), use should use getSupportActionBar() to point to suppport actionbar libraries.

    A sample looks like this.

    package com.coderzheaven.actionbartest;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    
    import com.example.actionbartest.R;
    
    public class MainActivity extends ActionBarActivity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		getSupportActionBar().setTitle("ActionBar Support Demo");
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.activity_main, menu);
    		return true;
    	}
    
    }
    

    For that you have to make changes in the styles.xml.

    This is my styles.xml in which I have made my ActionBar Custom.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- the theme applied to the application or activity -->
        <style name="CustomActionBarTheme"
               parent="@style/Theme.AppCompat.Light.DarkActionBar">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
    
            <!-- Support library compatibility -->
            <item name="actionBarStyle">@style/MyActionBar</item>
        </style>
    
        <!-- ActionBar styles -->
        <style name="MyActionBar"
               parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
            <item name="android:background">@drawable/blue</item>
    
            <!-- Support library compatibility -->
            <item name="background">@drawable/blue</item>
        </style>
    </resources>
    

    My strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">ActionBarTest</string>
        <string name="hello_world">Hello world!</string>
        <string name="menu_settings">Settings</string>
        
        <drawable name="blue">#0000FF</drawable>
    
    </resources>
    

    So you are done.
    Go on and run the project.

    You can download a sample of Project which contains the ActionBarCompat Library and a sample project which links this library from here.
    So when you import it to your eclipse make sure you import two projects included.

    How to Create a new theme for ActionBar in Android.

    In this post I will be telling you about how to change the default theme of ActionBar with our own custom One.
    Here I will be changing the background color, textColor and alpha of default ActionBar.
    Let’s see how it is done.

    By Checking out the styles.xml you will find that by default it looks like this.

    ActionBar Theme

    <resources>
     <style name="AppTheme" parent="android:Theme.Light" />
    </resources>
    

    Right now, we are using a theme “AppTheme” which is inheriting all the attributes from Theme.Light.
    Lets define a new style named “myActionBarTheme”.

    Now my styles.xml looks like this.
    Note that this time i Just changed “Holo.Light” to just “Holo” for darker theme.
    However you can leave it as the old one.

    styles.xml

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    
        <style name="CustomActionBarTheme" parent="android:style/Theme.Holo">
            <item name="android:actionBarStyle">@style/myActionBarTheme</item>
            
        </style>
    
        <style name="myActionBarTheme" parent="android:style/Widget.Holo.ActionBar">
            <item name="android:background">#FF4444</item>
            <item name="android:titleTextStyle">@style/myActionBarTitleTextStyle</item>
            <item name="android:alpha">1.0</item>
        </style>
         <style name="myActionBarTitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
            <item name="android:textColor">#FFFFFF</item>
        </style>
    
    </resources>
    

    Now we have to use it right. If we want to apply it to the complete application we can do it like this in the Android Manifest file.

    AndroidManifest.xml

    <application android:theme="@style/CustomActionBarTheme" >
    

    Or You can apply different ones to different activities also.
    In this example I am just changing the ActionBar text color and alpha also.
    You can do any customization according to your wish.

    What ever we use as background will be strectched to fill the size of the ActionBar , so make sure you will be providing a nine-patch image or an XML.
    Here I am using an Xml named “top_bar_bg.xml”

    top_bar_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
          <shape>
             <gradient android:angle="90" 
                  android:centerColor="#FF0066FF" 
                  android:endColor="#FF0066FF" 
                  android:startColor="#FF0066FF" />
            </shape>
        </item>
    </selector>
    

    More and more ActionBar customizations and tips will be coming in the next updates.
    Please visit again or subscribe to coderzheaven.com and also like us on facebook to get quick updates.

    PLease leave your valuable comments on this post.

    You can download the complete source code from here.

    How to handle configuration change in an activity using Fragments in android?

    Here is a simple example in which we will save or retain the state of the progress using fragments.

    Note : We have handle a configuration by another way – by ignoring the change i.e using this property in the activity tag
    android:configChanges=””
    By using this android ignores the configuration change and will never try to create a new instance of the activity.
    But Google discourages this practice.

    Handling configuration changes requires you to take many additional steps to ensure that each and every string, layout, drawable, dimension, etc. remains in sync with the device’s current configuration, and if you aren’t careful, you’re application can easily have a whole series of resource-specific bugs as a result.

    Another reason why Google discourages its use is because many developers incorrectly assume that setting android:configChanges=”orientation” (for example) will magically protect their application from unpredictable scenarios in which the underlying Activity will be destroyed and recreated. This is not the case. Configuration changes can occur for a number of reasons—not just screen orientation changes. Inserting your device into a display dock, changing the default language, and modifying the device’s default font scaling factor are just three examples of events that can trigger a device configuration change, all of which signal the system to destroy and recreate all currently running Activitys the next time they are resumed. As a result, setting the android:configChanges attribute is generally not good practice.

    retain state

    retain state

    Let’s go to the code and see how it is done.

    Now we will see how to do that.

    At first I will show you the layout which simply contains a textview that show the progress.

    <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"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_alignParentTop="true"
            android:text="Handling Runtime configuration changes by saving state using Fragments"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" >
    
            <TextView
                android:id="@+id/tv_pro"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:textColor="@android:color/holo_blue_bright"
                android:textSize="40sp" />
        </FrameLayout>
    
    </RelativeLayout>
    

    Now we will see the code for the activity.

    package com.example.retaininstanceusingfragmentsexample;
    
    import android.app.Activity;
    import android.app.FragmentManager;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements
    		TaskFragment.TaskCallbacks {
    
    	private TaskFragment mTaskFragment;
    	TextView tvPro = null;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		tvPro = (TextView) findViewById(R.id.tv_pro);
    
    		FragmentManager fm = getFragmentManager();
    		mTaskFragment = (TaskFragment) fm.findFragmentByTag("task");
    
    		// If the Fragment is not null, then it is currently being
    		// retained when a configuration change occurs.
    		if (mTaskFragment == null) {
    			mTaskFragment = new TaskFragment();
    			fm.beginTransaction().add(mTaskFragment, "task").commit();
    		}
    
    		// TODO: initialize views, restore saved state, etc.
    	}
    
    	@Override
    	public void onPreExecute() {
    	}
    
    	@Override
    	public void onProgressUpdate(int percent) {
    		tvPro.setText(percent + "%");
    	}
    
    	@Override
    	public void onCancelled() {
    	}
    
    	@Override
    	public void onPostExecute() {
    	}
    }
    

    Now the Fragments that handles the retained state.

    package com.example.retaininstanceusingfragmentsexample;
    
    import android.app.Activity;
    import android.app.Fragment;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.SystemClock;
    
    /**
     * This Fragment manages a single background task and retains itself across
     * configuration changes.
     */
    public class TaskFragment extends Fragment {
    
    	/**
    	 * Callback interface through which the fragment will report the task's
    	 * progress and results back to the Activity.
    	 */
    	static interface TaskCallbacks {
    		void onPreExecute();
    
    		void onProgressUpdate(int percent);
    
    		void onCancelled();
    
    		void onPostExecute();
    	}
    
    	private TaskCallbacks mCallbacks;
    	private DummyTask mTask;
    
    	/**
    	 * Hold a reference to the parent Activity so we can report the task's
    	 * current progress and results. The Android framework will pass us a
    	 * reference to the newly created Activity after each configuration change.
    	 */
    	@Override
    	public void onAttach(Activity activity) {
    		super.onAttach(activity);
    		mCallbacks = (TaskCallbacks) activity;
    	}
    
    	/**
    	 * This method will only be called once when the retained Fragment is first
    	 * created.
    	 */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    
    		// Retain this fragment across configuration changes.
    		setRetainInstance(true);
    
    		// Create and execute the background task.
    		mTask = new DummyTask();
    		mTask.execute();
    	}
    
    	/**
    	 * Set the callback to null so we don't accidentally leak the Activity
    	 * instance.
    	 */
    	@Override
    	public void onDetach() {
    		super.onDetach();
    		mCallbacks = null;
    	}
    
    	/**
    	 * A dummy task that performs some (dumb) background work and update the UI
    	 */
    	private class DummyTask extends AsyncTask<Void, Integer, Void> {
    
    		@Override
    		protected void onPreExecute() {
    			if (mCallbacks != null) {
    				mCallbacks.onPreExecute();
    			}
    		}
    
    		/**
    		 * Note that we do NOT call the callback object's methods directly from
    		 * the background thread, as this could result in a race condition.
    		 */
    		@Override
    		protected Void doInBackground(Void... ignore) {
    			for (int i = 0; !isCancelled() && i < 100; i++) {
    				SystemClock.sleep(500);
    				// call to onProgressUpdate
    				publishProgress(i);
    			}
    			return null;
    		}
    
    		@Override
    		protected void onProgressUpdate(Integer... percent) {
    			if (mCallbacks != null) {
    				mCallbacks.onProgressUpdate(percent[0]);
    			}
    		}
    
    		@Override
    		protected void onCancelled() {
    			if (mCallbacks != null) {
    				mCallbacks.onCancelled();
    			}
    		}
    
    		@Override
    		protected void onPostExecute(Void ignore) {
    			if (mCallbacks != null) {
    				mCallbacks.onPostExecute();
    			}
    		}
    	}
    }
    

    After running this application, try to rotate the device and see how the fragment retains the state of the progress.

    You can download the complete source code of this post from here.

    Using Meta-data in Android Manifest and accessing it.

    Sometimes you have the need to set up some app-wide configuration information in an Android app or need to create a class that can be used in multiple projects with a generic way of setting configuration values. This is particularly useful for things like API keys that will probably be different across apps but should be accessible in the same way. There are several ways to do it, but the one I’ve come to prefer is adding a meta-data node to the AndroidManifest.xml file. If you are familiar with Android Adnetworks, most of them are using meta-data for this.

    This field can be used to store a boolean, float, int, or String and is later accessed by the Bundle method for your data type (e.g., getInt()). Here is an example of how to define a value in your AndroidManifest.xml:

    Let’s look at an example

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.metadataexample"
        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.metadataexample.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>
            
             <meta-data android:name="myAPIKey" android:value="sample_value" />
        </application>
    
    </manifest>
    

    To get the value in the java code.

    String TAG = "Example Meta-Data";
    		try {
    			ApplicationInfo ai = getPackageManager().getApplicationInfo(
    					getPackageName(), PackageManager.GET_META_DATA);
    			Bundle bundle = ai.metaData;
    			String myAPIKey = bundle.getString("myAPIKey");
    			System.out.println("API KEY : " + myAPIKey);
    		} catch (NameNotFoundException e) {
    			Log.e(TAG,
    					"Failed to load meta-data, NameNotFound: " + e.getMessage());
    		} catch (NullPointerException e) {
    			Log.e(TAG,
    					"Failed to load meta-data, NullPointer: " + e.getMessage());
    		}
    

    You can download the sample code from here.

    How to write a custom content provider in android? with a sample application using our own custom content provider.

    Hello all

    we all know what are content providers right?
    Those who are unaware of content providers please visit this link http://developer.android.com/guide/topics/providers/content-providers.html

    Custom Content Provider

    Custom Content Provider

    In a breif content Providers are

    Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.

    When you want to access data in a content provider, you use the ContentResolver object in your application’s Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.

    This sample Demo is creating custom content provider using SQLite Database.

    Now we will see how we can create our own custom content provider.

    Tn this example I have three classes.
    1. MainActivity.java
    2. NotesContentProvider.java
    3. NotesMetaData.java

    At first we will look at “NotesMetaData.java”.

    package com.coderzheaven.custom_contentproviderdemo;
    
    import android.net.Uri;
    import android.provider.BaseColumns;
    
    public class NotesMetaData {
    
    	public NotesMetaData() {
    
    	}
    
    	// A content URI is a URI that identifies data in a provider. Content URIs
    	// include the symbolic name of the entire provider (its authority)
    	public static final String AUTHORITY = "com.coderzheaven.custom_contentproviderdemo.Notes";
    	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
    			+ "/notes");
    
    	public static final String DATABASE_NAME = "notes.db";
    	public static final int DATABASE_VERSION = 1;
    
    	public static final String CONTENT_TYPE_NOTES_ALL = "vnd.android.cursor.dir/vnd.coderz.notes";
    	public static final String CONTENT_TYPE_NOTES_ONE = "vnd.android.cursor.item/vnd.coderz.notes";
    
    	public class NotesTable implements BaseColumns {
    
    		private NotesTable() {
    
    		}
    
    		public static final String TABLE_NAME = "tbl_notes";
    
    		public static final String ID = "_id";
    		public static final String TITLE = "title";
    		public static final String CONTENT = "content";
    	}
    
    }
    
    

    NotesMetaData class defines some required constant values to use:

    + AUTHORITY: this is the name of your content provider

    + CONTENT_URI: is your content provider URI for other applications to access data from it.

    Let’s say if you want to get all notes, it would be like:

    content://”com.coderzheaven.custom_contentproviderdemo.Notes/notes”

    To retrieve a specific note:

    content://com.coderzheaven.custom_contentproviderdemo.Notes/notes/5″

    Now the “NotesContentProvider.java”

    package com.coderzheaven.custom_contentproviderdemo;
    
    import java.util.HashMap;
    
    import android.content.ContentProvider;
    import android.content.ContentUris;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.UriMatcher;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.database.sqlite.SQLiteQueryBuilder;
    import android.net.Uri;
    import android.text.TextUtils;
    
    public class NotesContentProvider extends ContentProvider {
    
    	private static final UriMatcher sUriMatcher;
    
    	private static final int NOTES_ALL = 1;
    	private static final int NOTES_ONE = 2;
    
    	static {
    		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    		sUriMatcher.addURI(NotesMetaData.AUTHORITY, "notes", NOTES_ALL);
    		sUriMatcher.addURI(NotesMetaData.AUTHORITY, "notes/#", NOTES_ONE);
    	}
    
    	// Map table columns
    	private static final HashMap<String, String> sNotesColumnProjectionMap;
    	static {
    		sNotesColumnProjectionMap = new HashMap<String, String>();
    		sNotesColumnProjectionMap.put(NotesMetaData.NotesTable.ID,
    				NotesMetaData.NotesTable.ID);
    		sNotesColumnProjectionMap.put(NotesMetaData.NotesTable.TITLE,
    				NotesMetaData.NotesTable.TITLE);
    		sNotesColumnProjectionMap.put(NotesMetaData.NotesTable.CONTENT,
    				NotesMetaData.NotesTable.CONTENT);
    	}
    
    	private static class NotesDBHelper extends SQLiteOpenHelper {
    
    		public NotesDBHelper(Context c) {
    			super(c, NotesMetaData.DATABASE_NAME, null,
    					NotesMetaData.DATABASE_VERSION);
    		}
    
    		private static final String SQL_QUERY_CREATE = "CREATE TABLE "
    				+ NotesMetaData.NotesTable.TABLE_NAME + " ("
    				+ NotesMetaData.NotesTable.ID
    				+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
    				+ NotesMetaData.NotesTable.TITLE + " TEXT NOT NULL, "
    				+ NotesMetaData.NotesTable.CONTENT + " TEXT NOT NULL" + ");";
    
    		@Override
    		public void onCreate(SQLiteDatabase db) {
    			db.execSQL(SQL_QUERY_CREATE);
    		}
    
    		private static final String SQL_QUERY_DROP = "DROP TABLE IF EXISTS "
    				+ NotesMetaData.NotesTable.TABLE_NAME + ";";
    
    		@Override
    		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
    			db.execSQL(SQL_QUERY_DROP);
    			onCreate(db);
    		}
    	}
    
    	// create a db helper object
    	private NotesDBHelper mDbHelper;
    
    	@Override
    	public boolean onCreate() {
    		mDbHelper = new NotesDBHelper(getContext());
    		return false;
    	}
    
    	@Override
    	public int delete(Uri uri, String where, String[] whereArgs) {
    		SQLiteDatabase db = mDbHelper.getWritableDatabase();
    		int count = 0;
    		switch (sUriMatcher.match(uri)) {
    		case NOTES_ALL:
    			count = db.delete(NotesMetaData.NotesTable.TABLE_NAME, where,
    					whereArgs);
    			break;
    
    		case NOTES_ONE:
    			String rowId = uri.getPathSegments().get(1);
    			count = db.delete(
    					NotesMetaData.NotesTable.TABLE_NAME,
    					NotesMetaData.NotesTable.ID
    							+ " = "
    							+ rowId
    							+ (!TextUtils.isEmpty(where) ? " AND (" + where
    									+ ")" : ""), whereArgs);
    			break;
    
    		default:
    			throw new IllegalArgumentException("Unknown URI: " + uri);
    		}
    
    		getContext().getContentResolver().notifyChange(uri, null);
    		return count;
    	}
    
    	@Override
    	public String getType(Uri uri) {
    
    		switch (sUriMatcher.match(uri)) {
    		case NOTES_ALL:
    			return NotesMetaData.CONTENT_TYPE_NOTES_ALL;
    
    		case NOTES_ONE:
    			return NotesMetaData.CONTENT_TYPE_NOTES_ONE;
    
    		default:
    			throw new IllegalArgumentException("Unknown URI: " + uri);
    		}
    	}
    
    	@Override
    	public Uri insert(Uri uri, ContentValues values) {
    
    		// you cannot insert a bunch of values at once so throw exception
    		if (sUriMatcher.match(uri) != NOTES_ALL) {
    			throw new IllegalArgumentException(" Unknown URI: " + uri);
    		}
    
    		// Insert once row
    		SQLiteDatabase db = mDbHelper.getWritableDatabase();
    		long rowId = db.insert(NotesMetaData.NotesTable.TABLE_NAME, null,
    				values);
    		if (rowId > 0) {
    			Uri notesUri = ContentUris.withAppendedId(
    					NotesMetaData.CONTENT_URI, rowId);
    			getContext().getContentResolver().notifyChange(notesUri, null);
    			return notesUri;
    		}
    		throw new IllegalArgumentException("<Illegal>Unknown URI: " + uri);
    	}
    
    	// Get values from Content Provider
    	@Override
    	public Cursor query(Uri uri, String[] projection, String selection,
    			String[] selectionArgs, String sortOrder) {
    		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    		switch (sUriMatcher.match(uri)) {
    		case NOTES_ALL:
    			builder.setTables(NotesMetaData.NotesTable.TABLE_NAME);
    			builder.setProjectionMap(sNotesColumnProjectionMap);
    			break;
    
    		case NOTES_ONE:
    			builder.setTables(NotesMetaData.NotesTable.TABLE_NAME);
    			builder.setProjectionMap(sNotesColumnProjectionMap);
    			builder.appendWhere(NotesMetaData.NotesTable.ID + " = "
    					+ uri.getLastPathSegment());
    			break;
    
    		default:
    			throw new IllegalArgumentException("Unknown URI: " + uri);
    		}
    
    		SQLiteDatabase db = mDbHelper.getReadableDatabase();
    		Cursor queryCursor = builder.query(db, projection, selection,
    				selectionArgs, null, null, null);
    		queryCursor.setNotificationUri(getContext().getContentResolver(), uri);
    
    		return queryCursor;
    	}
    
    	@Override
    	public int update(Uri uri, ContentValues values, String where,
    			String[] whereArgs) {
    
    		SQLiteDatabase db = mDbHelper.getWritableDatabase();
    		int count = 0;
    		switch (sUriMatcher.match(uri)) {
    		case NOTES_ALL:
    			count = db.update(NotesMetaData.NotesTable.TABLE_NAME, values,
    					where, whereArgs);
    			break;
    
    		case NOTES_ONE:
    			String rowId = uri.getLastPathSegment();
    			count = db
    					.update(NotesMetaData.NotesTable.TABLE_NAME, values,
    							NotesMetaData.NotesTable.ID	+ " = "	+ rowId	+ (!TextUtils.isEmpty(where) ? " AND ("	+ ")" : ""), whereArgs);
    
    		default:
    			throw new IllegalArgumentException("Unknown URI: " + uri);
    		}
    
    		getContext().getContentResolver().notifyChange(uri, null);
    		return count;
    	}
    
    }
    

    When you insert/update/delete, always remember to call “notifyChange()” to the URI that has been used.

    When you query values, always remember to to call “setNotificationUri()” for Cursor.

    That’s done for creating your custom content provider. In order to use, you need to register it to AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.coderzheaven.custom_contentproviderdemo"
        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.coderzheaven.custom_contentproviderdemo.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>
            
            
            
            <provider
                android:name="com.coderzheaven.custom_contentproviderdemo.NotesContentProvider"
                android:authorities="com.coderzheaven.custom_contentproviderdemo.Notes" >
            </provider>
            
        </application>
    
    </manifest>
    

    Now we look how we can use it in an activity

    MainActivity.java

    package com.coderzheaven.custom_contentproviderdemo;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import com.coderzheaven.custom_contentproviderdemo.NotesMetaData.NotesTable;
    
    public class MainActivity extends Activity implements OnClickListener {
    
    	private final static String TAG = "CustomContentProvider";
    	EditText title, content, delete_id;
    	Button add, update, delete, showNotes;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		title = (EditText) findViewById(R.id.title);
    		content = (EditText) findViewById(R.id.content);
    		delete_id = (EditText) findViewById(R.id.delete_id);
    
    		// add Click Listners
    		add = (Button) findViewById(R.id.button_add);
    		add.setOnClickListener(this);
    		update = (Button) findViewById(R.id.button_update);
    		update.setOnClickListener(this);
    		delete = (Button) findViewById(R.id.button_delete);
    		delete.setOnClickListener(this);
    		showNotes = (Button) findViewById(R.id.show_notes);
    		showNotes.setOnClickListener(this);
    
    		getNotes();
    
    	}
    
    	void addNote() {
    		if (title.getText().toString().length() > 0
    				&& content.getText().toString().length() > 0) {
    			ContentValues values = new ContentValues();
    			values.put(NotesTable.TITLE, title.getText().toString());
    			values.put(NotesTable.CONTENT, content.getText().toString());
    			getContentResolver().insert(NotesMetaData.CONTENT_URI, values);
    			Log.i(TAG, "Inserted");
    			makeToast("Note Added");
    		} else {
    			makeToast("Empty Field");
    		}
    	}
    
    	void deleteNote(String str_id) {
    		try {
    			int id = Integer.parseInt(str_id);
    			Log.i(TAG, "Deleting with id = " + id);
    			getContentResolver().delete(NotesMetaData.CONTENT_URI,
    					NotesMetaData.NotesTable.ID + " = " + id, null);
    			Log.i(TAG, "Deleted");
    			makeToast("Note Deleted");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	void updateNote(String str_id) {
    		try {
    			int id = Integer.parseInt(str_id);
    			Log.i(TAG, "Updating with id = " + id);
    			ContentValues values = new ContentValues();
    			values.put(NotesTable.TITLE, title.getText().toString());
    			values.put(NotesTable.CONTENT, content.getText().toString());
    			getContentResolver().update(NotesMetaData.CONTENT_URI, values,
    					NotesMetaData.NotesTable.ID + " = " + id, null);
    			makeToast("Note Updated");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	void getNotes() {
    
    		Cursor cur = getContentResolver().query(NotesMetaData.CONTENT_URI,
    				null, null, null, null);
    
    		if (cur.getCount() > 0) {
    			Log.i(TAG, "Showing values.....");
    			while (cur.moveToNext()) {
    				String Id = cur.getString(cur.getColumnIndex(NotesTable.ID));
    				String title = cur.getString(cur
    						.getColumnIndex(NotesTable.TITLE));
    				System.out.println("Id = " + Id + ", Note Title : " + title);
    			}
    			makeToast("Check the LogCat for Notes");
    		} else {
    			Log.i(TAG, "No Notes added");
    			makeToast("No Notes added");
    		}
    	}
    
    	@Override
    	public void onClick(View arg0) {
    		if (arg0 == add) {
    			addNote();
    		}
    		if (arg0 == update) {
    			// update note with Id
    			updateNote(delete_id.getText().toString());
    		}
    		if (arg0 == delete) {
    			// delete note with Id
    			deleteNote(delete_id.getText().toString());
    		}
    		if (arg0 == showNotes) {
    			// show all
    			getNotes();
    		}
    	}
    
    	private void makeToast(String text) {
    		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    	}
    
    }
    

    You can download the complete source code from this demo here.