Skip to content

Instantly share code, notes, and snippets.

@devrath
Created December 15, 2015 04:55
Show Gist options
  • Select an option

  • Save devrath/8664fb832e488a19268b to your computer and use it in GitHub Desktop.

Select an option

Save devrath/8664fb832e488a19268b to your computer and use it in GitHub Desktop.
List of all the functions that can be used in All the projects
public class CommonFunctions {
public static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
public static Activity context;
/*********************************************Network Check*********************************/
public static boolean isOnline(Activity _context) {
context=_context;
ConnectivityManager cm =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
/*********************************************Network Check*********************************/
/************************************ ShowSnackbar with message, KeepItDisplayedOnScreen for few seconds*****************************/
public static void showSnakbarTypeOne(View rootView, String mMessage) {
Snackbar.make(rootView, mMessage, Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show();
}
/************************************ ShowSnackbar with message, KeepItDisplayedOnScreen*****************************/
public static void showSnakbarTypeTwo(View rootView, String mMessage) {
Snackbar.make(rootView, mMessage, Snackbar.LENGTH_LONG)
.make(rootView, mMessage, Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null)
.show();
}
/************************************ ShowSnackbar without message, KeepItDisplayedOnScreen, OnClickOfOk restrat the activity*****************************/
public static void showSnakbarTypeThree(View rootView, final Activity activity) {
Snackbar
.make(rootView, R.string.snackbarNoConnectivity, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.snackbarTryAgain, new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = activity.getIntent();
activity.finish();
activity.startActivity(intent);
}
})
.setActionTextColor(Color.CYAN)
.setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
}
@Override
public void onShown(Snackbar snackbar) {
super.onShown(snackbar);
}
})
.show();
}
/************************************ ShowSnackbar with message, KeepItDisplayedOnScreen, OnClickOfOk restrat the activity*****************************/
public static void showSnakbarTypeFour(View rootView, final Activity activity, String mMessage) {
Snackbar
.make(rootView, mMessage, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.snackbarTryAgain, new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = activity.getIntent();
activity.finish();
activity.startActivity(intent);
}
})
.setActionTextColor(Color.CYAN)
.setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
}
@Override
public void onShown(Snackbar snackbar) {
super.onShown(snackbar);
}
})
.show();
}
/************************************ Hiding the keyboard *****************************/
public static void hideKeyboard(Context context, View view) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
/********************************** Hiding the keyboard ******************************/
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
/********************************** Make StatusBar Translucent******************************/
public static void makeStatusBarTranslucent(Activity activity) {
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
/********************************** Make StatusBar,NavigationBar Hidden******************************/
public static void makeStatusBarNavigationBarHidden(Activity activity) {
View decorView = activity.getWindow().getDecorView();
int uiOptions =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar
decorView.setSystemUiVisibility(uiOptions);
}
/********************************** SetBackgroundDrawable******************************/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) @SuppressWarnings("deprecation")
public static void setBackgroundDrawable(View mView, Activity mActivity, int mDrawableResource) {
if (mView instanceof Button) {
//Button
Button mButton = (Button) mView;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mButton.setBackgroundDrawable(ContextCompat.getDrawable(mActivity, mDrawableResource));
} else {
mButton.setBackground(ContextCompat.getDrawable(mActivity, mDrawableResource));
}
}else if(mView instanceof TextView){
//TextView
TextView mTextView = (TextView) mView;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mTextView.setBackgroundDrawable(ContextCompat.getDrawable(mActivity, mDrawableResource));
} else {
mTextView.setBackground(ContextCompat.getDrawable(mActivity, mDrawableResource));
}
}
}
/**********************************StartActivity
* @param mSourceActivity
* @param mDestinationActivity
* ******************************/
public static void startActivity(Activity mSourceActivity, Class<?> mDestinationActivity) {
Intent mIntent = new Intent(mSourceActivity,mDestinationActivity);
mSourceActivity.startActivity(mIntent);
mSourceActivity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
/**********************************ThrowExceptionToParentFunctionAndPrintTheTrace**********************************/
public static void throwException(Exception e) throws Exception {
if(AppConstants.isErr==false){
AppConstants.errMsg=e.toString();
AppConstants.isErr=true;
throw e;
}
}
/**********************************ThrowExceptionToParentFunctionAndPrintTheTrace**********************************/
public static void catchThrownException(Exception e){
if(AppConstants.isErr==false){
e.printStackTrace();
AppConstants.isErr=true;
}
}
/**********************************ResetErrorhandelingVariables**********************************/
public static void resetErrorHandelingVariables() {
AppConstants.errMsg="";
AppConstants.isErr=false;
}
/**.
* This value will not collide with ID values generated at build time by aapt for R.id.
*
* @return a generated ID value
*/
public static int generateViewId() {
for (;;) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
/**********************************ShowLoading**********************************/
public static ProgressDialog showLoadingDialog(ProgressDialog progress, Activity activity) {
if (progress == null) {
progress = new ProgressDialog(activity);
progress.requestWindowFeature(Window.FEATURE_NO_TITLE);
// progress.setTitle("Loading ...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setCancelable(false);
progress.setMessage("Loading. Please wait...");
// progress.setMessage("Please wait");
}
progress.show();
return progress;
}
/**********************************DismissLoading**********************************/
public static void dismissLoadingDialog(ProgressDialog progress) {
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
}
//FORMAT-FOUR-DATE
public static String getLockScrenDateFormat(String str) {
String mDate= str;
String newDateString = null;
DateFormat srcDateFrm = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
DateFormat destDateFrm = new SimpleDateFormat("EEE, MMM dd",Locale.ENGLISH);
Date finalDate;
try {
finalDate = srcDateFrm.parse(mDate);
newDateString = destDateFrm.format(finalDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
return newDateString;
}
/**********************************ShowLoading**********************************/
public static TransparentProgressDialog showCircularLoadingDialog(TransparentProgressDialog progress, Activity activity) {
if (progress == null) {
progress = new TransparentProgressDialog(activity, R.drawable.loader);
}
progress.show();
return progress;
}
/**********************************DismissLoading**********************************/
public static void dismissCircularLoadingDialog(TransparentProgressDialog progress) {
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment