Thursday, October 6, 2016

Save objects in Shared Preferences


private SharedPreferences preferences;
private SharedPreferences.Editor editor;

//Save objects
preferences = context.getSharedPreferences(namePreferences, mode);
editor = preferences.edit();
editor.putString(key, GSON.toJson(object));
editor.commit();

//Retrieve objects
String gson = preferences.getString(key, null);
Object o = GSON.fromJson(gson, a);

Tuesday, May 24, 2016

Calculate battery drain in your app

Interesting slide
 for each API call, it consumes 1/3 mAh
so if you just make API call a minute, you will drain the battery by 1/3 a day!


https://www.youtube.com/watch?v=QR3PIg0RDnk
#io16

Location, save battery and still have a great user experience!

Through patching data from Sensors (e.g. GPS, WiFi, Cell, Gyro, Magno, Acce) via FLP (Fused Location Provider) this leads to more battery saving as it sends those batched data at specific intervals which you can define according to your use case.
But if the user wants to know the exact location right now, they can immediately check it through Flushing callback which provides a seamless user experience.

Changes in Hardware (Lowest level of abstraction) makes User Experience much better (Highest level of abstraction)




Sources:
https://www.youtube.com/watch?v=OEvycEMoLUg
https://developers.google.com/awareness-location/

Thursday, March 17, 2016

Google Tag Manager and Analytics Integration

Firstly, we can say that you can send events from your app to GA directly. However, you can send events to GTM and then GTM will send those to analytics. Many reasons can be said why to use GTM over GA such as in here. But it always a Design/Need decision!

Anyhow, For Android, A draft implementation is at https://developers.google.com/tag-manager/android/v4/#getting-started

Check it, you may have some questions, you would find the answers in here I hope. So those are some of the lessons I learnt:
- Event Names and keys at Android code should match the corresponding ones at GTM.
dataLayer.pushEvent("openScreen",      DataLayer.mapOf("screenName", screenName));
- If you send Events from GTM to GA, the "value" field should be always Integer . if not, it will not be sent
public static void pushEvent(Context context, String event,String category, String label, Integer labelValue) {
        DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
        dataLayer.pushEvent(event, DataLayer.mapOf("eventCategory", category,"eventLabel", label, "eventValue", labelValue);
    }

- Configure GTM with Variables, Triggers, and Tags. as described here: https://developers.google.com/tag-manager/android/v4/ua#create-transaction-tag

- Use pushEvent rather than push. It is easier!

Thursday, March 10, 2016

Wednesday, March 9, 2016

Android font and caligraphy



So if you want to set an external font to a text view, you have different options:

- A common step: Get the font and put it under assets/fonts folder (create it if it not already created)

1 - Use this Gist:
Typeface typeface = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");
TextView textview = (TextView) findViewById(R.id.textview);
textview.setTypeface(typeface); 
2 - Use DataBinding Lib as described in here: fontbinding
3- Use XML attributes through custom widget: this is a good blog covering it all Custom fonts