Thursday, October 25, 2012

Split one string into different strings iPhone


Split one string into different strings



NSString *str = @"011597464952,01521545545,454545474,454545444|Hello this is were the message is.";
    
    NSArray *firstSplit = [str componentsSeparatedByString:@"|"];
    NSAssert(firstSplit.count == 2@"Oops! Parsed string had more than one |, no message or no numbers.");
    NSString *msg = [firstSplit lastObject];
    NSArray *numbers = [[firstSplit objectAtIndex:0componentsSepratedByString:@","];
    
    // print out the numbers (as strings)
    for(NSString *currentNumberString in number) {
        NSLog(@"Number: %@", currentNumberString);
    }

Wednesday, October 24, 2012

IPhone Custom Toolbar on Keyboard to Hide keyboard

IPhone Custom Toolbar on Keyboard

MY requirement was to work on numeric keys not alphabets so i have used Numeric Pad on UITEXTFIELD  and you all know that by default there is no button on numeric pad for hidden keyboard. You have to create Custom Button on ur numeric pad and call [numberTextField resignFirstResponder]; to hide numeric pad.

Solution

I have create a custom toolbar and have put two buttons on it 1) Apply and 2) Cancel. And created a method for both buttons.


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];
    numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField resignFirstResponder];
}

Thats it .. 
You can add Custom button on left side of 0 button. 



Tuesday, October 23, 2012

How To Install Android SDK


How To Install Android SDK
Here you will get complete guide how to install android SDK on you computer to get started will android. Installing android is not a big deal but there are 3 to 4 things to install to get started and that's what makes android installation complicated for some user.

Step 1
For Android SDK you must have Windows XP, Windows Vista or Windows 7.

Step 2
Install JDK or JRE



http://www.oracle.com/technetwork/java/javase/downloads/index.html

Step 3
Install Eclipse





For 32 bit
http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6.1-201009090800/eclipse-SDK-3.6.1-win32.zip

For 64 bit
http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6.1-201009090800/eclipse-SDK-3.6.1-win32-x86_64.zip


Step 4
Install Android SDK



http://developer.android.com/sdk/index.html

Step 5

http://developer.android.com/sdk/eclipse-adt.html

After installing Eclipse and Android SDK you must install Android Development Tools (ADT) Plugin to built android project in eclipse.

1- Start Eclipse then Help -> Install New Software
2- Click Add, in the top-right corner.
3- In the Add Repository dialog that appears, type  following URL for the Location:

    https://dl-ssl.google.com/android/eclipse/

4-In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
5-In the next window, you'll see a list of the tools to be downloaded. Click Next.
6-Read and accept the license agreements, then click Finish.
7-When the installation completes, restart Eclipse

Step 6
Open Eclipse then Windows . If you will see Android SDK and AVD Manager then it mean that you can now start development in android
If you don't see Android Project in New then go to Other and select Android Project.
After Selecting Android Project if Built Target is empty then go to Windows ->  Preferences and select android then locate you installed SDK in SDK location 

Click here to get all links that will help you to install android SDK


Core Plot Custom Themes in iPhone

Core Plot Custom Themes in iPhone


    CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme]; 
    graph = (CPXYGraph *)[theme newGraph];
    
    graph.fill = [CPFill fillWithColor:[CPColor clearColor]];
    graph.plotAreaFrame.fill = [CPFill fillWithColor:[CPColor clearColor]];

You can apply following Themes in Core Plot

themeNamed:kCPPlainWhiteTheme
themeNamed:kCPTDarkGradientTheme
themeNamed:kCPTPlainBlackTheme
themeNamed:kCPTSlateTheme
themeNamed:kCPTStocksTheme

Multiple PickerViews in one View


Multiple PickerViews in one View


Solution

if([pickerView isEqual:pickerView2])
    {
        return 3;
    }
    if ([pickerView isEqual:pickerView1]) {
        return 4;
    }


Complete Source Code

ViewController.h

@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
    IBOutlet UILabel *mlabel;
    NSMutableArray *arrayNo;
    IBOutlet UIPickerView *pickerView2;
    NSMutableArray *arrayNo1;
    IBOutlet UIPickerView *pickerView1;

}
@property (nonatomic, retain) UILabel *mlabel;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
@synthesize mlabel;

- (void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@" 100 "];
[arrayNo addObject:@" 200 "];
[arrayNo addObject:@" 400 "];
[arrayNo addObject:@" 600 "];
[arrayNo addObject:@" 1000 "];
[pickerView2 selectRow:1 inComponent:0 animated:NO];
    
    arrayNo1 = [[NSMutableArray alloc] init];
[arrayNo1 addObject:@" 1001 "];
[arrayNo1 addObject:@" 2001 "];
[arrayNo1 addObject:@" 4001 "];
[arrayNo1 addObject:@" 6001 "];
[arrayNo1 addObject:@" 10001 "];
[pickerView1 selectRow:1 inComponent:0 animated:NO];
    
    mlabel.text= [arrayNo1 objectAtIndex:[pickerView2 selectedRowInComponent:0]];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    if([pickerView isEqual:pickerView2])
    {
        return 1;
    }
    if ([pickerView isEqual:pickerView1]) {
        return 1;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if([pickerView isEqual:pickerView2])
    {
        mlabel.text= [arrayNo objectAtIndex:row];
    }
    if ([pickerView isEqual:pickerView1]) {
        mlabel.text= [arrayNo1 objectAtIndex:row];
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if([pickerView isEqual:pickerView2])
    {
        return 3;
    }
    if ([pickerView isEqual:pickerView1]) {
        return 4;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if([pickerView isEqual:pickerView2])
    {
        return [arrayNo objectAtIndex:row];
    }
    if ([pickerView isEqual:pickerView1]) {
        return [arrayNo1 objectAtIndex:row];
    }
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{

}

- (void)dealloc {
    [super dealloc];
}

@end

ViewController.XIB


2 UIPicker and 1 UILabel
Connect Datasource and delegate of both picker to file owner.
NOTE:-
Sorry for naming convention. You can achieve this multi picker by using one pickerView and divide it in number of columns.
In numberOfComponentsInPickerView return how many columns you want in one pickerView.

Loaded nib but the view outlet was not set



Loaded nib but the view outlet was not set

I was working on my iPhone project . And i copied Components from one nib file to another and when i run that project it gave this error. It happen when you copy nib components to another nib or sometime when you open new nib File.

Solution
  • Open the XIB file causing problems
  • Click on file's owner icon on the left bar (top one, looks like a yellow outlined box)
  • If you don't see the right-hand sidebar, click on the third icon above "view" in your toolbar. This will show the right-hand sidebar
  • In the right-hand sidebar, click on the third tab--the one that looks a bit like a newspaper
  • Under "Custom Class" at the top, make sure Class is the name of the ViewController that should correspond to this view. If not, enter it
  • In the right-hand sidebar, click on the last tab--the one that looks like a circle with an arrow in it
  • You should see "outlets" with "view" under it. Drag the circle next to it over to the "view" icon on the left bar (bottom one, looks like a white square with a thick gray outline
  • Save the xib and re-run



Monday, October 22, 2012

Android Grid View


In java file
GirdView_Act.java


package GirdView.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class GirdView_Act extends Activity implements
AdapterView.OnItemSelectedListener {
TextView selection;
String[] items = { "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "thirteen", "fouteen", "fifteen",
"sixteen", "seventeen", "eighteen", "ninteen", "twenty",
"twenty one", "twenty two", "twenty three", "twenty four",
"twenty five", "twenty six", "twenty seven", "twenty eight",
"twenty nine", "thirty", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven", "thirteen",
"fouteen", "fifteen", "sixteen", "seventeen", "eighteen",
"ninteen", "twenty", "twenty one", "twenty two", "twenty three",
"twenty four", "twenty five", "twenty six", "twenty seven",
"twenty eight", "twenty nine", "thirty", };

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
GridView g = (GridView) findViewById(R.id.grid);
g.setAdapter(new FunnyLookingAdapter(this,
android.R.layout.simple_list_item_1, items));
g.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
selection.setText(items[position]);
}

public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}

private class FunnyLookingAdapter extends ArrayAdapter {
Context ctxt;

@SuppressWarnings("unchecked")
FunnyLookingAdapter(Context ctxt, int resource, String[] items) {
super(ctxt, resource, items);
this.ctxt = ctxt;
}

public View getView(final int position, View convertView,
ViewGroup parent) {
TextView label = (TextView) convertView;
if (convertView == null) {
convertView = new TextView(ctxt);
label = (TextView) convertView;
}
label.setText(items[position]);
return (convertView);
}
}
}

In xml files
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="35px"
android:horizontalSpacing="5px"
android:numColumns="auto_fit"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>



Android List View ( using EfficientAdapter)


In java file


package ListView.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class my_listview extends Activity {
ListView itemList = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview2);
// TODO Auto-generated method stub
itemList = (ListView) findViewById(R.id.itemList);
itemList.setAdapter(new EfficientAdapter(this));
}

String[] Qitems = {"Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", "Q12", "Q13", "Q14", "Q15"};

String[] Aitems = { "Ans1", "Ans2", "Ans3", "Ans4", "Ans5", "Ans6", "Ans7", "Ans8", "Ans9", "Ans10", "Ans11", "Ans12", "Ans13", "Ans14", "Ans15" };

public class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public Context ctxt;

public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.ctxt = context;
}

