Created
November 29, 2017 13:55
-
-
Save Anrimian/c949c2118a1362f63d6bc7088abe8fc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("WeakerAccess") | |
public class AndroidUtils { | |
public static int getColorFromAttr(Context ctx, int attributeId) { | |
int colorId = getResourceIdFromAttr(ctx, attributeId); | |
return ContextCompat.getColor(ctx, colorId); | |
} | |
public static int dpToPx(int dp, Context ctx) { | |
DisplayMetrics displayMetrics = ctx.getResources().getDisplayMetrics(); | |
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); | |
} | |
public static Drawable getDrawableFromAttr(Context ctx, int attributeId) { | |
int drawableId = getResourceIdFromAttr(ctx, attributeId); | |
return ContextCompat.getDrawable(ctx, drawableId); | |
} | |
public static int getResourceIdFromAttr(Context ctx, int attributeId) { | |
TypedValue typedValue = new TypedValue(); | |
Resources.Theme theme = ctx.getTheme(); | |
theme.resolveAttribute(attributeId, typedValue, true); | |
return typedValue.resourceId; | |
} | |
public static void hideKeyboard(Activity activity) { | |
View view = activity.getCurrentFocus(); | |
if (view != null) { | |
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); | |
if (imm != null) { | |
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); | |
} | |
} | |
} | |
public static void showDateTimePickerDialog(Context ctx, Date startDate, DateReadyListener listener) { | |
showDatePickerDialog(ctx, startDate, date -> showTimePickerDialog(ctx, date, listener)); | |
} | |
public static void showDatePickerDialog(Context ctx, Date startDate, DateReadyListener listener) { | |
Calendar calendar = Calendar.getInstance(); | |
Date now = calendar.getTime(); | |
if (startDate == null) { | |
startDate = now; | |
} | |
calendar.setTime(startDate); | |
DatePickerDialog.OnDateSetListener dateSetListener = (datePicker, year, monthOfYear, dayOfMonth) -> { | |
calendar.set(year, monthOfYear, dayOfMonth); | |
listener.onDateReady(calendar.getTime()); | |
}; | |
DatePickerDialog dialog = new DatePickerDialog(ctx, | |
dateSetListener, | |
calendar.get(Calendar.YEAR), | |
calendar.get(Calendar.MONTH), | |
calendar.get(Calendar.DAY_OF_MONTH)); | |
dialog.getDatePicker().setMaxDate(now.getTime()); | |
dialog.show(); | |
} | |
public static void showTimePickerDialog(Context ctx, Date startDate, DateReadyListener listener) { | |
Calendar calendar = Calendar.getInstance(); | |
Date now = calendar.getTime(); | |
if (startDate == null) { | |
startDate = now; | |
} | |
calendar.setTime(startDate); | |
TimePickerDialog.OnTimeSetListener timeSetListener = (timePicker, hour, minute) -> { | |
calendar.set(Calendar.HOUR, hour); | |
calendar.set(Calendar.MINUTE, minute); | |
listener.onDateReady(calendar.getTime()); | |
}; | |
TimePickerDialog dialog = new TimePickerDialog(ctx, | |
timeSetListener, | |
calendar.get(Calendar.HOUR), | |
calendar.get(Calendar.MINUTE), | |
true); | |
dialog.show(); | |
} | |
public interface DateReadyListener { | |
void onDateReady(Date date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment