Monthly Archives: October 2012

Android using SharedPreferences

Rather than having to call PreferenceManager.getDefaultSharedPreferences everytime in different activities, and have to keep track of all the keys in your application, it is easier to keep all preferences in one place. For that I create a wrapper class just for my preferences.

PreferencesClass.java

class PreferencesClass {
    static String preference_name = "my_preferences";
    static String preference_one = "preference_one";
    static String preference_two = "preference_two";
    static String preference_three = "preference_three";
    private static SharedPreferences getPreference(Context context){
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static int getPreferenceOne(Context context){ return getPreference(context).getInt(preference_one,0); }
    public static int setPreferenceOne(Context context, int value){ getPreference(context).edit().putInt(preference_one, value).commit(); }

public static int getPreferenceTwo(Context context){ return getPreference(context).getString(preference_two,null); }
    public static int setPreferenceTwo(Context context, string value){ getPreference(context).edit().putString(preference_two, value).commit(); }
    public static int getPreferenceThree(Context context){ return getPreference(context).getBoolean(preference_three,false); }
    public static int setPreferenceThree(Context context, boolean value){ getPreference(context).edit().putBoolean(preference_one, value).commit(); }

To use the method simply call the following in your activity

int preference_one = PreferencesClass.getPreferenceOne(getContext());
Advertisements

Check if virtual keyboard is up

As most of you can find in internet, there is no easy way to check if the soft keyboard is shown on the screen. Depending on what you are looking to achieve, for Touch Screen only application, I use the following solution. Might not be the perfect solution for you but it works for me.

As this is TOUCH only application, so I assume soft keyboard will only get triggered by when one of the edit text field got touch. So I set a public variable for the flag, false by default. When user click on either one of the edit text field, touch listener will set the flag to true, and when focused came back to main view, I will set the flag back to false in dispatchTouchEvent method.

Setting the flag

//This is my flag
public boolean isSoftKeyboardDisplayed = false;

Now, set listener for the edit text field

// This is my edit text field box
EditText editText = (EditText)findViewById(R.id.edittext);
editText.setOnTouchListener(new onTouchListener(){
@Override
public void onTouch(View v, MotionEvent event){
// The flag is set to true everytime the edit text is touched.
isSoftKeyboardDisplayed = true;
}
});

Dismiss the soft keyboard by implementing  dispatchTouchEvent to your root view. So when user touch any where other than the edit text field, the soft keyboard dissapear

// This part is to dismiss the softkeyboard when you click anywhere other than the edit text
@Override
public boolean dispatchTouchEvent(MotionEvent event) {

View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
// This is where I set the flag back to false when my soft keyboard is hidden
isSoftKeyboardDisplayed= false;
}
}
return ret;
}

As a result, I just check the isSoftKeyboardDisplayed flag for my keyboard display check.

Simple Android Animation on View GONE/VISIBLE

If you just want a better transition view on GONE / VISIBLE rather than the default Android show/hide, I came across  some simple trick to make a simple animation using TranslateAnimation class.

*Note: For a better transitioning, you can use animationlistener , and setVisibility onAnimationStart() or onAnimationEnd().


// View to be animate (I use ImageView as example here)
ImageView imageView = (ImageView)findViewById(R.id.imageview);

Below are the methods to set view from VISIBLE to GONE


// To animate view slide out from left to right
public void slideToRight(View view){
TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
// To animate view slide out from right to left
public void slideToLeft(View view){
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}

// To animate view slide out from top to bottom
public void slideToBottom(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}

// To animate view slide out from bottom to top
public void slideToTop(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}