Check if a given time lies between two times regardless of date – Android

If start time “23:00:00” and end “02:00:00″[next day] and current time is “01:30:00” then result will false…
Answer given below works perfect

public static boolean isTimeBetweenTwoTime(String initialTime, String finalTime, String currentTime) throws ParseException {
        String reg = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
        if (initialTime.matches(reg) && finalTime.matches(reg) && currentTime.matches(reg)) {
            boolean valid = false;
            //Start Time
            java.util.Date inTime = new SimpleDateFormat("HH:mm:ss").parse(initialTime);
            Calendar calendar1 = Calendar.getInstance();

            //Current Time
            java.util.Date checkTime = new SimpleDateFormat("HH:mm:ss").parse(currentTime);
            Calendar calendar3 = Calendar.getInstance();

            //End Time
            java.util.Date finTime = new SimpleDateFormat("HH:mm:ss").parse(finalTime);
            Calendar calendar2 = Calendar.getInstance();

            if (finalTime.compareTo(initialTime) < 0) {
                calendar2.add(Calendar.DATE, 1);
                calendar3.add(Calendar.DATE, 1);

            java.util.Date actualTime = calendar3.getTime();
            if ((actualTime.after(calendar1.getTime()) || actualTime.compareTo(calendar1.getTime()) == 0) 
                    && actualTime.before(calendar2.getTime())) {
                valid = true;
            return valid;
        } else {
            throw new IllegalArgumentException("Not a valid time, expecting HH:MM:SS format");


“07:00:00” – “17:30:00” – “15:30:00” [current] – true
“17:00:00” – “21:30:00” – “16:30:00” [current] – false
“23:00:00” – “04:00:00” – “02:00:00” [current] – true
“00:30:00” – “06:00:00” – “06:00:00” [current] – false

Leave a comment

Posted by on August 31, 2015 in Android Source Code


Creating a “Card” UI in Android




<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="">
      <shape android:shape="rectangle">
         <solid android:color="#CABBBBBB"/>
         <corners android:radius="2dp" />

      <shape android:shape="rectangle">
         <solid android:color="@android:color/white"/>
         <corners android:radius="2dp" />


<?xml version="1.0" encoding="utf-8"?>
      android:text="Hello Card!\nThis is an example of a card..."/>
Leave a comment

Posted by on July 7, 2015 in Android Source Code


Find start and end date of week from input date in java

// set the date e.g. 12 June 2015
Calendar cal = Calendar.getInstance();
cal.set(2015, 6 – 1, 12);

// “calculate” the start date of the week
Calendar first = (Calendar) cal.clone();
first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() – first.get(Calendar.DAY_OF_WEEK));

// and add six days to the end date
Calendar last = (Calendar) first.clone();
last.add(Calendar.DAY_OF_YEAR, 6);

// print the result
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd”);
System.out.println(df.format(first.getTime()) + ” -> ” +

2015-06-08 -> 2015-06-14

Leave a comment

Posted by on June 6, 2015 in Android Source Code


Find system time format in android

String clockType = android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.TIME_12_24);

It will return “12” or “24” as per device setting.

Leave a comment

Posted by on April 15, 2015 in Android Source Code


ListView inside ScrollView is not scrolling on Android

Sometimes we face issue of ListView height if we put ListView inside ScrollView. One more issue comes with this is we are unable to scroll the page. ListView inside ScrollView is a bad practice but there is one solution for it.
Create class as shown below and just use that as a Listview and start binding data to it.

package com.example.logactivity;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;

public class NestedListView extends ListView implements OnTouchListener, OnScrollListener {

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;

    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);

    public void onScrollStateChanged(AbsListView view, int scrollState) {

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                newHeight += getDividerHeight() * listPosition;
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
        } else {
            newHeight = getMeasuredHeight();
        setMeasuredDimension(getMeasuredWidth(), newHeight);

    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
        return false;

Your ListView inside your layout should be:

                android:scrollbars="none" >
Leave a comment

Posted by on March 24, 2015 in Android Source Code


How to handle “App Not Installed.” issue in Android

The workaround for above issue is:
After Uninstalling App You may found that App Name in Apps List of Downloaded Tab.
Step 1: Go to Settings
Step 2: Apps
Step 3: At bottom of list you found YourApp with “NOT INSTALLED” Tag
Step 4: Open
Step 5: Click on OptionMenu and Select “Uninstall for all Users”

It will work for sure.
Or else if you are getting message in Logcat like “Installation error: INSTALL_FAILED_DUPLICATE_PERMISSION” you can follow steps above.

Leave a comment

Posted by on February 19, 2015 in Android Source Code


BroadcastReceiver receives multiple identical messages for one event – Android

Receiving multiple broadcast is a device specific problem. Some phones just send one broadcast while other send 2 or 3. But there is a work around:

Assuming you get the disconnect message when the wifi is disconnected, I would guess the first one is the correct one and the other 2 are just echoes for some reason.

To know that the message has been called, you could have a static boolean that gets toggled between connect and disconnect and only call your sub-routines when you receive a connection and the boolean is true. Something like:

public class ConnectionChangeReceiver extends BroadcastReceiver {
    private static boolean firstConnect = true;

    public void onReceive(Context context, Intent intent) {
        final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null) {
            if(firstConnect) { 
                // do subroutines here
                firstConnect = false;
        else {
            firstConnect= true;
Leave a comment

Posted by on February 7, 2015 in Android Source Code


Get every new post delivered to your Inbox.