Saturday 30 November 2013

Install APK Files in Emulator By cmd

Hi Friends,

Here i am going to explain you how to install APK file in Emulator by cmd. follow mention below steps.


  1. First of all run Emulator
  2. Locate adb.exe file in sdk which is normally present in C:\adt-bundle-windows-x86-20130717\sdk\platform-tools.
  3. Paste your APK file in platform-tools folder.
  4. Run Command Prompt (eg. Start -> Programs -> Accessories -> Command Prompt).
  5. Navigate to platform-tools folder in Command Prompt (eg. C:\adt-bundle-windows-x86-20130717\sdk\platform-tools>).
  6. Type adb install myapp.apk press Enter.

That's it, this will install your application in Emulator.

Enjoy Developing Android Apps ...................... 




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Thursday 28 November 2013

Create txt File And Store In SDCard

Hi Friends,

Here i am going to explain how to create a txt file and stored into in SD card of mobile phone, checkout the following codes:

Example:





Codes:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create txt file" />

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.createtxtfileinsdcard;

import java.io.File;
import java.io.FileWriter;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

Button create;
FileWriter fWriter;

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

create = (Button)findViewById(R.id.button1);

create.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

       try{
            fWriter = new FileWriter(Environment.getExternalStorageDirectory()+ File.separator + "File.txt");
            fWriter.write("Contents of the File");
            fWriter.flush();
            fWriter.close();
            Toast.makeText(MainActivity.this, "File Created in SD Card", Toast.LENGTH_SHORT).show();
        }catch(Exception e){
                 e.printStackTrace();
        }

}
});


}


}


Manifest

Add permissions 

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

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


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Wednesday 27 November 2013

Fetching All Contacts Details From Phonebook

Hi Friends,

Today i am going to explain you how we can fetch all contacts information from phonebook in our application. Follow mention below steps :

Examples:




Code:

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/whitecolor" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
   <TextView
       android:id="@+id/textView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       android:layout_margin="10dp"
       android:gravity="center"
       android:text="Contacts Information"
       android:textSize="20dp" />

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginLeft="10dp"
       android:layout_marginRight="10dp"
       android:layout_marginTop="18dp" />
        
    </LinearLayout>

</ScrollView>


MainActivity.java

package in.androiddevelopmentanddiscussion.getallcontactlist;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView outputText;
background bg;
ProgressDialog pg;
StringBuffer output;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputText = (TextView) findViewById(R.id.textView1);

bg = new background();
bg.execute();
}

public void fetchContacts() {

String phoneNumber = null;
String email = null;

Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

Uri EmailCONTENT_URI =  ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
String DATA = ContactsContract.CommonDataKinds.Email.DATA;

output = new StringBuffer();

ContentResolver contentResolver = getContentResolver();

Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);

// Loop for every contact in the phone
if (cursor.getCount() > 0) {

while (cursor.moveToNext()) {

String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));

int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));

if (hasPhoneNumber > 0) {

output.append("\nName:" + name);

// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);

while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
output.append("\nNumber:" + phoneNumber);

}

phoneCursor.close();

// Query and loop for every email of the contact
Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null);

while (emailCursor.moveToNext()) {

email = emailCursor.getString(emailCursor.getColumnIndex(DATA));

output.append("\nEmail:" + email);

}

emailCursor.close();
}
output.append("\n");

}


}
}


public void progress(){
pg = new ProgressDialog(MainActivity.this);
pg.setTitle("");
pg.setMessage("Please Wait.........");
pg.setCancelable(false);
        pg.setIndeterminate(true);
pg.show();
}


class background extends AsyncTask<Void, Void, Void>{

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
outputText.setText("Please wait........");
fetchContacts();
return null;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
pg.dismiss();
outputText.setText(output);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress();

}

}


}


Manifest

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




Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Sunday 17 November 2013

Turn On GPS Programmatically

Hi Friends,

Here i am going to show you how we can start GPS pro-grammatically.

Example:






Codes :

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:text="Turn on GPS Programmatically"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="50dp"
            android:text="Start GPS" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Stop GPS" />
    

</LinearLayout>

MainActivity.java

package in.androiddevelopmentanddiscussion.turningongpsprogrammatically;

import com.example.turningongpsprogrammatically.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button start,stop;


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

start = (Button)findViewById(R.id.button1);
stop = (Button)findViewById(R.id.button2);

start.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
       intent.putExtra("enabled", true);
       sendBroadcast(intent);
}
});     
        
