If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.example.jens.myapplication.util;
/*www.java2s.com*/import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import com.example.jens.myapplication.domain.Person;
import org.joda.time.DateTime;
/**
* Created by Sam on 9/11/2014.
*/publicclass ActivityUtils {
/**
* Opens a date picker for a person's date of birth
* @param context
* @param person The person whose date of birth should be used as a starting point of the
* datepicker
* @param callback the callback that happens when the datepicker is finished
*/publicstaticvoid chooseDateOfBirth(Context context, Person person, final OnDateChosenListener callback){
DateTime startDate;
if(person == null || person.getDateOfBirth() == null){
startDate = DateTime.now();
}else{
startDate = person.getDateOfBirth();
}
int startYear = startDate.getYear();
int startMonth = startDate.getMonthOfYear() - 1;
int startDay = startDate.getDayOfMonth();
DatePickerDialog.OnDateSetListener onDateSetCallback = new DatePickerDialog.OnDateSetListener() {
@Override
publicvoid onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if(view.isShown()){
callback.onDateChosen(year, monthOfYear + 1, dayOfMonth);
}
}
};
DatePickerDialog dialog = new DatePickerDialog(context,
onDateSetCallback, startYear, startMonth, startDay);
dialog.show();
}
/**
* Hides the keyboard
* @param activity The activity currently active
*/publicstaticvoid hideKeyboardFromActivity(Activity activity){
View v = activity.getCurrentFocus();
if(v != null){
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* Get the span count for a gridlayoutmanager for example. returns how many columns the
* list of items should contain.
* @param activity
* @return The amount of columns the screen can fit
*/publicstaticint getSpanCount(Activity activity){
double diagonalInches = getDisplayInches(activity);
int orientation = getDisplayOrientation(activity);
if (diagonalInches >= 10) {
if(orientation == Configuration.ORIENTATION_PORTRAIT){
return 3;
}
else
{
return 4;
}
}
elseif (diagonalInches >= 7) {
if(orientation == Configuration.ORIENTATION_PORTRAIT){
return 2;
}
else
{
return 3;
}
}
else {
if(orientation == Configuration.ORIENTATION_PORTRAIT){
return 1;
}
else
{
return 1;
}
}
}
privatestaticint getDisplayOrientation(Activity activity){
return activity.getResources().getConfiguration().orientation;
}
privatestaticdouble getDisplayInches(Activity activity){
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
double diagonalInches = Math.sqrt((widthInches * widthInches)+ (heightInches * heightInches));
return diagonalInches;
}
publicinterface OnDateChosenListener{
/**
* Called when a date was chosen
* @param year Year that was chosen
* @param monthOfYear Month of the year of the date that was chosen
* @param dayOfMonth Day of the month of the date that was chosen
*/publicvoid onDateChosen(int year, int monthOfYear, int dayOfMonth);
}
}