Wednesday, June 3, 2015

Android maximum zoom in Google Map |Android show all markers in Google Map | Android Google Map version 2 with max zoom to show all markers | LatLngBuilder example in Android Google Map

Hi Friends, again I am back with interesting post "How to show all the marker pins in google map in one time?" Are you looking for code to do this? follow given below step-


1)Create new project with blank activity
2)Import Play services lib project inside your project
3)Go to activity_main.xml and create MapFregment like this-
<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="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
4)Go to MainActivity.java and paste below code-

package com.androoidhub4you.googlemapmaxzoom;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdate;
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.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* @author manish
*
*/
public class MainActivity extends Activity {
private GoogleMap map;
LatLngBounds.Builder builder;
CameraUpdate cu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**get the reference of map from layout*/
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
/**call the map set up method*/
mSetUpMap();
}
/**create method to set map view*/
public void mSetUpMap() {
/**clear the map before redraw to them*/
map.clear();
/**Create dummy Markers List*/
List<Marker> markersList = new ArrayList<Marker>();
Marker Delhi = map.addMarker(new MarkerOptions().position(new LatLng(
28.61, 77.2099)).title("Delhi"));
Marker Chaandigarh = map.addMarker(new MarkerOptions().position(new LatLng(
30.75, 76.78)).title("Chandigarh"));
Marker SriLanka = map.addMarker(new MarkerOptions().position(new LatLng(
7.000, 81.0000)).title("Sri Lanka"));
Marker America = map.addMarker(new MarkerOptions().position(new LatLng(
38.8833, 77.0167)).title("America"));
Marker Arab = map.addMarker(new MarkerOptions().position(new LatLng(
24.000, 45.000)).title("Arab"));
/**Put all the markers into arraylist*/
markersList.add(Delhi);
markersList.add(SriLanka);
markersList.add(America);
markersList.add(Arab);
markersList.add(Chaandigarh);
/**create for loop for get the latLngbuilder from the marker list*/
builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
/**initialize the padding for map boundary*/
int padding = 50;
/**create the bounds from latlngBuilder to set into map camera*/
LatLngBounds bounds = builder.build();
/**create the camera with bounds and padding to set into map*/
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
/**call the map call back to know map is loaded or not*/
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
/**set animated zoom camera into map*/
map.animateCamera(cu);
}
});
}
}
5)Go to your Manifest.xml and make changes and  permissions-

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androoidhub4you.googlemapmaxzoom"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<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" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<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>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDtKRBFRrpjshqoFfCDq-oalPbbgx4XNBs" />
</application>
</manifest>

Download zip code

Thanks,
Manish



Tuesday, May 5, 2015

Android Watch Demo | Google Watch | Android Watch Time Demo | Creating Wearable Apps in Android Google

Hello Friends, today I am going to share steps to create a simple Google watch demo to show current time and date using java.util-

1)Open Android Studio IDE and update it to API Level 20.
2)Create New Project
3)Choose Android Wear project
4)Now by default IDE will create MainActivity.java and activity_main.xml, rect_activity_main.xml, round_activity_main.xml
5)Now open your MainActivity.java and set text value-
 mTextView.setText(String.valueOf(new Date()));






Full Code:
1)MainActivty.java

package demo.watch.androidhub4you.com.androidwatch;

import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.widget.TextView;
import java.util.Date;

public class MainActivity extends Activity {

    private TextView mTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
                mTextView.setText(String.valueOf(new Date()));
            }
        });
    }
}

2)activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/watch_view_stub"
    android:layout_width="match_parent" android:layout_height="match_parent"
    app:rectLayout="@layout/rect_activity_main" app:roundLayout="@layout/round_activity_main"
    tools:context=".MainActivity"
    tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>

 

3)round_activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"  tools:deviceIds="wear_round">

    <TextView android:id="@+id/text" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" android:text="@string/hello_round" />
</RelativeLayout>

 

4)rect_activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".MainActivity" tools:deviceIds="wear_square">

    <TextView android:id="@+id/text" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/hello_square" />
</LinearLayout>

Thanks,
Manish




Thursday, April 2, 2015

Android App Widgets| App Widgets in Android | Create App Widgets in Android

Hello Friends,

Today I am here with new learning "how to create widgets for your application"
like you see calender, calculator, Facebook, whether widgets on your phone in widget section.
It is very simple to create your own app widget, follow below step to make it done-




1)Create a demo project i.e.- AppWidgetDemo

2)Create a Widget Provider for  your application i.e.- MyProvider.java


package com.androidhub4you.app.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class MyProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        // Get all component
        ComponentName thisWidget = new ComponentName(context, MyProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {

            //set the remote view to widget
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.activity_my_widget);
            // Set the text to textview you change it in runtime
            remoteViews.setTextViewText(R.id.my_text, "Manish");

            // Intent to set dynamic vaalues to views
            Intent intent = new Intent(context, MyProvider.class);

            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.my_text, pendingIntent);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
}

3)Create a widget layout for your app inside res/layout/activity_my_widget.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:background="@drawable/my_shape"
    tools:context="com.androidhub4you.app.widget.MainActivity" >

    <ImageView
        android:id="@+id/my_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/widget_logo" />

    <TextView
        android:id="@+id/my_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_image"
        android:layout_margin="2dp" />

</RelativeLayout>

4)Create an appwidget-provider inside res/xml/my_widget.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/activity_my_widget"
    android:minHeight="60dp"
    android:minWidth="400dp"
    android:updatePeriodMillis="200000" >

</appwidget-provider>

5)For the good look for you widgets create a shape inside res/drawable/my_shape.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="@android:color/white" />

    <gradient
        android:angle="225"
        android:endColor="@android:color/background_dark"
        android:startColor="@android:color/background_light" />

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

</shape>

6)and inside your manifest.xml call your reciver-

   <receiver
            android:name="MyProvider"
            android:icon="@drawable/widget_logo"
            android:label="My Widget Demo" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/my_widget" />
        </receiver>


Reference URL-
https://developer.android.com/guide/topics/appwidgets/index.html

DOWNLOAD FULL CODE


Thanks,
Manish