In this post we are going to implement a Date Slider with View Pager. Output will be look like this:
Step 1: Add View pager in your layout file
Step 3: Create Custom Date Objects :
Create Custom BO/Modal class for Date Object with following fields. Generate their Setter & Getters.
Create ArrayList of these custom date objects to show in Activity
Step 5: Create View Pager Adapter
Clean and run the project. It will show the output as shown in the picture above in blog.
Step 1: Add View pager in your layout file
<android.support.v4.view.ViewPagerStep 2: Initialize View Pager in Activity file:
android:id="@+id/pager"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="54dp" />
private void initViewPager() {
adapter = new BookNowDateListAdapter(this, R.layout.layout_date_item,
getDates());
adapter.setCurrentItem(0);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// set intial position.
onPagerItemClick(pager.getChildAt(0), 0);
}
Step 3: Create Custom Date Objects :
Create Custom BO/Modal class for Date Object with following fields. Generate their Setter & Getters.
private String date;
private String month;
private String year;
private String day;
private String formattedDate;
private Date dateObj;
Create ArrayList of these custom date objects to show in Activity
private ArrayList<CustomDate> getDates() {Step 4: Handle View Pager click to show selected Date:
dates = new ArrayList<CustomDate>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
for (int index = 1; index < 365; index++) {
CustomDate date = new CustomDate();
date.setDate("" + calendar.get(Calendar.DATE));
date.setDay(getDay(calendar.get(Calendar.DAY_OF_WEEK)));
date.setYear("" + calendar.get(Calendar.YEAR));
date.setMonth("" + getMonth(calendar.get(Calendar.MONTH)));
date.setFormattedDate(calendar.get(Calendar.YEAR) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.DATE));
dates.add(date);
calendar.add(Calendar.DATE, 1);
}
return dates;
}
private String getDay(int index) {
switch (index) {
case Calendar.SUNDAY:
return "SUN";
case Calendar.MONDAY:
return "MON";
case Calendar.TUESDAY:
return "TUE";
case Calendar.WEDNESDAY:
return "WED";
case Calendar.THURSDAY:
return "THUR";
case Calendar.FRIDAY:
return "FRI";
case Calendar.SATURDAY:
return "SAT";
}
return "";
}
private String getMonth(int index) {
switch (index) {
case Calendar.JANUARY:
return "JANUARY";
case Calendar.FEBRUARY:
return "FEBRUARY";
case Calendar.MARCH:
return "MARCH";
case Calendar.APRIL:
return "APRIL";
case Calendar.MAY:
return "MAY";
case Calendar.JUNE:
return "JUNE";
case Calendar.JULY:
return "JULY";
case Calendar.AUGUST:
return "AUGUST";
case Calendar.SEPTEMBER:
return "SEPTEMBER";
case Calendar.OCTOBER:
return "OCTOBER";
case Calendar.NOVEMBER:
return "NOVEMBER";
case Calendar.DECEMBER:
return "DECEMBER";
}
return "";
}
public void onPagerItemClick(View view, int index) {
System.out.println("" + index);
adapter.setCurrentItem(index);
pager.setAdapter(adapter);
}
Step 5: Create View Pager Adapter
public class BookNowDateListAdapter extends PagerAdapter {Step 6: Clean and Run the project:
private Context mContext;
private ArrayList<CustomDate> dates;
private LayoutInflater inflater;
private int currentItemPos;
public BookNowDateListAdapter(Context context, int resourceId,
ArrayList<CustomDate> objects) {
this.mContext = context;
this.dates = objects;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setCurrentItem(int item) {
this.currentItemPos = item;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ViewHolder holder;
CustomDate date = this.dates.get(position);
View convertView = inflater.inflate(R.layout.layout_date_item,
container, false);
holder = new ViewHolder();
holder.dateTextView = (TextView) convertView
.findViewById(R.id.layout_date_text);
holder.dayTextview = (TextView) convertView
.findViewById(R.id.layout_date_day);
holder.monthTextView = (TextView) convertView
.findViewById(R.id.layout_date_month);
holder.outerLayout = (LinearLayout) convertView
.findViewById(R.id.layout_date_item_outer_layout);
convertView.setTag(Integer.valueOf(position));
holder.dateTextView.setText(date.getDate());
holder.dayTextview.setText(date.getDay());
holder.monthTextView.setText(date.getMonth());
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((MyActivity) mContext).onPagerItemClick(v,
(Integer) v.getTag());
}
});
if (position == currentItemPos) {
holder.outerLayout.setBackgroundColor(Color.parseColor("#EC522C"));
} else {
holder.outerLayout.setBackgroundColor(Color.parseColor("#ffffff"));
}
((ViewPager) container).addView(convertView);
return convertView;
}
private class ViewHolder {
private TextView monthTextView;
private TextView dayTextview;
private TextView dateTextView;
private LinearLayout outerLayout;
}
@Override
public int getCount() {
return dates.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == (object);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
((ViewPager) container).removeView(view);
view = null;
}
public float getPageWidth(int position) {
return 0.2f;
}
}
Clean and run the project. It will show the output as shown in the picture above in blog.
Please send me the entire project files.kurukshetran.t@gmail.com
ReplyDeletePlease send me entire project to tuhn22@gmail.com
ReplyDeletePlease send me entire project to tuhn22@gmail.com
ReplyDeletethank you so much
ReplyDeleteThis comment has been removed by the author.
ReplyDeletewhere is layout_date_item
ReplyDeletePlease send me the entire project 3rdenebileg@gmail.com
ReplyDeletethank you
Please send me the project at garv.dev26@gmail.com. I cant figure how to implement slider along with the pager adapter
ReplyDeletePlease send me the entire project files >> riosmith04@gmail.com
ReplyDeleteHi!
ReplyDeleteThis project is very good.
Please send me entire project to rstcom919@gmail.com
Hey, please send me the project at vanya.santeva@gmail.com . Thank you <3
ReplyDeleteHello!
ReplyDeletePlease send me the project at andre.viyt@gmail.com
Thank you
Hi! Could you send me the all project please? ilpadrino00@gmail.com. Thank you very much.
ReplyDeleteHi!
ReplyDeletePlease send the project code to ms.swab@gmail.com.It will be very useful,if you send it.Thanks in advance.
Hi!
ReplyDeletePlease send the project code to ms.swab@gmail.com.It will be very useful,if you send it.Thanks in advance.
Hi~
ReplyDeletePlease send me this android project entire code. Please... soodus1119@gmail.com
Thank you so much!
Please send me the entire project files dubiktpm3@gmail.com
ReplyDeleteplease send entire project gpattani5@gmail.com
ReplyDeletePlease send me the entire project ad87mb@gmail.com
ReplyDeletethank you
please send me the code @ b111049@iiit-bh.ac.in
ReplyDeleteFood for thought: Why not just post the entire project for everyone to download?!?!
ReplyDeleteWhy did you give us the steps the build this but failed to give us complete steps from start to finish to make this work?
Wish their were a dislike button on this blog!
Guys, just so you know, I've been following the topic since 2016 May 4, he hasn't sent the code to anyone yet, it's probably an abandoned blog.
ReplyDeleteBut when I make something similar to this, I'll make sure to share it here :)
ReplyDeleteCan u share the code now?
Delete