Skip to content

Instantly share code, notes, and snippets.

@jie-meng
jie-meng / cloudSettings
Last active June 8, 2020 04:06
Visual Studio Code Settings Sync Gist
{"lastUpload":"2019-02-27T02:08:16.923Z","extensionVersion":"v3.2.4"}
@jie-meng
jie-meng / android_restart_app.java
Created November 29, 2017 07:59
Android Restart App
static void restart() {
killBackgroundProcesses();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
super.run();
PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
ComponentName componentName = intent.getComponent();
Intent mainIntent = Intent.makeRestartActivityTask(componentName);
@jie-meng
jie-meng / MainActivity.java
Last active October 18, 2017 05:44
Fix Android App restarts rather than resumes when launch from launcher bug. This is usually happen when you install an App from mobile device, package manager installs it then you would click 'OPEN', After App startup, press home button and then launch App from launcher. Then the app would brought to front and create a new MainActivity, that was…
//The behavior you are experiencing is caused by an issue that exists in some Android launchers since API 1. You can find details about the bug as well as possible solutions here: https://code.google.com/p/android/issues/detail?id=2373.
//It's a relatively common issue on Samsung devices as well as other manufacturers that use a custom launcher/skin. I haven't seen the issue occur on a stock Android launcher.
//Basically, the app is not actually restarting completely, but your launch Activity is being started and added to the top of the Activity stack when the app is being resumed by the launcher. You can confirm this is the case by clicking the back button when you resume the app and are shown the launch Activity. You should then be brought to the Activity that you expected to be shown when you resumed the app.
//The workaround I chose to implement to resolve this issue is to check for the Intent.CATEGORY_LAUNCHER category and Intent.ACTION_MAIN action in the intent that starts the initial Activity. If those
@jie-meng
jie-meng / HideMenuItem.java
Created October 13, 2017 04:54
Android hide menu item
//anywhere in your code
...
mState = HIDE_MENU; // setting state
invalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again
...
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// inflate menu from xml
@jie-meng
jie-meng / CreateProgressView.java
Last active October 11, 2017 14:55
createProgressView
public static View createProgressView(Activity activity) {
ViewGroup viewGroup = (ViewGroup) activity.findViewById(android.R.id.content);
View progressView = LayoutInflater.from(activity).inflate(R.layout.view_progress_bar, viewGroup, false);
viewGroup.addView(progressView);
return progressView;
}
@jie-meng
jie-meng / ButtonClickForegroundStyle.java
Created October 11, 2017 14:50
Add water ripple to Button
<Button
android:id="@+id/btn_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/dark_blue"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:textAllCaps="false"
android:textSize="@dimen/textsize_button"
android:textColor="@android:color/white"
android:text="@string/next"/>
@jie-meng
jie-meng / ApplyDimToActivity.java
Created October 11, 2017 14:29
Apply dim to Activity
public static void applyDim(@NonNull Activity activity){
float dimAmount = 0.5f;
ViewGroup rootView = (ViewGroup) (activity.getWindow().getDecorView().getRootView());
Drawable dim = new ColorDrawable(Color.BLACK);
dim.setBounds(0, 0, rootView.getWidth(), rootView.getHeight());
dim.setAlpha((int) (255 * dimAmount));
ViewGroupOverlay overlay = rootView.getOverlay();
overlay.add(dim);
}
@jie-meng
jie-meng / values.xml
Created October 11, 2017 08:13
Android Multiline Snackbar To override the predefined value used for that in values.xml of the app
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="design_snackbar_text_max_lines">5</integer>
</resources>
@jie-meng
jie-meng / ViewPagerInvalidateActionBar.java
Created October 11, 2017 07:29
ViewPager invalidate ActionBar
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
invalidateActionbar(position);
}
@jie-meng
jie-meng / EditTextInputFilter.java
Created October 10, 2017 09:40
EditText input filter
editText.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!somePattern.matcher(source).matches()) {
//return empty to refuse
return "";
} else {
//return null if accept
return null;