2015年11月25日 星期三

[Exception] The specified child already has a parent. You must call removeView() on the child's parent first.

Reference:
http://www.cnblogs.com/over140/archive/2013/06/06/3121354.html

Pre-condition:
Activity + Fragment

Solution:
View v = inflater.inflate(R.layout.preference_fragment, container, false);


Set the focus and display the keyboard on EditText

Reference:

http://stackoverflow.com/questions/8991522/how-can-i-set-the-focus-and-display-the-keyboard-on-my-edittext-programmatic

Code snippet:

EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)                     getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

2015年11月13日 星期五

Get screen shot bitmap

Code snippet:

public Bitmap getScreenShot(int x,int y,int width,int height){
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        final Bitmap screenShot = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        final Bitmap cropBitmap = Bitmap.createBitmap(screenShot,x,y,width,height);
        return cropBitmap.copy(cropBitmap.getConfig(),true);
}

2015年11月11日 星期三

Check if Google Play Service is available

Reference:

http://stackoverflow.com/questions/18737632/how-to-check-google-play-services-version
http://stackoverflow.com/questions/28479849/when-does-isgoogleplayservicesavailable-return-service-version-update-required
http://stackoverflow.com/questions/25989727/isgoogleplayservicesavailable-always-returns-2
http://www.4byte.cn/question/135275/googlemap-v2-crash-if-first-use-android.html

Code snippet:

public static void checkGooglePlayServiceAvailability(Activity activity){
        Log.d(TAG,"sdk gms versionCode: " + GooglePlayServicesUtil.GOOGLE_PLAY_SERVICES_VERSION_CODE);
        try{
            int deviceGmsVersion = activity.getPackageManager().getPackageInfo("com.google.android.gms",0).versionCode;
            Log.d(TAG,"" + "device gms versionCode: " + deviceGmsVersion);
        } catch(Exception e){
            e.printStackTrace();
        }

        int statusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
        Log.e(TAG,"statuscode: " + statusCode);
        Log.e(TAG,"error string: " + GooglePlayServicesUtil.getErrorString(statusCode));

        if(statusCode != ConnectionResult.SUCCESS){
            if(GooglePlayServicesUtil.isUserRecoverableError(statusCode)){
                Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(statusCode, activity, 0);
                // If Google Play services can provide an error dialog
                if (errorDialog != null) {
                    errorDialog.show();
                }
            }
        }
    }

2015年11月10日 星期二

Handle back key event

Reference:

http://stackoverflow.com/questions/5312334/how-to-handle-back-button-in-activity

Code snippet:

For API level 5

@Override
public void onBackPressed() {
    // your code.
}

For older then API 5 use this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // your code
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

2015年11月9日 星期一

Get the view's height when the views are not built in onCreate(), onStart(), or onResume()

Reference:
http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#4406090
http://stackoverflow.com/questions/8170915/getheight-returns-0-for-all-android-ui-objects
http://www.eoeandroid.com/thread-240677-1-1.html?_dsign=f73765c1

Code snippet:
private ViewTreeObserver mViewTreeObserver;
private TextView mTextView;
protected void onCreate(Bundle savedInstanceState) {
    .........
    mTextView = (TextView) findViewById(R.id.my_text_view);
    mViewTreeObserver = mTextView.getViewTreeObserver();
    mViewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {                      
                        int height = mTextView.getHeight();
                        if(!mViewTreeObserver.isAlive()){
                            mViewTreeObserver = mNewTrackingRelativeLayout.getViewTreeObserver();
                        }
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            mViewTreeObserver.removeOnGlobalLayoutListener(this);
                        } else {
                            mViewTreeObserver.removeGlobalOnLayoutListener(this);
                        }
                    }
                });
    .........
}


2015年11月6日 星期五

Add search button on keyboard and close the keyboard when click the search button

Reference:
http://stackoverflow.com/questions/3205339/android-how-to-make-keyboard-enter-button-say-search-and-handle-its-click
http://stackoverflow.com/questions/23528644/edittext-ignores-the-imeactionlabel
http://stackoverflow.com/questions/3400028/close-virtual-keyboard-on-button-press
http://stackoverflow.com/questions/13593069/androidhide-keyboard-after-button-click

Code snippet:
layout.xml

<EditText android:imeOptions="actionSearch"
    android:inputType="text"/>

class.java

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();

            // close the keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                      InputMethodManager.RESULT_UNCHANGED_SHOWN);
            return true;
        }
        return false;
    }
});