Accumulated knowledge of Android development

Install Android NDK

First download it from this.

Unzip it to a folder,like :

C:\Users\JuniperPhoton\AppData\Local\Android\ndk\android-ndk-r12b

Then in local.Properties file, append the following line:

ndk.dir=C\:\\Users\\JuniperPhoton\\AppData\\Local\\Android\\ndk\\android-ndk-r12b

OK. That's done.

Handle back pressed logic

First override onBackPressed() method,then you can implement your logic like closing NavigationDrawer. If you would like to back to homescreen just like pressing the home button, do NOT call super.onBackPressed(), which will exit your app.

@Override
    public void onBackPressed() {
        if () {
            //Handle your logic
        }
        } else {
            //super.onBackPressed();
            //Back to homescreen
            Intent intent= new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
        }
    }

Scale a Bitmap to specified size

            Bitmap bmp = BitmapFactory.decodeResource(getResources().getDrawable(R.drawable.white_tick));
            //Alternately, you can convert a Drawable to Bitmap
            //Bitmap bmp=((BitmapDrawable)mDrawable).getBitmap();

            //Make a matrix to scale the bitmap
            Matrix matrix = new Matrix();
            float scaleWidth = targetWidth / bmp.getWidth();
            float scaleHeight = targetHeight / bmp.getHeight();
            matrix.postScale(scaleWidth, scaleHeight);

            Bitmap destBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
            
            //In a custom view,now you can draw the bitmap in targetsize.
            canvas.drawBitmap(destBmp, 0, 0, null);

Aoid to serialize fields of specifed type in Gson

Just like BitmapImage in UWP, in Android developing, you can't serialize or deserialize Drawable/Bitmap. To avoid this, you should make an annotation @Expose to annotate that this field should be serialized or deserialized, just like this:

@Expose private Drawable mDrawable;

Then, here is how to use gson builder:

Gson gson = builder.excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization().create();

That wil be depressed that we can't tell which fields should NOT be serialized, instead, you can only specifiy which ones should be serialized. So if your class A contains Class B and the instance of Class A is going to be serialized, the fields in Class B which you would like to be serialized should all be annotated @Expose.

Dismiss keyboard

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEditedText.getWindowToken(),0);