Skip to content

Instantly share code, notes, and snippets.

@smanikandan14
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smanikandan14/7edbd1a14d208f46d60e to your computer and use it in GitHub Desktop.
Save smanikandan14/7edbd1a14d208f46d60e to your computer and use it in GitHub Desktop.
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.
}
}
}
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import com.for4wheeler.R;
import com.for4wheeler.database.LocationDBManager;
import com.for4wheeler.screens.MainActivity;
import com.for4wheeler.screens.TransportationListActivity;
import com.for4wheeler.util.FontProvider;
import java.util.Calendar;
import java.util.List;
/**
* Created by maniselvaraj on 7/6/14.
* Custom navigation list adapter.
*/
public class NavigationAdapter extends BaseAdapter implements SpinnerAdapter {
private LayoutInflater mLayoutInflater;
private List<String> mTitles;
private List<String> mDropDownList;
private <Activity> mAct;
/** Check if user has clicked on specific day item **/
public static boolean isSpecificDayClicked = false;
public NavigationAdapter(Context context, List<String> titles,List<String> dropdown,
TransportationListActivity act) {
mLayoutInflater = LayoutInflater.from(context);
mAct = act;
mTitles = titles;
mDropDownList = dropdown;
}
/**
* Sets the title for 'Specific day' option
**/
public void setTitle(String title) {
mTitles.set(2, title);
}
@Override
public int getCount() {
return mTitles.size();
}
@Override
public Object getItem(int position) {
return mTitles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup p_parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = m_layoutInflater.inflate(R.layout.navigation_title, p_parent, false);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.title.setTypeface(FontProvider.getLightFont());
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.title.setText(mTitles.get(position));
// As soon as a drop down item is choosen, framework tries to go through all the titles list and get the correct
// title. We use this point as the trigger to show the date picker dialog.
if (isSpecificDayClicked ) {
isSpecificDayClicked = false;
showDatePickerDialog();
}
return convertView;
}
@Override
public View getDropDownView(final int position, View convertView, ViewGroup p_parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = m_layoutInflater.inflate(R.layout.navigation_list_item, p_parent, false);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.title.setTypeface(FontProvider.getLightFont());
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final int pos = position;
// If user clicks on 'Specific day' mark the static bool to indicate user has pressed it.
// onClick listener doesn't work as system doesn't dismiss the drop down item as we capture the onCLick.
// Even in this case, we return false in onTouch, we dont consume the touch events further.
// Just to get the trigger that user has touched 'Specific day' we need this.
if (position == 0 || position == 1 || position == 2) {
holder.title.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (position == 0 || position == 1) {
isSpecificDayClicked = false;
} else {
isSpecificDayClicked = true;
}
return false;
}
});
}
holder.title.setText(mDropDownList.get(position));
return convertView;
}
class Holder {
TextView title;
}
public void showDatePickerDialog() {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog((Context)mAct, mAct, year, month, day);
long startTime = LocationDBManager.getInstance().getStartDateFromTransports();
dialog.getDatePicker().setMinDate(startTime);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
dialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment