Saturday, September 24, 2016

Google Map Android

Important steps are given below-

1)Generate SHA1 certificate for your Google map from google developer account from below link-
https://code.google.com/apis/
2)Create new android project.
3)Copy paste below given activity and layout.
4)Add Internet permission in your manifest.
5)Put your map key in manifest.
7)Enable your Network connection or GPS.
8)Run your project and tap on map for draw circle and marker.
9)Long press for remove circle.
10)Put your comment for more help. 

(A) Print Screen :


(B) MainActivity.java :


package com.manish.googlemap;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.manish.googlemap.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
/**
 * @author manish
 *
 */
public class MainActivity extends FragmentActivity implements
  OnMapClickListener, OnMapLongClickListener{

 private GoogleMap googleMap;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  googleMap = fm.getMap();
  //display zoom map
     googleMap.moveCamera( CameraUpdateFactory.zoomTo(13.2f));
        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);
  
  googleMap.setOnMapClickListener(this);
  googleMap.setOnMapLongClickListener(this);
 
 }

 @Override
 public void onMapClick(LatLng point) {
  //add marker
  MarkerOptions marker=new MarkerOptions();
  marker.position(point);
  googleMap.addMarker(marker);
  //add circle
  CircleOptions circle=new CircleOptions();
  circle.center(point).fillColor(Color.LTGRAY).radius(1000);
  googleMap.addCircle(circle);
  }

 @Override
 public void onMapLongClick(LatLng point) {
  googleMap.clear();
 }
 

}

(C) 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" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        class="com.google.android.gms.maps.SupportMapFragment" />
     
</RelativeLayout>

(D) AndroidManifest.xml :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.googlemap.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="com.google.android.maps.v2.API_KEY"
            android:value="Put Your Map Key Here!" />
    </application>

</manifest>

Google Key Generation Android

  MD5 Certificate genration is needed for implement google map in our apllication so first we need to genrate md5 certificate.
We can generate it in different opperating systems-

Step1-

Windows Vista: C:\Users\<user>\.android\debug.keystore

Windows XP: C:\Documents and Settings\<user>\.android\debug.keystore

OS X and Linux: ~/.android/debug.keystore

Open console/terminal on to the above location where debug.keystore file is present and execute

"keytool -list -keystore debug.keystore"

Output will be like (press simply enter when password is asked)
=====================================================================
root@ZSS0057:~/.android# keytool -list -keystore debug.keystore
Enter keystore password:
*****************  WARNING WARNING WARNING  *****************
* The integrity of the information stored in your keystore  *
* has NOT been verified!  In order to verify its integrity, *
* you must provide your keystore password.                  *
*****************  WARNING WARNING WARNING  *****************

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

androiddebugkey, 11 Sep, 2012, PrivateKeyEntry,
Certificate fingerprint (MD5): 04:FB:63:04:D5:6B:12:92:9F:3E:20:9F:23:DD:B8:53

Step2-
copy MD5 Certificate fingerprint and paste it in below url-

https://developers.google.com/android/maps-api-signup

You will get Map Keys On successful signup. Put those in the MapView Element of your view.

Dynamic Swipe View Android

I am going to explore my first  sample application in android ,which gives you
an idea , How to create a Dynamic swipe view in Android.

Hope this will helps you......


1- Print Screen of Dynamic Swipe View


2-Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="swip.view.example"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
           </application>
</manifest>

3-MainActivity code


package swip.view.example;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
int cardRest;
String userRest;
String[] name;
Integer[] card;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;

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

ArrayList<Contact> Contact_data = new ArrayList<Contact>();
// fill hard coded data contact type in array list
Contact_data.add(new Contact(R.drawable.card1, "Ashish"));
Contact_data.add(new Contact(R.drawable.card2, "Avinash"));
Contact_data.add(new Contact(R.drawable.card3, "Ben"));
Contact_data.add(new Contact(R.drawable.card4, "Champ"));

name = new String[Contact_data.size()];
card = new Integer[Contact_data.size()];
for (int i = 0; i < Contact_data.size(); i++) {

name[i] = Contact_data.get(i).title;
card[i] = Contact_data.get(i).icon;
}

viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
for (int j = 0; j < Contact_data.size(); j++) {
TextView label = new TextView(this);
TextView value = new TextView(this);
ImageView imge = new ImageView(this);
WebView web = new WebView(this);
Button btn = new Button(this);

label.setText("Get your  world here!");
value.setText(name[j]);
imge.setImageResource(card[j]);
imge.setLayoutParams(new LinearLayout.LayoutParams(70, 70));
web.loadUrl("http://www.google.co.in/");
web.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 300));
btn.setText("Go To Facebook!");
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));


LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mLinearLayout.setOrientation(1);
LinearLayout mLinearLayout2 = new LinearLayout(this);

mLinearLayout2.setOrientation(0);
mLinearLayout.addView(mLinearLayout2);
mLinearLayout2.addView(imge);
LinearLayout mLinearLayout3 = new LinearLayout(this);

mLinearLayout3.setOrientation(1);
mLinearLayout2.addView(mLinearLayout3);
mLinearLayout3.addView(value);
mLinearLayout3.addView(label);
mLinearLayout.addView(web);
mLinearLayout.addView(btn);
viewFlipper.addView(mLinearLayout);


slideLeftIn = AnimationUtils.loadAnimation(this,
R.anim.slide_in_left);
slideLeftOut = AnimationUtils.loadAnimation(this,
R.anim.slide_out_left);
slideRightIn = AnimationUtils.loadAnimation(this,
R.anim.slide_in_right);
slideRightOut = AnimationUtils.loadAnimation(this,
R.anim.slide_out_right);

gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};

}
}

class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
} catch (Exception e) {
// nothing
}
return false;
}
}

// It is necessary to return true from onDown for the onFling event to
// register
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}
3-contact.java 
package swip.view.example;

public class Contact {
  public int icon;
    public String title;
    public Contact(){
        super();
    }
        public Contact(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }
}


4-Main.xml file
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ViewFlipper>

5-And create a folder name "anim" in res and add 4 xml file for left, right up, down swipe on touch screen.
i)res/anim/slide_in_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

ii)res/anim/slide_in_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

iii)res/anim/slide_out_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

iv)res/anim/slide_out_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

Dynamic Design Android

Print Screen for Application



2- DynamicUIDesigning.java

package hello.test.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class DynamicUIDesigning extends Activity {

String countryName[] = { "India", "Pakistan", "China", "Nepal", "Bangladesh" };

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic);

// Getting first layout from main.xml file
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
// create a loop how many time you want repeat the content
for (int k = 1; k < 5; k++) {

// create a base linear layout for linear1
LinearLayout mLinearLayout1 = new LinearLayout(this);
mLinearLayout1.setOrientation(0);
mLinearLayout.addView(mLinearLayout1);

// create dynamic text box and label
TextView title = new TextView(this);
title.setText("User Info-" + k);
// create name label
TextView name = new TextView(this);
name.setText("User Name:");
// create title2
TextView title2 = new TextView(this);
title2.setText("User Info-" + k);
// create city label
TextView city = new TextView(this);
city.setText("City of residence");
// create text box
EditText editName = new EditText(this);
editName.setText("User Name:");

// setting values of second layout in linear1
LinearLayout mLinearLayout2 = new LinearLayout(this);
mLinearLayout2.setOrientation(1);
mLinearLayout1.addView(mLinearLayout2);
mLinearLayout2.addView(title);
mLinearLayout2.addView(name);
mLinearLayout2.addView(editName);
// setting value of third layout in linear1
LinearLayout mLinearLayout3 = new LinearLayout(this);
mLinearLayout3.setOrientation(1);
mLinearLayout1.addView(mLinearLayout3);
mLinearLayout3.addView(title2);
mLinearLayout3.addView(city);

// create radio button
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);// or RadioGroup.VERTICAL
for (int i = 0; i < 5; i++) {
rb[i] = new RadioButton(this);
rg.addView(rb[i]); // the RadioButtons are added to the
// radioGroup instead of the layout
rb[i].setText(countryName[i]);

}
mLinearLayout3.addView(rg);
}
}
}


3-dynamic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearBase"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>

</LinearLayout>