Prevent Launch Browser When Calling loadurl on Webview


WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("http://www.example.com");



private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

Filtering data in EditText component in Android


dialogNric.setKeyListener(new NumberKeyListener()
{
@Override
public int getInputType()
{
return InputType.TYPE_MASK_VARIATION;
}
@Override
protected char[] getAcceptedChars()
{
return new char[]{'i','o','p','a','s','d','f','g','h','j','k','l','z','x','q','w','e','r','t','y','u','c','v','b','n','m','1','2','3','4','5','6','7','8','9','0'};
}
});

validate an phone number on Android


public final static boolean isValidPhone(CharSequence target) {
   if (target == null) {
       return false;
   } else {
       return android.util.Patterns.PHONE.matcher(target).matches();
   }
}

Validate an e-mail address on Android






public final static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}




Detect Shake


In your view controller:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

-(BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.type == UIEventSubtypeMotionShake) {
        //Your code here
        NSLog(@"Shake");
    }
}

Full Screen Dialog


final Dialog instructionDialog = new Dialog(getParent(), android.R.style.Theme);
instructionDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
instructionDialog.setContentView(getLayoutInflater().inflate(R.layout.dialog_instruction
       , null));
instructionDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Button btn = (Button) instructionDialog.findViewById(R.id.btn_instruction);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
instructionDialog.dismiss();
}
});
instructionDialog.show();

Android's NSUserDefaults


//--Init
int myvar = 12;


//--SAVE Data
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", MODE_PRIVATE);  
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("var1", myvar);
editor.commit();


//--READ data       
myvar = preferences.getInt("var1", 0);  

Add Done Button on Top of PickerView


UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                    delegate:nil
                                                    cancelButtonTitle:nil
                                                    destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];

[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

List All Fonts on iPhone


  NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
  NSArray *fontNames;
  NSInteger indFamily, indFont;
  for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
  {
      NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
      fontNames = [[NSArray alloc] initWithArray:
          [UIFont fontNamesForFamilyName:
          [familyNames objectAtIndex:indFamily]]];
      for (indFont=0; indFont<[fontNames count]; ++indFont)
      {
          NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
      }
  }