show a month/year picker on clicking on the month

This commit is contained in:
tibbi 2016-01-29 23:46:53 +01:00
parent c3e2a00b09
commit 5d8bb9e8b7
4 changed files with 95 additions and 3 deletions

View File

@ -19,7 +19,8 @@ import butterknife.BindDimen;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements MyDatePickerDialog.DatePickedListener {
public static final String DATE = "date";
private static final String DATE_PATTERN = "ddMMYYYY";
private static final String YEAR_PATTERN = "YYYY";
@ -118,6 +119,15 @@ public class MainActivity extends AppCompatActivity {
fillCalendar();
}
@OnClick(R.id.table_month)
public void pickMonth() {
final MyDatePickerDialog dialog = new MyDatePickerDialog();
final Bundle bundle = new Bundle();
bundle.putString(DATE, targetDate.toString());
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "datepicker");
}
private String getMonthName() {
final String[] months = new DateFormatSymbols().getMonths();
final StringBuilder sb = new StringBuilder();
@ -130,4 +140,10 @@ public class MainActivity extends AppCompatActivity {
}
return sb.toString();
}
@Override
public void onDatePicked(DateTime dateTime) {
targetDate = dateTime;
fillCalendar();
}
}

View File

@ -0,0 +1,52 @@
package calendar.simplemobiletools.com;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import org.joda.time.DateTime;
public class MyDatePickerDialog extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View view = getActivity().getLayoutInflater().inflate(R.layout.date_picker, null);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
hideDayPicker(datePicker);
final Bundle bundle = getArguments();
if (bundle != null && bundle.containsKey(MainActivity.DATE)) {
final DateTime dateTime = new DateTime(bundle.getString(MainActivity.DATE));
datePicker.init(dateTime.getYear(), dateTime.getMonthOfYear() - 1, 1, null);
}
builder.setView(view);
view.findViewById(R.id.date_picker_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DatePickedListener listener = (DatePickedListener) getActivity();
final int month = datePicker.getMonth() + 1;
final int year = datePicker.getYear();
listener.onDatePicked(new DateTime().withMonthOfYear(month).withYear(year));
dismiss();
}
});
return builder.create();
}
private void hideDayPicker(DatePicker datePicker) {
final LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
final LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
ll2.getChildAt(0).setVisibility(View.GONE);
}
public interface DatePickedListener {
void onDatePicked(DateTime dateTime);
}
}

View File

@ -22,6 +22,8 @@
android:id="@+id/table_month"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/right_arrow"
android:layout_toRightOf="@+id/left_arrow"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
@ -47,7 +49,6 @@
android:layout_below="@+id/table_month"
android:layout_marginBottom="@dimen/activity_margin"
android:layout_marginTop="@dimen/activity_margin"
android:gravity="center">
android:gravity="center"/>
</TableLayout>
</RelativeLayout>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"/>
<Button
android:id="@+id/date_picker_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/date_picker"
android:layout_alignRight="@id/date_picker"
android:layout_below="@id/date_picker"
android:text="OK"/>
</RelativeLayout>