Monday, August 22, 2016

Android - Formatting Date and Time.



Date and Time formatting Cheat Sheet

SymbolMeaningType Type
GEraText GG->AD
YYearNumber yy -> 03, yyyy -> 2003
MMonthText or Number M -> 7, MM -> 07, MMM -> Jul , MMMM -> July                 
dDay in monthNumber d -> 3 , dd -> 03
hHour (1-12, AM/PM)Number h -> 3 , hh -> 03
HHour (0-23)Number H -> 15 , HH -> 15
kHour (1-24)Number k -> 3 , kk -> 03
KHour (0-11 AM/PM) Number K -> 15 , KK -> 15
mMinuteNumber m -> 7 , m -> 15 , mm -> 15
sSecondNumber s -> 15 , ss -> 15
SMillisecond (0-999)Number SSS -> 007
EDay in weekText EEE -> Tue , EEEE -> Tuesday
DDay in yr(1-365,1-364)Number D -> 65 , DDD -> 065
FDay of week in month (1-5)|Number F -> 1
wWeek in year (1-53)Number w -> 7   
aAM/PMText a -> AM , aa -> AM
zTime zoneText z -> EST , zzz -> EST , zzzz -> Eastern Standard Time


Some sample of date format


 String DATE_FORMAT_1 = "dd-MMM-yyyy kk:mm";
 String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";
 String DATE_FORMAT_3 = "yyyy-MM-dd";
 String DATE_FORMAT_4 = "dd-MMM-yyyy"; 
 String DATE_FORMAT_5 = "dd_MM_yyyy"; 
 String DATE_FORMAT_6 = "yyyy-MM-dd'T'HH:mm:ss"; 
 String DATE_FORMAT_7 = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"; 



1. Get today date.


Example - 1
public String getCurrentTimeStr() { return new SimpleDateFormat(DATE_FORMAT_1, Locale.US).format(new Date()) }


2. Compare dates.


Example - 2
public boolean isFutureDate(String originalDate) { boolean isFuture = false; SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_1, Locale.US); Date dateObj; try { dateObj = sdf.parse(originalDate); if (new Date().before(dateObj)) { isFuture = true; } } catch (ParseException e) { } return isFuture }


3. Convert UTC to Local date UTC is the common time standard across the world.


Example - 3
public String convertUtcToLocal(String dateTime) { String do = ""; if(dateTime !=null){ DateFormat originalFormat = new SimpleDateFormat(DATE_FORMAT_2, Locale.US); originalFormat.setTimeZone(TimeZone.getTimeZone("UTC")); DateFormat targetFormat = new SimpleDateFormat(DATE_FORMAT_2, Locale.US); targetFormat.setTimeZone(TimeZone.getDefault()) try { Date date = originalFormat.parse(dateTime); dob = targetFormat.format(date); } catch (ParseException e) { } } return do; }



4. Merge Date and time

private static final String DATE_TIME = "d MMM yyyy HH:mm";


Example - 4
public Date getTrainStartTime(String originalDate, String time) { String mAlertDateTime = originalDate + " " + time; SimpleDateFormat dft = new SimpleDateFormat(DATE_TIME, Locale.getDefault()); try { Date d = dft.parse(mAlertDateTime); return d; } catch (ParseException e) { e.printStackTrace(); } return new Date(); }




Share:

Monday, June 20, 2016

Android - Load more custom ListView with footer

Overview:- If you are loading data from a web server and the list is huge, then the practical solution would be to load a certain amount and if the user scrolls to the end of the list, load some more and keep going until you load the full list. 




I have handled the following events:


Add a view that will visible on the footer of a list when app loading data from the server.

Notify to your view when load more event occurs.

Notify to your view when scroll end.


CustomListView
import android.content.Context; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; import android.widget.ListView; import android.widget.ProgressBar; import static android.content.Context.LAYOUT_INFLATER_SERVICE; public class CustomListView extends ListView implements AbsListView.OnScrollListener { private Context mContext; //The Load more footer. private View loadMoreFooter; private boolean isLoadingMore; private int currentScrollState; private int currentFirstVisibleItem; private int currentVisibleItemCount; private int currentTotalItemCount; private int currentLastItem; public interface ListViewListener { // Load data. void loadData(); void onScrollEnd(); } private ListViewListener mLoadDataListener; /*** Instantiates a new Custom list view. * * @param context the context * @param attrs the attrs * @param defStyle the def style */ public CustomListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.setOnScrollListener(this); mContext = context; initializeLoadMore(); } /*** Instantiates a new Custom list view. * @param context the context * @param attrs the attrs */ public CustomListView(Context context, AttributeSet attrs) { super(context, attrs); this.setOnScrollListener(this); mContext = context; initializeLoadMore(); } /*** Set load more listener. * @param loadData the load data */ public void setLoadMoreListener(ListViewListener loadData){ mLoadDataListener =loadData; } /*** Instantiates a new Custom list view. * @param context the context */ public CustomListView(Context context) { super(context); this.setOnScrollListener(this); initializeLoadMore(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { this.currentScrollState = scrollState; this.isScrollCompleted(); if (scrollState == OnScrollListener.SCROLL_STATE_IDLE&&mLoadDataListener!=null) { mLoadDataListener.onScrollEnd(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.currentFirstVisibleItem = firstVisibleItem; this.currentVisibleItemCount = visibleItemCount; this.currentTotalItemCount = totalItemCount; } private void isScrollCompleted() { if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) { this.currentLastItem = this.currentFirstVisibleItem + this.currentVisibleItemCount; if (currentLastItem == currentTotalItemCount && !(isLoadingMore)&&mLoadDataListener!=null) { isLoadingMore = true; addFooterView(loadMoreFooter); loadMoreFooter.setVisibility(View.VISIBLE); mLoadDataListener.loadData(); } } } /*** Reset load more view. */ public void resetLoadMoreView() { isLoadingMore = false; loadMoreFooter.setVisibility(View.GONE); removeFooterView(loadMoreFooter); } private void initializeLoadMore() { loadMoreFooter = ((LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.loading_more_, null, false); loadMoreFooter.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white)); ProgressBar progressBar = (ProgressBar) loadMoreFooter.findViewById(R.id.progressBar); progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(mContext, R.color.login_bg), android.graphics.PorterDuff.Mode.SRC_IN); } }

Adding a FooterView to the ListView  (loading_more_.xml)

A footer View is nothing more than a piece of XML that defines how the footer of your listview will look like. Mine is as follows:
loading_more.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:gravity="center_horizontal|center_vertical" android:orientation="horizontal" android:padding="@dimen/padding_ten"> <ProgressBar android:id="@+id/progressBar" android:layout_width="@dimen/height_width_20" android:layout_height="@dimen/height_width_20" /> </LinearLayout>

At the end ,just implement  ListViewListener in Activity or Fragment and set listener 
listView.setLoadMoreListener(this); , you get lord more and scroll end event of list.


Share:

Get it on Google Play

React Native - Start Development with Typescript

React Native is a popular framework for building mobile apps for both Android and iOS. It allows developers to write JavaScript code that ca...