Showing posts with label Utils. Show all posts
Showing posts with label Utils. Show all posts

Friday, 7 April 2017

Change color of a material icon - Android

17:27:00 Posted by Kumanan , ,
public final class DrawableUtils {

    public static Drawable applyColorFilter(@NonNull Context context, @DrawableRes int drawableId, @ColorRes int colorId, @NonNull PorterDuff.Mode mode) {
        Drawable d = ContextCompat.getDrawable(context, drawableId);
        int color = ContextCompat.getColor(context, colorId);

        d.setColorFilter(color, mode);
        return d;
    }

    public static Drawable applyColorFilter(@NonNull Context context, @DrawableRes int drawableId) {
        return applyColorFilter(context, drawableId, R.color.colorPrimaryDark, PorterDuff.Mode.SRC_ATOP);
    }
}

Check wheather Service is running or not

17:25:00 Posted by Kumanan ,
Create a Class in a name of Services :

public class Services {

    public static boolean isMyServiceRunning(Activity mActivity, Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) mActivity.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}


In Your Activity :

  if (Services.isMyServiceRunning(ActivityName.this, ServiceName.class)) {
     // Write your condition here if service is running
  }

Get Time updated with an interval

17:19:00 Posted by Kumanan ,

public static String getUpdateTime(String updatedTime, int minute) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long t = c.getTimeInMillis();
        Date date = null;
        try {
            date = df.parse(updatedTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long millis = date.getTime();

        Date afterAddingTenMins = new Date(millis + (minute * ONE_MINUTE_IN_MILLIS));
        String formattedDate = df.format(afterAddingTenMins);

        return formattedDate;
    }

To Get Time difference between 2 times

17:16:00 Posted by Kumanan ,
// TODO Time difference between 2 times

    public static boolean getTimeDifference(String current, String updatedTime) {

        Log.e("Pixel Current Time", "-->" + current);
        Log.e("Pixel Updated Time", "-->" + updatedTime);

        boolean timeUpdated = false;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = format.parse(current);
            Date date2 = format.parse(updatedTime);

            long difference = date2.getTime() - date1.getTime();

            if (date1.after(date2)) {
                timeUpdated = true;
            } else if (date1.before(date2)) {
                timeUpdated = false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return timeUpdated;
    }

Check time between 2 times

17:14:00 Posted by Kumanan ,
// Check Time between 2 times

public static boolean getWorkingHours(String currentTime) {
        boolean isWorkingHour = false;
        try {
            String string1 = "09:00:00";
            Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(time1);

            String string2 = "18:00:00";
            Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2);
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(time2);
            calendar2.add(Calendar.DATE, 1);

//            String someRandomTime = "01:00:00";
            Date d = new SimpleDateFormat("HH:mm:ss").parse(currentTime);
            Calendar calendar3 = Calendar.getInstance();
            calendar3.setTime(d);
            calendar3.add(Calendar.DATE, 1);

            Date x = calendar3.getTime();
            if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
                //checkes whether the current time is between 14:49:00 and 20:11:13.
                isWorkingHour = true;
                Log.e("", "==========> Is Woking Hour");
            } else {
                Log.e("", "==========> Is Not Woking Hour");
                isWorkingHour = false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return isWorkingHour;
    }

Wednesday, 9 March 2016

Check for Valid Mobile Number

10:55:00 Posted by Kumanan
private static boolean isValidPhoneNumber(String mobile) {
    String regEx = "^[0-9]{10}$";    return mobile.matches(regEx);}