stop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
       intent.putExtra("enabled", false);
       sendBroadcast(intent);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

Manifest

Add below permissions in Manifest

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


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Check Internet Connection

Hi Friends,

Here i am going to show you how we can check internet connection in Android. 

Example:







Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="50dp"
        android:text="Check Internet Connection"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.checkinternetconnection;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

Button btn;
TextView result;
final Context context = this;

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

btn = (Button)findViewById(R.id.button1);
result = (TextView)findViewById(R.id.textView1);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();

       if (networkInfo != null && networkInfo.isConnected()) {
        result.setText("Internet Connection Available");
       }
       else{
        result.setText("Internet Connection Not Available");
       }

}
});

}


}


Manifest

Add below permission in Manifest

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


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Disable App Screen Rotation/Orientation

Hi Friends,

Here i am going to show you how to stop/disable screen rotation/orientation of your application in Android while Auto Rotation is enabled in your mobile.

For this just you have to made changes in Activity tag of your Application Manifest :


 <activity
android:name="in.androiddevelopmentanddiscussion.disablescreenrotationexample.MainActivity"
android:label="@string/app_name" 
android:screenOrientation="portrait">

If you want you application activity in Landscape then just change orientation "portrait" to "landscape".

That's it Enjoy Coding ............ :)


Example: 

Landscape Screen


Portrait Screen



Code:

activity_main.xml


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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


MainActivity.java


package in.androiddevelopmentanddiscussion.disablescreenrotationexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


Manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.androiddevelopmentanddiscussion.disablescreenrotationexample"
    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="in.androiddevelopmentanddiscussion.disablescreenrotationexample.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">  (Change it Accordingly)
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Wednesday 13 November 2013

Google Map Example

Here i am going to explain working on Google Map V2 in android, before starting we need to go through some pre required steps. These steps involves importing Required Library, Generating SHA1 Fingerprint, Configuring Maps in Google Console and Importing google_play_services_lib in project.

1. Required Library

Before started with Google Map we need to install Google Play Services from Android SDK Manager

Open EclipseWindowsAndroid SDK Manager and check whether you have already downloaded Google Play Services or not under Extras section. If not select play services and install the package.



2. Generating SHA1 Fingerprint


After Installing Google Play Services, you have to generate SHA1 key, this is a certificate fingerprint for Google Map API, follow below step to generate SHA1 key


Open cmd to generate SHA1 key


Press start => Run => type 'cmd' and press Enter


Now navigate to following folder 



C:\Program Files\Java\jdk1.7.0_09\bin>

Then type


keytool.exe -list -v -alias androiddebugkey -keystore "C:\Documents and Settings\User\.android\debug.keystore" -storepass android -keypass android


Press Enter, It will generate SHA1 key copy it and save it to another place we can use this key after some time



3. Configuring Maps in Google Console

After getting SHA1 key we have to generate Google Map API Key form API Console - Google follow bellow steps

Open browser and go to www.google.com => search API Console - Google  this will show you following result:





Click first link of the search this will ask you to login with your gmail account



After login this will ask you to create project 



Click on API Project and click on Create



Name your Project




 Activate Google Map Android API v2



Navigate to API Access and click on Create New Android Key



Now Put your SHA1 Key and Your Project package path and click on Create





Now you have a final Google Map API Key 





4. Importing google_play_services_lib

After getting Google API Key we are back to Eclipse now we have to include google_play_services_lib in our project follow the steps

a. In Eclipse goto File ⇒ Import ⇒ Android ⇒ Existing Android Code Into Workspace

b. Click on Browse and select Google Play Services project from your android sdk folder. You can locate play services library project from

android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib

c. Importantly while importing check Copy projects into workspace option as shown in the below image.


After that Right Click on you project folder and navigate to Configure Build Path


Click on Add Class Folder


Select google_play_services_lib => ok



Click on Android after that click on Apply to save setting 



Select Google API 16 (You can select as per your choice but it should be Google API)



Select google_play_services_lib => ok



Click Apply and then click on Ok



Its done with all the external steps now we need to create a code for Google Map 

Example :















Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/click"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="18sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="GPS"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="80dp"
        android:text="Check Location on Map"
        android:textStyle="bold"
        android:visibility="invisible" />

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.googlemapexample;


import android.location.Location;

