Wednesday, April 16, 2014

Android: Show notification example


Show notification:

        Intent intent = new Intent(context, ExampleReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Notification n;
        Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            n = new Notification.Builder(context)
                    .setContentTitle("title")
                    .setContentText("Short message\nContent message")
                    .setTicker("ticker message")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setLargeIcon(bm)
                    .setStyle(new Notification.BigTextStyle().bigText("Long message.\nTesting..Testing..\nlooooooooooooooooon message"))
                    .setContentIntent(paramIntent)
                    .setAutoCancel(true).build();
        } else {
            n = new Notification.Builder(context)
                    .setContentTitle("title")
                    .setContentText("Short message\nContent message")
                    .setTicker("ticker message")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setLargeIcon(bm)
                    .setContentIntent(paramIntent)
                    .setAutoCancel(true).getNotification();
        }
        n.flags = n.flags | Notification.FLAG_NO_CLEAR;
        notificationManager.notify(0, n);


Show only the ticker and make it go disappear:

        Intent intent = new Intent(context, ExampleReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Notification n;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            n = new Notification.Builder(context)
                    .setTicker("ticker message")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setAutoCancel(true)
                    .build();
        } else {
            n = new Notification.Builder(context)
                    .setTicker("ticker message")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setAutoCancel(true)
                    .getNotification();
        }
        notificationManager.notify(0, n);
        notificationManager.cancel(0);


How to uninstall through adb command when multiple devices are connected

Sometimes when you have a physical device and an emulator connected to your computer, adb command doesn't work.

This will display serial number of connected devices:
adb devices -l









Now knowing the serial number of  my physical device (in this case: 8d5351c6062aa124), type adb uninstall command:

adb -s (serial number) uninstall (com.package.name)

in my case:
adb -s 8d5351c6062aa124 uninstall (com.package.name)

Monday, April 14, 2014

How to outsource/freelance Android app icon design for a cheap price?

You make apps and you have a terrible icon so no one downloads your app. Here's a solution:

Here's what I've learned (so far).

Fiverr
The cheapest place to do is fiverr.
Watch out! Not everything is $5.
They make you add this and that feature for extra $5~$20. But if you search carefully, some designers satisfies all your needs in $5.

Warning: Make sure you provide EVERYTHING you want your icon to be like. Don't expect them to read your mind and receive an awesome icon. Sometimes you have no idea what kind of icon is suitable. Even so, do some research because most likely they don't spend time to do research either. You end up, wasting your time and their time, and your money. (Been there, done that).

Provide either:

  • A pencil sketch of expected icon (take a photo and send to the designer)
  • A link or screenshot of an existing icon (maybe some other competitor icon) and tell them you want this style.
  • A detail description. (Color of the icon, add a shadow effect or flat icon, etc etc)
You still have a chance to go back and forth and request modification until you're satisfied but you should provide detail expectation ahead of time so that you don't waste the designer's time.

In my experience, when I requested for an awesome icon and explained what my app does, they made me a crappy icon. I had to provide EXACTLY what to do, they did a good job.

It seems you pay hourly, I haven't done it.

You provide a description and multiple designers make an icon for you. You simply pick the best one and give the money to the winner. The benefit is that you can compare and pick the best icon that you like. Not cheap so I haven't done it.

Use license-free free icons. See my 'Where to get a free Android icons?'



Please add a comment if you know other places.

Where to get a free Android icons?

Here's the list of places you can search for the free icons.
Make sure you set filter for "Commercial free" if your app is 'paid' app or has an 'ad' (you make some sort of profit)

http://www.iconarchive.com/
https://www.iconfinder.com/
http://findicons.com/
http://iconizer.net/en/free_icons

You can also search 'Google image' with "(search word) + icon" and set 'Labeled for reuse' filter.



Please add a comment if you know other places.


Wednesday, March 27, 2013

Android: How to compare Drawables


Answer:

  drawable1.getConstantState().equals(drawable2.getConstantState());

It doesn't compare actual data (bytes of images) so it is fast and memory efficient.

Monday, March 25, 2013

Android: How to use Android default font size example

Small:
<style name="DefaultMedium">

<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>

</style>


Medium:

<style name="DefaultMedium">

<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>

</style>

Large:

<style name="DefaultMedium">

<item name="android:textAppearance">?android:attr/textAppearanceLarge</item>

</style>

Reference: Android source code

Android Styles.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml

Android Res files
https://github.com/android/platform_frameworks_base/tree/master/core/res/res

Friday, March 22, 2013

Android: Full Screen & No title/action bar example

No Action bar:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Caution: this code has to be written BEFORE setContentView(...); or it will crash!

Full Screen:
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.layout_main);

}

Android: Question mark only in strings.xml resource example

Use backslash:

    <string name="quick_question">\?</string>

Thursday, March 21, 2013

Android: Get screen width programmatically example

It returns number in pixel





      @SuppressLint("NewApi")
      private int getScreenWidth() {
            int iMeasuredwidth = 0;
            Point size = new Point();
            WindowManager w = getWindowManager();
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
                  w.getDefaultDisplay().getSize(size);
                  iMeasuredwidth = size.x;
            }else{
                  Display d = w.getDefaultDisplay();
                  iMeasuredwidth = d.getWidth();
            }
            return iMeasuredwidth;
      }


Android: How to convert from dp to px? (example)

From dp to pixel:
 
private static int fromDp2Px(int dp, Context context) {
final Resources res = context.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
return (int) px;
}
 
or
 
private static int fromDp2Px(int dp, Context context) {
final Resources res = context.getResources();
DisplayMetrics metrics = res.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
}
 
 
 
 
From pixel to dp:
 
private static int fromPx2Dp(int px, Context context) {
final Resources res = context.getResources();
DisplayMetrics metrics = res.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return (int) dp;
}