Last active
August 29, 2015 14:02
-
-
Save smanikandan14/7edbd1a14d208f46d60e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Activity implements DatePickerDialog.OnDateSetListener { | |
int selectedIndex = 0; | |
int previousSelectedIndex = 0; | |
NavigationAdapter adapter; | |
onCreate() { | |
ArrayList<String> itemList = new ArrayList<String>(); | |
itemList.add("Today"); | |
itemList.add("Yesterday"); | |
itemList.add("Specific day"); | |
ArrayList<String> titleList = new ArrayList<String>(); | |
titleList.add("Today"); | |
titleList.add("Yesterday"); | |
titleList.add("Specific day"); | |
adapter = new NavigationAdapter(this, titleList, itemList, this); | |
getSupportActionBar().setListNavigationCallbacks(adapter, this); | |
getSupportActionBar().setSelectedNavigationItem(selectedIndex); | |
previousSelectedIndex = 0; | |
} | |
@Override | |
public boolean onNavigationItemSelected(int position, long id) { | |
selectedIndex = position; | |
if (position == 0 || position == 1) { | |
previousSelectedIndex = position; | |
} | |
// Set the title for 'Specific day' item to be 'Today' or 'Yesterday'. | |
if (previousSelectedIndex == 0) { | |
adapter.setTitle("Today"); | |
adapter.notifyDataSetChanged(); | |
} else if (previousSelectedIndex == 1) { | |
adapter.setTitle("Yesterday"); | |
adapter.notifyDataSetChanged(); | |
} | |
if(position != 2) { | |
// Do what you wanted to do for 'Today' & 'Yesterday' | |
} | |
return true; | |
} | |
public void onDateSet(DatePicker view, int year, int month, int day) { | |
// Do something with the date chosen by the user | |
if (view.isShown()) { | |
Calendar calendar = Calendar.getInstance(); | |
int todayDate = calendar.get(Calendar.DAY_OF_MONTH); | |
long yesterdayTime = calendar.getTimeInMillis() - ( 24 * 60 * 60 * 1000); | |
calendar.setTime(new Date (yesterdayTime)); | |
int yesterdayDate = calendar.get(Calendar.DAY_OF_MONTH); | |
if (previousSelectedIndex == 0) { | |
if (todayDate == day) { | |
return; | |
} | |
} else if (previousSelectedIndex == 1) { | |
if (yesterdayDate == day) { | |
return; | |
} | |
} | |
calendar.set(year, month, day, 0, 0, 0); | |
startTime = calendar.getTimeInMillis(); | |
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM dd"); | |
if (todayDate == day) { | |
adapter.setTitle("Today"); | |
} else if (yesterdayDate == day) { | |
adapter.setTitle("Yesterday"); | |
} else { | |
adapter.setTitle(format.format(new Date(startTime))); | |
} | |
getSupportActionBar().setSelectedNavigationItem(2); | |
adapter.notifyDataSetChanged(); | |
// Do what you wanted to do rest. | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment