Tuesday, June 21, 2011

Release Memory, Valid URL, Valid Email, Valid Phone

1) How we release a memory in android ?
Answer :-
Suppose if we use in our activity
                     Button btnYes;
                     LinearLayout llMain;
                     Gallery gallery
                     ImageButton btnSearch
                     ImageView backImageView;
 

@Override
 protected void onPause() {
       releaseViewMemory(btnYes);
       releaseViewMemory(llMain);
       releaseViewMemory(gallery);
                                   releaseViewMemory(btnSearch);         
                                   releaseImageViewMemory(backImageView);
                                   super.onPause();
                             }

public static void releaseViewMemory(View view) {
    if (view != null) {
        Drawable draw = view.getBackground();
        if (draw != null) {
           draw.setCallback(null);
           draw = null;
        }
   }
}

public static void releaseImageViewMemory(ImageView imgView) {
     if (imgView != null) {
         Drawable draw = imgView.getBackground();
         if (draw != null) {
            draw.setCallback(null);
            draw = null;
         }
         draw = imgView.getDrawable();
         if (draw != null) {
            draw.setCallback(null);
            draw = null;
         }
     }
}

2) Write a method for valid url.
Answer :-
 

public static boolean isValidURL(String url) {
    boolean isValid = false;
    String expression = "^(http|https|ftp)\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\\-\\._\\?\\,\\'/\\\\+&%\\$#\\=~])*$";
   CharSequence inputStr = url;
   Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
   Matcher matcher = pattern.matcher(inputStr);
   if (matcher.matches()) {
      isValid = true;
   }
   return isValid;
}

3) Write a method for valid email.
Answer :-
 
public static boolean isValidEmail(String email) {
    boolean isValid = false;
    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}quot;;
    CharSequence inputStr = email;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}



3) Write a method for valid phone.
Answer :-
 
public static boolean isValidphone(String phone) {
    boolean isValid = false;
    String expression = "^[+][0-9]{10,13}$";
    CharSequence inputstr = phone;
    if (phone.length() == 0)
        return false;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputstr);
    if (matcher.matches()) {
        if (phone.length() == 10)
            isValid = true;
        }
        return isValid;
   }


3 comments: