Skip to content

Instantly share code, notes, and snippets.

@PepDevils
Last active June 30, 2022 14:20
Show Gist options
  • Save PepDevils/106657966138afdb365a94d8aeab63c5 to your computer and use it in GitHub Desktop.
Save PepDevils/106657966138afdb365a94d8aeab63c5 to your computer and use it in GitHub Desktop.
//more on:
// http://www.androidcodesnippets.com
// http://www.androidsnippets.com
// https://github.com/changer/android-utils/blob/master/Utils/app/src/main/java/nl/changer/android/opensource/Utils.java
static void TintDrawable(Context c, int drawable, int color){
//example: HelperUtils.TintDrawable(this, R.drawable.logo,R.color.colorPrimaryDark);
Drawable mDrawable = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
mDrawable = c.getResources().getDrawable(R.drawable.x, null);
}else{
mDrawable = c.getResources().getDrawable(R.drawable.x);
}
mDrawable.setColorFilter(ContextCompat.getColor(c, R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);
}
public static void TintDrawable(Context c, ImageView iv, int color){
Drawable mDrawable = iv.getDrawable();
mDrawable.setColorFilter(ContextCompat.getColor(c,color), PorterDuff.Mode.SRC_ATOP);
}
static Intent buildIntentWithAction(Context context, Class clazz, String action) {
Intent intent = new Intent(context, clazz);
intent.setAction(action);
return intent;
}
static void SetTextWintSp(TextView tv, int dimension){
//dimension = R.dimen.ts_med_regular
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getActivity().getResources().getDimensionPixelSize(dimension));
}
static void MakeActivitiesTransitions(Activity a) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Transition transition = new Explode();
Transition transition_ = new Fade();
a.getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
a.getWindow().setEnterTransition(transition);
a.getWindow().setExitTransition(transition);
a.getWindow().setSharedElementEnterTransition(transition_);
a.getWindow().setSharedElementExitTransition(transition_);
}
}
static void ShowStatusBar(AppCompatActivity a) {
//show status bar
/* View decorView = a.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);*/
//change color of statusbar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = a.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(a.getResources().getColor(R.color.colorPrimaryDark, null));
}
//show action bar
/* ActionBar actionBar = a.getSupportActionBar();
actionBar.show();*/
a.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
/* | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);*/
}
static void HideStatusBar(AppCompatActivity a) {
//hide status bar
/* View decorView = a.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
// int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);*/
//change color of statusbar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = a.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLACK);
}
//hide action bar
/*ActionBar actionBar = a.getWindow().getSupportActionBar();
actionBar.hide();*/
a.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
public static void saveObjectInSharedPref(Context c, News not, String tag, int total) {
try {
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(not);
prefsEditor.putInt("total", total);
prefsEditor.putString(tag, json);
prefsEditor.apply();
} catch (Exception e) {
Log.d("CacheDatePepeError:", "saveObjectInFileCache: " + e.getMessage());
}
}
public static ArrayList<News> getObjectInSharedPref(Context c, String tag) {
ArrayList<News> notic = new ArrayList<>();
try {
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(c);
Gson gson = new Gson();
int total = appSharedPrefs.getInt("total", 0);
if (total > 0) {
for (int i = 0; i < total; i++) {
String json = appSharedPrefs.getString(tag + i, "");
News n = gson.fromJson(json, News.class);
notic.add(n);
}
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
return notic;
}
public static float pxToDp(float px) {
float densityDpi = Resources.getSystem().getDisplayMetrics().densityDpi;
return px / (densityDpi / 160f);
}
public static int dpToPx(int dp) {
float density = Resources.getSystem().getDisplayMetrics().density;
return Math.round(dp * density);
}
public static void hideKeyboard(Activity activity) {
InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
}
public static boolean isNetworkConnected(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
static void TranslucideSystemDef(AppCompatActivity a) {
//colocar este metodo antes da "setContentView(R.layout.activity_main);" na Activity
a.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
a.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
a.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
a.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
a.getWindow().setStatusBarColor(a.getResources().getColor(R.color.black_overlay));
a.getWindow().setNavigationBarColor(a.getResources().getColor(R.color.black_overlay));
} else {
a.getWindow().setStatusBarColor(a.getResources().getColor(R.color.black_overlay, a.getTheme()));
a.getWindow().setNavigationBarColor(a.getResources().getColor(R.color.black_overlay, a.getTheme()));
}
//res/color
<color name="black_overlay">#AA000000</color>
//res/styles
<style name="MyFullscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment