Auto Formatting Phone Numbers in EditText

Android provide PhoneNumberFormattingTextWatcher class to auto formatting phone numbers. All we have to do add TextChangedListener in our EditText.

phnNum = (EditText)findViewById(R.id.phoneNumber);
/*Set Input Type as Phone Number*/
phnNum.setInputType(InputType.TYPE_CLASS_PHONE);
/*Phone Number formatting*/
phnNum.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

There is also a Google phone number library for validating and formatting phone number.

Parse a CSV File Using Ruby

Ruby has a standard library for parsing CSV files. All we need to do is open a csv file, read it and parse the file using CSV parser. So, here is my CSV file.

repository_name,repository_language,repository_url,total_number_of_forks
bootstrap,JavaScript,https://github.com/twitter/bootstrap,14037
bootstrap,JavaScript,https://github.com//bootstrap,13700
Spoon-Knife,null,https://github.com/octocat/Spoon-Knife,13316
Spoon-Knife,null,https://github.com//Spoon-Knife,12831
homebrew,Ruby,https://github.com/mxcl/homebrew,5758

I will use CSV library. So, we need to add this

require 'csv'

Now, we need to read and parse the file and by specify the headers exist, the parse will use the first row as names for each value.

require 'csv'
csv_text = File.read("info.csv")
csv = CSV.parse(csv_text, :headers => true)

And now we just need to loop through each record for printing output in terminal.

require 'csv'
 csv_text = File.read("info.csv")
 csv = CSV.parse(csv_text, :headers => true)
 csv.each do |row|
 puts
 "Name: #{row['repository_name']} -
 Language: #{row['epository_language']} -
 URL: #{row['repository_url']} -
 Total Number of Forks: #{row['total_number_of_forks']}"
 end

Dismiss and Show UIPickerView

In the last post, I changed the background color using UIPickerView. Now, for a better looking UI. You may want to show and hide your PickerView. To do so, just follow this step

  • Create an outlet. Like
    IBOutlet UIPickerView *mypicker;
  • Create action for two buttons(show and hide). Inside action
    mypicker.hidden = Yes //hide PickerView
    mypicker.hidden = NO //show PIckerView

 

Basic iOS application using UILabel,UITextField & UIAlertView

Basic iOS Application using UILabel,UITextField,UIAlert

At first add that three elements(TextField,Label and Button) on your user interface designer file. The idea of this application is so simple. If you write something in your text field. It will show the message via label and alert.

This is a single view application and for this application we just need to deal with ViewController.m and ViewController.h file. So here is my code

ViewController.h:

//ViewController.m

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//Button Action
- (IBAction)changeWord:(id)sender;

//Outlet Label
@property (weak, nonatomic) IBOutlet UILabel *MyLabel;
//Outlet TextField
@property (weak, nonatomic) IBOutlet UITextField *MyTextField;

@end

 

ViewController.m:

#import "ViewController.h"

@implementation ViewController
@synthesize MyLabel;
@synthesize MyTextField;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setMyLabel:nil];
    [self setMyTextField:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

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

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

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (IBAction)changeWord:(id)sender {
    NSString *message = [[NSString alloc]initWithFormat:@"%@",[MyTextField text]];
    UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Alert"message:[MyTextField text]
        delegate:nil
        cancelButtonTitle:@"Okay"
        otherButtonTitles: nil];
    [myAlert show];

    [MyLabel setText:message];

    [MyTextField resignFirstResponder];//Dismiss the Keyboard
}
@end

How to Solve Android SDK Error : Unable to resolve target ‘android-x’

Currently, I’m working on my Java semester final project. It’s an Android application which can control several task of computer . So, I found a similar project on internet and imported that project in eclipse. But, It showed me an error Unable to resolve target ‘android-7 ‘ and I found the solution. If you’re facing this messages

Unable to resolve target 'android-1'
Unable to resolve target 'android-2'
Unable to resolve target 'android-3'
Unable to resolve target 'android-4'
Unable to resolve target 'android-5'
Unable to resolve target 'android-6'
Unable to resolve target 'android-7'
Unable to resolve target 'android-8'
Unable to resolve target 'android-9'
Unable to resolve target 'android-10'
Unable to resolve target 'android-11'
Unable to resolve target 'android-12'
Unable to resolve target 'android-13'
Unable to resolve target 'android-14'
Unable to resolve target 'android-15'

It means system couldn’t find the proper Android SDK  version. If the android SDK is installed correctly the problem is that the platform SDK requested by the “default.properties” is not installed. Like :

