Monday, 14 September 2015

Always Visible Multiple InfoWindows on Google Map



This post is about resolution of an issue that I faced when I started working on a requirement in which multiple info windows were supposed to be shown and that too simultaneously.

Conventionally there is no such method/class in Google maps api that could allow developers to implement this functionality. Though we can add multiple markers default or customized with an attached infowindow but cannot show all the infowindow simultaneously.


Prerequisite : 
Get google map key for your map to be launched correctly. Please following link to create one
https://developers.google.com/maps/documentation/android-api/signup


Alternative:
Whatever. Above feature is highly needed along with autoupdating the content on the info window.
Never knew Markers were enough to replicate it into an infowindow too.



Conception: 
We will be going to create custom Markers in which we will draw all the information as bitmap and then set it to the markers icon property using

customMarkerOption.icon(BitmapDescriptorFactory.fromBitmap(createMarkerBitmap("76.3, 23.4 75%"))));


where customMarkerOption is an instance of MarkerOptions and createMarkerBitmap() is the method that creates bitmap using the content to be shown on map.



private Bitmap createMarkerBitmap(String iMarkerText) {
         Bitmap.Config conf = Bitmap.Config.ARGB_8888;
         Bitmap bmp = Bitmap.createBitmap(200, 100, conf);
         Canvas canvas = new Canvas(bmp);
 
         canvas.drawRect(0,75,200,175, mMarkerContainerPaint);
         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.purple_marker),100,25,mMarkerTextPaint);
         canvas.drawText(iMarkerText, 50, 100, mMarkerTextPaint);
 
         return bmp;
     }


You can set up the map and use MarkerOptions instances above to create markers on map. And the content will also update automatically.

You can get the complete source for the demo app here.
MapDemos


Please check and let me know in case of issues.

Tuesday, 23 June 2015

Issues with Animators


What are animators ? Why do we need them ? What is the difference with the conventional view animations ? How animators are implemented in android ?


Well today in this post I will not talk about these, instead I will put light on some issues that I faced while working and came to know about the reason very late.

The main issues that I noticed are :

1. I cannot reuse an ObjectAnimator and AnimatorSet object to get it usable for multiple views.
2. I cannot clear animation from my view. I mean resetting the view to its original position.

So after delving deeply into the android developer docs I reached to the following solutions at the time of writing :

1. Clone the create ObjectAnimator and AnimatorSet object to create a completely different copy of that object which could be used easily with a new view.
2. To get the reset functionality for animator set, we should be creating an animator that animates the same property to back to its original state(reverse action of the animator).


Please comment in case of issues :

Wednesday, 1 April 2015

Simplest steps to integrate Parse for push notifications in Android

This blog entry is about how to fastly integrate Parse for push notifications in android. It only provide information on how to get started with the Parse's sample and the flow for most basic configurations.

Prerequisites : 

Signup into the www.parse.com. Create a project which will generate few keys like APPLICATION ID and CLIENT KEY to be used later in the project while integrating sdk.

Integration and Configuration:

1. Make a quickstart by navigating to this url https://www.parse.com/apps/quickstart
2. Select the Push options.
3. Select Android
4. Out of the two options : Create a new project and Add to existing project. Choose what you want to do ?

5. Choosing New Project will take you to https://www.parse.com/apps/quickstart#parse_push/android/new
Follow the steps on this page to make respective changes in the source files : Well you can find this step defined in the app properly

5.1 Download the the project and import it in the android studio.
5.2 We will need to connect to parse. This will require us to edit some of the files in the project.

5.2.1 Edit the Application class's onCreate() method

             public void onCreate() {
                // Replace the APPLICATION ID and CLIENT KEY with the one created for a project
                Parse.initialize(this, "APPLICATION ID", "CLIENT KEY");
             }

5.2.2 Add the following service and broadcast receiver definitions to AndroidManifest.xml immediately before the closing </application> tag:


<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.USER_PRESENT" />
  </intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
    android:exported="false">
  <intent-filter>
    <action android:name="com.parse.push.intent.RECEIVE" />
    <action android:name="com.parse.push.intent.DELETE" />
    <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
 
    <!--
      IMPORTANT: Change "com.parse.starter" to match your app's package name.
    -->
    <category android:name="com.parse.starter" />
  </intent-filter>
</receiver>


Change the android:name attribute of <category> element above to match your application's package name.

Also add the permissions below, typically immediately before the opening <application> tag:


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 
<!--
  IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
  to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
    android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />


Change the android:name attribute of <category> element above to match your application's package name.

5.2.3 Compile and run.

6. Similarly follow all the substeps just as in step 5 to do the Parse integration in android sdk.


Testing for push notifications : 

OK, now you are done with the most basic integration so you are good to go for testing.

7. Navigate to https://www.parse.com/apps to open dashboard containing all your projects created till now. Select the current project and the select the "Push" option.

7.1 Now here you will find an option/button to send a push message. You can also see the notification history if any notifications send previously.
7.2 Once selected one can also find a box to create a message to be sent. And the settings to choose the devices that can receive the notifications.
7.3 Also you can update the time of delivery of notification
7.4 Once message is configured properly, it can be pushed to the devices.


8. If everything is fine, app will receive the notifications and on clicking notification it will launch the application with the launcher activity. 

Ok ,you are done here with the integration of Parse for push notifications in android. 
Wait .... We are missing something .... ?
Don't you want to use one of the icon for the notification from your app reserve rather than default icon ? I know you are dying to add .
Don't you want to an implementation specific to your app like an desired activity to be launched. Yes you always wanted to drive
So we will do it in our app by creating our app's version of class com.parse.ParsePushBroadcastReceiver which allows to customize notification and implementation on receiving push message.



public class MyReceiver extends ParsePushBroadcastReceiver {
  
     @Override
     protected Notification getNotification(Context context, Intent intent) {
         final Notification notification = super.getNotification(context, intent);
  // TODO Update this notification here          
         return notification;
     }
 
     @Override
     protected void onPushOpen(Context context, Intent intent) {
  //TODO For app specific implementation comment the following line calling super version of this method  
  // super.onPushOpen(context, intent);

  //TODO Add App Specific implementation below
         Intent lastIntent = new Intent(context,SecondActivity.class);
         lastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(lastIntent);
     }
}



This is just a brief walk through of Parse for push notifications in android. It is a very big thing in itself. Parse is a collection of multiple sdks for various platform.

References: https://www.parse.com/

Source: https://github.com/Keshava11/parsecheck