import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {


Button gpsbtn,viewbtn;

TextView output;
LocationManager lm;
LocationListener ll;
Location loc;

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

gpsbtn = (Button)findViewById(R.id.button1);
viewbtn = (Button)findViewById(R.id.button2);
output = (TextView)findViewById(R.id.textView1);
 
lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

gpsbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
output.setText("Please wait......");
}
});

viewbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
double lng,lat;
lng = loc.getLongitude();
lat = loc.getLatitude();

Intent i = new Intent();
i.putExtra("longitude",lng);
i.putExtra("latitude",lat);
i.setClass(MainActivity.this,MapActivity.class);
startActivity(i);
finish();
}
});

ll = new LocationListener() {

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {
showSettingsAlert();
}

@Override
public void onLocationChanged(Location location) {

loc = location;

output.setText("Latitude :"+String.valueOf(location.getLatitude())+"\n Longitude :"+String.valueOf(location.getLongitude()));

gpsbtn.setVisibility(View.INVISIBLE);

viewbtn.setVisibility(View.VISIBLE);



}};

}

public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

alertDialog.setTitle("GPS Settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}});

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
            }});

alertDialog.show();

}


}


activity_map.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button

            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="Hybrid"
            android:textStyle="bold" />

        <Button

            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="Satellite"
            android:textStyle="bold" />

        <Button

            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Normal"
            android:textStyle="bold" />

    </LinearLayout>

    
     <fragment
         android:id="@+id/fragment1"
         android:name="com.google.android.gms.maps.MapFragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />


</LinearLayout>


MapActivity.java

package in.androiddevelopmentanddiscussion.googlemapexample;


import java.io.IOException;

import java.util.List;
import java.util.Locale;

import com.google.android.gms.maps.CameraUpdateFactory;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Address;

import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MapActivity extends Activity {


Double longitude,latitude;

GoogleMap map;
String strCompleteAddress= "";
Button hybrid,satellite,normal;
final Context context = this;
Geocoder geoCoder;

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

Bundle extras = getIntent().getExtras();

longitude = extras.getDouble("longitude");
latitude = extras.getDouble("latitude");

hybrid = (Button)findViewById(R.id.button1);
satellite = (Button)findViewById(R.id.button2);
normal = (Button)findViewById(R.id.button3);
           
   
StrictMode.enableDefaults();

background bg = new background();
   bg.execute();
}

public void mapview()

{
try {
   geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
   
   List<Address> addresses = geoCoder.getFromLocation(latitude,longitude, 1);
   
   
   if (addresses.size() > 0)
   {
   for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
  strCompleteAddress+= addresses.get(0).getAddressLine(i);
   }
   Log.i("My Location => ", strCompleteAddress);
  
   }
   catch (IOException e) {
   Log.i("MyLocTAG => ", "this is the exception part");
   e.printStackTrace();
   strCompleteAddress = "IO Exception in getting Location";
   }   
}

class background extends AsyncTask<Void, Void, Void>

{
@Override
protected void onPreExecute() {
super.onPreExecute();

}


@Override

protected Void doInBackground(Void... params) {
mapview();
return null;
}

@Override

protected void onPostExecute(Void res) {
super.onPostExecute(res);

final LatLng mylocation = new LatLng(latitude,longitude);

   map = ((MapFragment)getFragmentManager().findFragmentById(R.id.fragment1)).getMap();

   map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
   
   hybrid.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
   
   satellite.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
});
   
   normal.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
});
   
   
   Marker hamburg = map.addMarker(new MarkerOptions().position(mylocation).title("Current Location :"));
   map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocation, 15));
   map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
   map.getUiSettings().setCompassEnabled(true);
   hamburg.setSnippet(strCompleteAddress);


}



}


}


Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.androiddevelopmentanddiscussion.googlemapexample"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <permission
    android:name="in.androiddevelopmentanddiscussion.googlemapexample.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="in.androiddevelopmentanddiscussion.googlemapexample.permission.MAPS_RECEIVE"/>
    
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

    <application  
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:hardwareAccelerated="true">
        
     <meta-data
      android:name="com.google.android.maps.v2.API_KEY"
      android:value="Put Your API Key"/>
        
        <activity
            android:name="in.androiddevelopmentanddiscussion.googlemapexample.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>
        <activity
            android:name="in.androiddevelopmentanddiscussion.googlemapexample.MapActivity"
            android:label="@string/title_activity_map" >
        </activity>
    </application>

</manifest>


Download full project from here




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en