Drawable bottom line

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-6dp" android:left="-6dp" android:right="-6dp">
        <shape> 
            <solid android:color="#2b7996"/>
            <stroke android:color="#33b5e5" android:width="6dp"/>
        </shape>
    </item>
</layer-list>

Continue Reading

Passing bitmap to bundle

I found this somewhere in Internet and I thought it would be useful someday

To pass bitmap to bundle, first we ought to convert it to byte array. Then the rest goes straight.

public static byte[] toByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

public static Bitmap fromByteArray(byte[] byteArray) {
    return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}

 

Continue Reading

Helper to compare i.e two months in different years

This method will help in comparing two dates by trimming date format to our specific format. So we avoid unnecessary data.

Sample:

We compare two months in different years. First date is:

2017-10-10 10:00

First we convert it to milliseconds using `SimpleDateFormat`.

We want know only time for

2017-10

So after we get millisecond, we can convert it to another one `yyyy-MM-dd` using code below:

public long getTimeInMillis(long milliSeconds, String dateFormat) {
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat, Locale.forLanguageTag("pl"));
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);
    String format = formatter.format(calendar.getTime());

    long milli = milliSeconds;

    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.forLanguageTag("pl"));
    try {
        Date parse = sdf.parse(format);
        Calendar c = Calendar.getInstance();
        c.setTime(parse);
        milli = c.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return milli;
}
Continue Reading

Utils class: compression string gzip

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Compression {
    public static byte[] compress(String paramString) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(paramString.length());
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(paramString.getBytes());
        gzipOutputStream.close();
        byte[] bytes = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        return bytes;
    }

    public static String decompress(byte[] paramArrayOfByte) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, 32);
        StringBuilder stringBuilder = new StringBuilder();
        byte[] bytes = new byte[32];
        for (; ; ) {
            int i = gzipInputStream.read(bytes);
            if (i == -1) {
                break;
            }
            stringBuilder.append(new String(bytes, 0, i));
        }
        gzipInputStream.close();
        byteArrayInputStream.close();
        return stringBuilder.toString();
    }
}

 

Continue Reading

Difference standard, singleTop, singleInstance, singleTask

standard (default) :- Multiple instances of the activity class can be instantiated and multiple instances can be added to the same task or different tasks. This is the common mode for most of the activities.

singleTop :- The difference from standard is, if an instance of the activity already exists at the top of the current task and the system routes the intent to this activity, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object.

singleTask:- A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. This mode is useful for activities that act as the entry points.

singleInstance:- Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance’s. Accordingly, the activity with launch mode is always in a single activity instance task. This is a very specialized mode and should only be used in applications that are implemented entirely as one activity.

source: http://stackoverflow.com/questions/25773928/setting-launchmode-singletask-vs-setting-activity-launchmode-singletop

To translate for my language:

We have 3 activities A>B>C and we are now in C

  1. standard  – creates everytime new activity. There is nothing to explain.
  2. singleTop – when we are in C and we want startActivity A it will create another A, but when we startActivity C it won’t create it because C is on top
  3. singleTask – when we are in C and we want startActivity A, it will bring to front A, but when we startActivity C it won’t create it because C is the current task
  4. singleInstance – imho don’t know 😀 above explanation might help a bit
Continue Reading
1 2 3 9