public int getCount() {
return Qitems.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}


public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;

if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_listview, null);
holder = new ViewHolder();
holder.titletxt = (TextView) convertView.findViewById(R.id.titletxt);
holder.ratingtxt = (TextView) convertView.findViewById(R.id.ratingtxt);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ratingtxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), Aitems[position],Toast.LENGTH_LONG).show();
}
});

holder.titletxt.setText(Qitems[position]);
holder.ratingtxt.setText("Answer");

return convertView;
}

class ViewHolder {
TextView titletxt;
TextView ratingtxt;

}
}
}

In first xml file (.xml)

custom_listview.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/titletxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="fasdsadsad"
android:textStyle="bold">
</TextView>
<TextView
android:id="@+id/ratingtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#ffffff"
android:text="123"
android:layout_below="@+id/titletxt"
android:textSize="10sp">
</TextView>
</RelativeLayout>
</LinearLayout>

In second xml file (.xml)

listview2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/ImageView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="@drawable/icon">
</ImageView>
<ListView
android:layout_below="@+id/ImageView1"
android:id="@+id/itemList"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:background="#20000000">
</ListView>
</RelativeLayout>

Note :- Question and Answers in String Qitem and Aitems are just for example.



Android List View ( Simple ListView using ArrayAdapter)


In java file( .java)


package ListView_Pkge.app;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListView_Acty extends ListActivity {

String[] Qitems = {
"Q1:What are the advantages of Object Oriented Programming Languages (OOPL)?",
"Q2:How do you express an ‘is a’ relationship and a ‘has a’ relationship or explain inheritance and composition? What is the difference between composition and aggregation?",
"Q3:What do you mean by polymorphism, inheritance, encapsulation, and dynamic binding?",
"Q4:What is the difference between an abstract class and an interface and when should you use them?",
"Q5:How would you communicate between applets and servlets?",
"Q6:Why use LDAP when you can do the same with relational database (RDBMS)?",
"Q7:Explain the RMI architecture?",
"Q8:What is a remote object? Why should we extend UnicastRemoteObject?",
"Q9:How will you pass parameters in RMI?",
"Q10:What is the difference between final, finally and finalize() in Java?",
"Q11:Why use LDAP when you can do the same with relational database (RDBMS)?",
"Q12:Explain the RMI architecture?",
"Q13:What is a remote object? Why should we extend UnicastRemoteObject?",
"Q14:How will you pass parameters in RMI?",
"Q15:What is the difference between final, finally and finalize() in Java?"};

String[] Aitems = {  "Ans1","Ans2","Ans3","Ans4","Ans5","Ans6","Ans7","Ans8","Ans9","Ans10","Ans11","Ans12","Ans13","Ans14","Ans15"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.listview2);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Qitems));
}

public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(this, Aitems[position], Toast.LENGTH_LONG).show();
}
}


In XML file ( .xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>


Note :- Question and Answers in String Qitem and Aitems are just for example.
Source Code