activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.user.calendersample.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/rentTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tabBackground="@color/white"
app:tabIndicatorColor="@color/colorAccent"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextAppearance="@style/CustomTabTextBold"
app:tabTextColor="@color/line_light_grey" />
<FrameLayout
android:layout_weight="1"
android:id="@+id/rent_tab"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatButton
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/btn_submit"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="Submit"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.samset.user.calendersample;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.samset.user.calendersample.fragment.TabFromFragment;
import com.samset.user.calendersample.fragment.TabToFragment;
import com.samset.user.calendersample.views.CalendarView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
public class MainActivity extends AppCompatActivity {
CalendarView simpleCalendarView;
TabLayout tabLayout;
TabLayout.Tab tabFrom;
TabLayout.Tab tabTo;
String from,to;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.rentTabLayout);
findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (from!=null)
{
if (to!=null)
{
Toast.makeText(MainActivity.this,"From "+from+" To "+to,Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(MainActivity.this,"Select from date",Toast.LENGTH_SHORT).show();
}
}
});
setTab();
}
private void setTab() {
tabFrom = tabLayout.newTab();
tabFrom.setText("FROM");
//tabOffers.setIcon(R.drawable.ic_home);
tabLayout.addTab(tabFrom);
beginTransction(new TabFromFragment());
tabTo = tabLayout.newTab();
tabTo.setText("TO");
tabLayout.addTab(tabTo);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new TabFromFragment();
break;
case 1:
fragment = new TabToFragment();
break;
}
beginTransction(fragment);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void getSelectedDatefrom(String data)
{
this.from=data;
}
public void getSelectedDateto(String data)
{
this.to=data;
}
private void beginTransction(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.rent_tab, fragment);
//transaction.addToBackStack(null);
transaction.commit();
}
}
TabFromFragment.java
package com.samset.user.calendersample.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.samset.user.calendersample.MainActivity;
import com.samset.user.calendersample.R;
import com.samset.user.calendersample.views.CalendarView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
/**
* A simple {@link Fragment} subclass.
*/
public class TabFromFragment extends Fragment {
private View view;
private CalendarView calendarView;
public TabFromFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tab_from, container, false);
initView(view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getSelectedDate();
}
private void getSelectedDate()
{
HashSet<Date> events = new HashSet<>();
events.add(new Date());
calendarView.updateCalendar(events);
calendarView.setEventHandler(new CalendarView.EventHandler()
{
@Override
public void onDayLongPress(Date date)
{
// show returned day
DateFormat df = SimpleDateFormat.getDateInstance();
Toast.makeText(getActivity(), df.format(date), Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(Date date) {
DateFormat df = SimpleDateFormat.getDateInstance();
((MainActivity)getActivity()).getSelectedDatefrom(df.format(date));
Toast.makeText(getActivity(), df.format(date), Toast.LENGTH_SHORT).show();
}
});
}
private void initView(View view) {
calendarView = (CalendarView) view.findViewById(R.id.calendar_view);
}
}
Thank you
Live Sample
FullSource codeDateRanger
No comments:
Post a Comment