Skip to content

Commit

Permalink
-Add option to open on chosen locale
Browse files Browse the repository at this point in the history
  • Loading branch information
malaa101 committed Jun 22, 2019
1 parent 60263ff commit 0348731
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.kunzisoft.switchdatetimesample;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
Expand All @@ -18,6 +20,7 @@

/**
* Sample class for an example of using the API SwitchDateTimePicker
*
* @author JJamet
*/
public class Sample extends AppCompatActivity {
Expand All @@ -44,12 +47,13 @@ protected void onCreate(Bundle savedInstanceState) {

// Construct SwitchDateTimePicker
dateTimeFragment = (SwitchDateTimeDialogFragment) getSupportFragmentManager().findFragmentByTag(TAG_DATETIME_FRAGMENT);
if(dateTimeFragment == null) {
if (dateTimeFragment == null) {
dateTimeFragment = SwitchDateTimeDialogFragment.newInstance(
getString(R.string.label_datetime_dialog),
getString(android.R.string.ok),
getString(android.R.string.cancel),
getString(R.string.clean) // Optional
getString(R.string.clean),// Optional
"ar"
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.kunzisoft.switchdatetime;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.fragment.app.DialogFragment;
import androidx.appcompat.app.AlertDialog;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -34,6 +38,7 @@

/**
* A fragment that displays a dialog window with Date and Time who can be selected by switch button
*
* @author J-Jamet
*/
public class SwitchDateTimeDialogFragment extends DialogFragment {
Expand All @@ -53,9 +58,14 @@ public class SwitchDateTimeDialogFragment extends DialogFragment {
private static final String TAG_POSITIVE_BUTTON = "POSITIVE_BUTTON";
private static final String TAG_NEGATIVE_BUTTON = "NEGATIVE_BUTTON";
private static final String TAG_NEUTRAL_BUTTON = "NEUTRAL_BUTTON";
private static final String TAG_DEFAULT_LOCALE = "DEFAULT_LOCALE";

private static final String DEFAULT_LOCALE = "en";


private String mLabel;
private String mPositiveButton;
private String mDefaultLocale;
private String mNegativeButton;
private String mNeutralButton;
private OnButtonClickListener mListener;
Expand All @@ -82,29 +92,33 @@ public class SwitchDateTimeDialogFragment extends DialogFragment {

/**
* Create a new instance of SwitchDateTimeDialogFragment
* @param label Title of dialog
*
* @param label Title of dialog
* @param positiveButton Text for positive button
* @param negativeButton Text for negative button
* @return DialogFragment
*/
public static SwitchDateTimeDialogFragment newInstance(String label, String positiveButton, String negativeButton) {
return newInstance(label, positiveButton, negativeButton, null);
return newInstance(label, positiveButton, negativeButton, null, DEFAULT_LOCALE);
}

/**
* Create a new instance of SwitchDateTimeDialogFragment
* @param label Title of dialog
*
* @param label Title of dialog
* @param positiveButton Text for positive button
* @param negativeButton Text for negative button
* @param defaultLocale Text for default locale
* @return DialogFragment
*/
public static SwitchDateTimeDialogFragment newInstance(String label, String positiveButton, String negativeButton, String neutralButton) {
public static SwitchDateTimeDialogFragment newInstance(String label, String positiveButton, String negativeButton, String neutralButton, String defaultLocale) {
SwitchDateTimeDialogFragment switchDateTimeDialogFragment = new SwitchDateTimeDialogFragment();
// Add arguments
Bundle args = new Bundle();
args.putString(TAG_LABEL, label);
args.putString(TAG_POSITIVE_BUTTON, positiveButton);
args.putString(TAG_NEGATIVE_BUTTON, negativeButton);
args.putString(TAG_DEFAULT_LOCALE, defaultLocale);
if (neutralButton != null) {
args.putString(TAG_NEUTRAL_BUTTON, neutralButton);
}
Expand All @@ -115,6 +129,7 @@ public static SwitchDateTimeDialogFragment newInstance(String label, String posi

/**
* Set listener for actions
*
* @param onButtonClickListener Listener for click
*/
public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
Expand All @@ -132,28 +147,31 @@ public void onSaveInstanceState(Bundle savedInstanceState) {
}

@Override
public @NonNull Dialog onCreateDialog(Bundle savedInstanceState) {
public @NonNull
Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);

assert getActivity() != null;
assert getContext() != null;

dateTimeCalendar.setTimeZone(timeZone);

if(getArguments() != null) {
if (getArguments() != null) {
mLabel = getArguments().getString(TAG_LABEL);
mPositiveButton = getArguments().getString(TAG_POSITIVE_BUTTON);
mNegativeButton = getArguments().getString(TAG_NEGATIVE_BUTTON);
mNeutralButton = getArguments().getString(TAG_NEUTRAL_BUTTON);
mDefaultLocale=getArguments().getString(TAG_DEFAULT_LOCALE);
}
setDefaultLocale(mDefaultLocale);

if(savedInstanceState != null) {
if (savedInstanceState != null) {
currentPosition = savedInstanceState.getInt(STATE_CURRENT_POSITION);
dateTimeCalendar.setTime(new Date(savedInstanceState.getLong(STATE_DATETIME)));
}

// Throw exception if default select date isn't between minimumDateTime and maximumDateTime
if(dateTimeCalendar.before(minimumDateTime) || dateTimeCalendar.after(maximumDateTime))
if (dateTimeCalendar.before(minimumDateTime) || dateTimeCalendar.after(maximumDateTime))
throw new RuntimeException("Default date " + dateTimeCalendar.getTime() + " must be between "
+ minimumDateTime.getTime() + " and " + maximumDateTime.getTime());

Expand All @@ -164,7 +182,7 @@ public void onSaveInstanceState(Bundle savedInstanceState) {

// Set label
TextView labelView = dateTimeLayout.findViewById(R.id.label);
if(mLabel != null)
if (mLabel != null)
labelView.setText(mLabel);
else
labelView.setText(getString(R.string.label_datetime_dialog));
Expand All @@ -186,7 +204,8 @@ public void onAnimationEnd(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {}
public void onAnimationRepeat(Animation animation) {
}
});
viewSwitcher.getOutAnimation().setAnimationListener(new Animation.AnimationListener() {
@Override
Expand All @@ -200,11 +219,12 @@ public void onAnimationEnd(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {}
public void onAnimationRepeat(Animation animation) {
}
});

// Defined the start position
if(startAtPosition != UNDEFINED_POSITION)
if (startAtPosition != UNDEFINED_POSITION)
currentPosition = startAtPosition;
viewSwitcher.setDisplayedChild(currentPosition);

Expand All @@ -215,7 +235,7 @@ public void onAnimationRepeat(Animation animation) {}
@Override
public void onClick(View view) {
Utils.animLabelElement(view);
if(!(blockAnimationIn && blockAnimationOut))
if (!(blockAnimationIn && blockAnimationOut))
viewSwitcher.showNext();
}
});
Expand All @@ -237,14 +257,14 @@ public void onClick(View view) {
yearHeaderValues.setOnClickListener(onYearClickListener);

// Init simple date format if null
if(dayAndMonthSimpleDate == null)
if (dayAndMonthSimpleDate == null)
dayAndMonthSimpleDate = new SimpleDateFormat("MMMM dd", Locale.getDefault());
if(yearSimpleDate == null)
if (yearSimpleDate == null)
yearSimpleDate = new SimpleDateFormat("yyyy", Locale.getDefault());

dayAndMonthSimpleDate.setTimeZone(timeZone);
yearSimpleDate.setTimeZone(timeZone);

// Init headers
yearHeaderValues.setText(yearSimpleDate.format(dateTimeCalendar.getTime()));
monthAndDayHeaderValues.setText(dayAndMonthSimpleDate.format(dateTimeCalendar.getTime()));
Expand Down Expand Up @@ -316,23 +336,23 @@ public void onYearSelected(View view, int yearPicker) {
db = new AlertDialog.Builder(getContext());
}
db.setView(dateTimeLayout);
if(mPositiveButton == null)
if (mPositiveButton == null)
mPositiveButton = getString(android.R.string.ok);
db.setPositiveButton(mPositiveButton, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(mListener !=null) {
if (mListener != null) {
mListener.onPositiveButtonClick(dateTimeCalendar.getTime());
}
}
});
if(mNegativeButton == null)
if (mNegativeButton == null)
mNegativeButton = getString(android.R.string.cancel);
db.setNegativeButton(mNegativeButton, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Close dialog
if(mListener !=null) {
if (mListener != null) {
mListener.onNegativeButtonClick(dateTimeCalendar.getTime());
}
}
Expand All @@ -342,7 +362,7 @@ public void onClick(DialogInterface dialog, int which) {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mListener != null) {
if(mListener instanceof OnButtonWithNeutralClickListener)
if (mListener instanceof OnButtonWithNeutralClickListener)
((OnButtonWithNeutralClickListener) mListener).onNeutralButtonClick(dateTimeCalendar.getTime());
}
}
Expand All @@ -351,6 +371,14 @@ public void onClick(DialogInterface dialog, int which) {
return db.create();
}

private void setDefaultLocale(String mDefaultLocale) {
Locale locale = new Locale(mDefaultLocale);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config,getResources().getDisplayMetrics());
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
Expand Down Expand Up @@ -396,6 +424,7 @@ public void setYear(int year) {

/**
* Assign default month at start (ex: Calendar.DECEMBER)
*
* @see Calendar
*/
public void setDefaultMonth(int month) {
Expand Down Expand Up @@ -468,6 +497,7 @@ public int getYear() {

/**
* Get current month as Calendar.MONTH
*
* @see Calendar
*/
public int getMonth() {
Expand Down Expand Up @@ -539,12 +569,13 @@ public SimpleDateFormat getSimpleDateMonthAndDayFormat() {

/**
* Assign a SimpleDateFormat like "d MMM" to show formatted DateTime
*
* @param simpleDateFormat Format to show month and day
*/
public void setSimpleDateMonthAndDayFormat(SimpleDateFormat simpleDateFormat) throws SimpleDateMonthAndDayFormatException{
public void setSimpleDateMonthAndDayFormat(SimpleDateFormat simpleDateFormat) throws SimpleDateMonthAndDayFormatException {
Pattern patternMonthAndDay = Pattern.compile("(M|w|W|D|d|F|E|u|\\.|\\s)*");
Matcher matcherMonthAndDay = patternMonthAndDay.matcher(simpleDateFormat.toPattern());
if(!matcherMonthAndDay.matches()) {
if (!matcherMonthAndDay.matches()) {
throw new SimpleDateMonthAndDayFormatException(simpleDateFormat.toPattern() + "isn't allowed for " + patternMonthAndDay.pattern());
}
this.dayAndMonthSimpleDate = simpleDateFormat;
Expand All @@ -559,17 +590,18 @@ public void set24HoursMode(boolean is24HoursMode) {

/**
* Highlight AM or PM selected, by default AM or PM selected is not highlight. Only works if 24 hours mode is activated
*
* @param highlightAMPMSelection true to visually highlight selected item
*/
public void setHighlightAMPMSelection(boolean highlightAMPMSelection) {
this.highlightAMPMSelection = highlightAMPMSelection;
}

/**
* Set timezone different from default
*/
* Set timezone different from default
*/
public void setTimeZone(TimeZone timeZone) {
if(timeZone != null) {
if (timeZone != null) {
this.timeZone = timeZone;
}
}
Expand All @@ -595,6 +627,7 @@ public class SimpleDateMonthAndDayFormatException extends Exception {
*/
public interface OnButtonClickListener {
void onPositiveButtonClick(Date date);

void onNegativeButtonClick(Date date);
}

Expand Down Expand Up @@ -625,7 +658,7 @@ public int getPosition() {
/**
* Listener for click on Header element
*/
public class OnClickHeaderElementListener implements View.OnClickListener{
public class OnClickHeaderElementListener implements View.OnClickListener {
private int positionView;

OnClickHeaderElementListener(int positionView) {
Expand All @@ -635,7 +668,7 @@ public class OnClickHeaderElementListener implements View.OnClickListener{
@Override
public void onClick(View view) {
Utils.animLabelElement(view);
if(viewSwitcher.getDisplayedChild() != positionView)
if (viewSwitcher.getDisplayedChild() != positionView)
viewSwitcher.setDisplayedChild(positionView);

startAtPosition = positionView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<TextView
android:id="@+id/hours"
android:id="@+id/minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time_placeholder"
android:layout_toStartOf="@id/minutes"
android:layout_toStartOf="@id/hours"
android:layout_marginEnd="@dimen/separator_total_vertical_padding_approximation"
android:layout_centerVertical="true"
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<TextView
android:id="@+id/minutes"
android:id="@+id/hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time_placeholder"
Expand All @@ -63,7 +63,7 @@
android:text="@string/time_placeholder"
android:paddingLeft="@dimen/ampm_left_padding"
android:paddingRight="@dimen/ampm_left_padding"
android:layout_toEndOf="@+id/minutes"
android:layout_toEndOf="@+id/hours"
android:layout_alignBaseline="@+id/separator"
style="@style/Theme.SwitchDateTime.TimeLabelAmPm" />
</RelativeLayout>
2 changes: 1 addition & 1 deletion switchdatetime/src/main/res/layout/year_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="wrap_content">
<TextView
android:id="@+id/year_textView"
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/year_item_padding"
android:textSize="16sp"
Expand Down

0 comments on commit 0348731

Please sign in to comment.