Unable to resolve target 'android-1' - (Android 1.0) change the "default.properties"
Unable to resolve target 'android-2' - (Android 1.1) change the "default.properties"
Unable to resolve target 'android-3' - install SDK Platform Android 1.5
Unable to resolve target 'android-4' - install SDK Platform Android 1.6
Unable to resolve target 'android-5' - install SDK Platform Android 2.0
Unable to resolve target 'android-6' - install SDK Platform Android 2.0.1
Unable to resolve target 'android-7' - install SDK Platform Android 2.1
Unable to resolve target 'android-8' - install SDK Platform Android 2.2
Unable to resolve target 'android-9' - install SDK Platform Android 2.3
Unable to resolve target 'android-10' - install SDK Platform Android 2.3.3
Unable to resolve target 'android-11' - install SDK Platform Android 3.0
Unable to resolve target 'android-12' - install SDK Platform Android 3.1
Unable to resolve target 'android-13' - install SDK Platform Android 3.2
Unable to resolve target 'android-14' - install SDK Platform Android 4.0
Unable to resolve target 'android-15' - install SDK Platform Android 4.0.3

You can fix this by Android SDK setup utility.

  • Eclipse – “Windows” \ “Android SDK and AVD Manager” \ “Available packages

You might need this(Questions form stackoverflow)

Android change SDK version in Eclipse? Unable to resolve target android-x

Android requires compiler compliance level 5.0 or 6.0. Found ’1.7′ instead. Please use Android Tools > Fix Project Properties

Drop down List in Ruby on Rails

I will create a drop down menu to allow a user to change an entry’s field in my table.  There will be three priority option for user “High”, “Medium”, “Low”. So, I added a column in my db table priority:string. Now, I will add this in my _form.html.erb file.

<%= f.select :priority, options_for_select([["High", "high"], ["Medium", "medium"],["Low","low"]], @task.priority) %>

This will make a drop down list on your form. I know, this is too easy. But, I write this reference for beginners.

Configure GrameenPhone EDGE Modem in Linux Mint

Required Packages:

  • libssl0.9.8_0.9.8o-7ubuntu1_i386.deb
  • libuniconf4.6_4.6.1-1_i386.deb
  • libwvstreams4.6-base_4.6.1-1_i386.deb
  • usb-modeswitch_1.1.9-1ubuntu3_i386.deb
  • wvdial_1.61-4_i386.deb

Download this packages. Now,open you terminal and type

sudo nautilus
  • Then go File System->var->cache->apt->archives and copy those packages in archives folder.
  • Open you terminal and type
    sudo dpkg-i*.deb
  •  Now, connect your modem & re-start your pc.
  • After that,you will see a new broadband connection.
  • Now, Configure your broadband connection. It’s quite easy. Just choose the country name & service provider name from the list.

** I was tested this on Linux Mint 12.

Configure and Install ORACLE Java JDK 1.7.0_02 in Linux

Download 32 bit or 64 bit Linux “compressed binary file” – it has a “.tar.gz” file extension. Like ”[java-version]-i586.tar.gz

  • After downloading. Uncompress it.
tar -xvf jdk-7u2-linux-i586.tar.gz (32bit)
tar -xvf jdk-7u2-linux-x64.tar.gz (64bit)
  • Extract this package in ./jdk1.7.0_02 directory.  Now, move the directory to /usr/lib/jvm/
sudo mv ./jdk1.7.0_02 /usr/lib/jvm/
  • Now, run this in your terminal
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_02/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_02/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0_02/bin/javaws" 1
  • Then check the version of JDK
java -version

java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) Client VM (build 22.0-b10, mixed mode)

 

NSMutableArray and NSArray

NSMutableArray is the subclass of NSArray .The difference between NSArray & NSMutableArray is you can not modified NSArray. Like sometime later if you want to add,remove or replace object, you will not able to do with the regular array(NSArray) but NSMutableArray does this for you. So,I write a code with some popular methods of NSMutableArray. I will show you how add,remove and replace an object.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        NSMutableArray *myArray =[NSMutableArray arrayWithCapacity:20];
        /*add an object*/
        for (int i=2; i<=100;i+=2)
        {
                [myArray addObject:[NSNumber numberWithInt:i]];

        }
        /* Remove specified index*/

        [myArray removeObjectAtIndex:2]; 

        /*Remove lastObejctAtIndex*/

        [myArray removeLastObject]; 

        /*Replace Specified Index*/

        [myArray replaceObjectAtIndex:3 withObject:@"100"];

        for(int x=0;x<[myArray count];x++)
        {

            NSLog(@"Item here is %i",[[myArray objectAtIndex:x]intValue]);
        }

    return 0;
}

here,we declared myArray and arrayWithCapacity means how many objects you want to store in this array. I set it to 20.But richest feature of mutable array is, if you add more than 20 objects it automatically expand it’s size.Then,I run a loop for printing all the even numbers between 2 to 100. In the next line,I removed an object in index[2]=6.Our last object is 100. So,removeLastObject will remove from the array. We can also replace our index by using replaceObjectAtIndex, here I replaced object 8 with 100 which is in index[3]. For more information you can check the Official apple documentation about NSMutableArray.