Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@raviyadav4875
Last active January 23, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raviyadav4875/8b4e01bcbcd22c795238 to your computer and use it in GitHub Desktop.
Save raviyadav4875/8b4e01bcbcd22c795238 to your computer and use it in GitHub Desktop.
How to read static json response,hide soft keyboard,check internet connection,show Toast from single utility java class in Android Studio
public class MyUtils {
/**
* get date in UTC time format
*
* @param date
* @return
*/
public static String current_datetime_To_UTC(Date date) {
SimpleDateFormat actualdate_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
actualdate_format.setTimeZone(TimeZone.getTimeZone("UTC"));
String Utc_Time = actualdate_format.format(date);
return Utc_Time;
}
/**
* Use to expand view
*
* @param v
*/
public static void expand(final View v) {
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LayoutParams.WRAP_CONTENT
: (int) (targtetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(600);
// a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
/**
* Use to collapse view
*
* @param v
*/
public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(600);
//a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
/**
* To fetch base64 of image.
* @param bitmapInput
* @return
*/
public static String getBase64ofBitmap(Bitmap bitmapInput) {
String encoded = "";
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmapInput.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP);
} catch (Exception e) {
LOG.logE("Exception in getBase64ofBitmap:"+e.getLocalizedMessage());
}
return encoded;
}
public static int getDeviceWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
return width;
}
/**
* Convert date in string format to another string format
*
* @param stringdate
* @param inputFormat
* @param outFormat
* @return
*/
public static String convertStringDate(String stringdate, String inputFormat, String outFormat) {
String strDateTime = "";
try {
DateFormat inputFormatter = new SimpleDateFormat(inputFormat, Locale.getDefault());
Date da = (Date) inputFormatter.parse(stringdate);
// System.out.println("==Date is ==" + da);
DateFormat outputFormatter = new SimpleDateFormat(outFormat, Locale.getDefault());
strDateTime = outputFormatter.format(da);
// System.out.println("==String date is : " + strDateTime);
return strDateTime;
} catch (ParseException e) {
e.printStackTrace();
}
return strDateTime;
}
/**
* Get Hours from string date
*
* @param stringdate
* @return
*/
public static int getHoursFromStringDate(String stringdate, String inputFormat) {
int hours_24 = 0;
try {
DateFormat inputFormatter = new SimpleDateFormat(inputFormat, Locale.getDefault());
Date date = (Date) inputFormatter.parse(stringdate);
hours_24 = date.getHours(); // int
} catch (ParseException e) {
e.printStackTrace();
}
return hours_24;
}
/**
* This method returns any json as String to calling method
* @param context
* @return
*/
public static String readJsonForMe(Context context) {
InputStream inputStream = context.getResources().openRawResource(R.raw.test_json);
System.out.println(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
/**
* Check internet connection before calling any code which requires internet to proceed
* (e.g Calling Webservice
* @param context
* @return
*/
public static boolean isInternetAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
/**
* Display custom Toast
* @param context
* @param message
*/
public static void showToast(Context context,String message)
{
Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
}
/**
* DEmo internet connection alert
* @param context
*/
public static void showAlert(Context context) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Connection Error");
// set dialog message
alertDialogBuilder
.setMessage("No Internet Connection Available")
.setCancelable(true)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
/**
* Hide keyboard
*/
public static void hideKeyboard(Activity activity) {
if (activity.getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
/**
* To dial number
* @param context
* @param number
*/
public static void call(Context activity, String number) {
if (number != null && number.length() > 2) {
String uri = "tel:" + number;
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(uri));
activity.startActivity(intent);
} else {
Toast.makeText(activity, Constants.CANNT_CALL_THIS_NUMBER, Toast.LENGTH_SHORT).show();
}
}
/**
* Text message / SMS
* @param context
* @param number
*/
public static void message(Context context, String number) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", number);
smsIntent.putExtra("sms_body", "Body of Message");
context.startActivity(smsIntent);
}
/**
* To display the difference between current time and time from server response.
* Take care before using this method as this mrthod was for my specific requirement.
* @param current
* @param end
* @return
*/
public static String calcDifference(Date current, Date end) {
String left = "";
try {
long diff = current.getTime() - end.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);
int diffInDays = (int) (diff) / (1000 * 60 * 60 * 24);
if (diffInDays > 2) {
left = Utility.getDateInString(end, date_yyyy_MM_dd);
} else if (diffInDays > 1 && diffInDays <= 2) {
left = diffInDays + " days ago";
} else if (diffInDays == 1) {
left = diffInDays + " day ago";
} else if (diffHours > 0 && diffHours < 24) {
if (diffHours == 1) {
left = diffHours + " hour ago";
} else {
left = diffHours + " hours ago";
}
} else if ((diffMinutes >= 1) && (diffMinutes < 60)) {
if (diffMinutes == 1) {
left = diffMinutes + " minute ago";
} else {
left = diffMinutes + " minutes ago";
}
} else {
left = "just now";
}
} catch (Exception e) {
e.printStackTrace();
Debug.e(TAG,"calcDifference exception=="+e.toString());
}
return left;
}
}
@mkamals1989
Copy link

Hi,

Can you share your custom Utility class file which is used here in calcDifference method?.

Thank You.

@raviyadav4875
Copy link
Author

@mkamals1989 What you need actually. I have used this method for showing difference for chat message timings. Here I have passed two dates as you can see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment