Navigation


RSS : Articles / Comments


Android Multi format TextView

2:58 AM, Posted by ruchira, No Comment

Hey Androids !!!

This is going to be my very first post on Android development.
You may know that I've written many posts on Dot Net, Java & iPhone. From today onwards, I will add Android to that list :)

So, if you're in a situation that you need to have a single label(TextView) with multiple formats, then this will be the post for you. It becomes very simple when you know a bit of html.

Here is a list of tags supported by Android TextView before you get started.
Have a look at this before you start.

Suppose you have a TextView named multiFormatView

TextView multiFormatView;

The first step is to build your HTML string.

String htmlString = "<b>This is bold</b>"+"And this is not.";

Now there is only one line of code. Just set the text to the TextView using.


multiFormatView.setText(Html.fromHtml(htmlString));

That's it.

So, it will print 

This is bold.And this is not.

You can juggle around with each of the tags supported to see how you can get your job done.
Happy coding my friends

:)



XCode importing a file to whole project

1:59 AM, Posted by ruchira, No Comment

Hi,

Sometimes you may encounter situations where you have a file which is used frequently throughout the XCode project. This may be a configurations file which is used in 80% of your code files. Importing this in each file is a certain possibility(and the very normal way with no issues), but if you are lazy and just want to add it in one place, this is the post for you.

If you analyze the project, there is a file named <project-name>.pch. There is a block of code which has the following.

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

NOTE: As you can see, UIKit is already added and so is the Foundation framework. That is why you can use classes such as "UIView" or "NSArray" without any additional imports in your code file. That's just an extra bit of info for the curious !!!.

So, likewise, we can add our commonly used file into that block as well in the following manner.


#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "MyConfigFileName.h"
#endif    


Once you add this, you can use "MyConfigFile" within your project without additionally importing it in each file.

NOTE: Remember, this is not the place to import each and every file for convenience. Only do it if the file is needed to be imported across a wide number of places in the project. Otherwise it'll just bring more trouble.

Happy coding :)

iOS Convert Seconds into hours,minutes and seconds

1:27 AM, Posted by ruchira, No Comment

Hi all,

In building iPhone applications you may have come across situations that you count some time in seconds, but wish to let the user view it in a more readable format such as 1 hour 38 minutes and 44 seconds. Here is a piece of code that I wrote if you need to get this done in your code.


- (NSString *)timeFormatted:(int)totalSeconds
{
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
   
    NSString *timeString =@"";
    NSString *formatString=@"";
    if(hours > 0){
        formatString=hours==1?@"%d hour":@"%d hours";
        timeString = [timeString stringByAppendingString:[NSString stringWithFormat:formatString,hours]];
    }
    if(minutes > 0 || hours > 0 ){
        formatString=minutes==1?@" %d minute":@" %d minutes";
        timeString = [timeString stringByAppendingString:[NSString stringWithFormat:formatString,minutes]];
    }
    if(seconds > 0 || hours > 0 || minutes > 0){
        formatString=seconds==1?@" %d second":@" %d seconds";
        timeString  = [timeString stringByAppendingString:[NSString stringWithFormat:formatString,seconds]];
    }
    return timeString;
      
}

This code checks to see if the number of hours, minutes or seconds is equal to one and displays either hour/hours, minute/minutes or second/seconds accordingly.

Have fun coding, guys & gals !!!

Mac Audio Convert Tool

4:05 AM, Posted by ruchira, No Comment

Hi guys.

It's really painful when you need to have a soundtrack in one format when you have another format. I found this really useful tool named SoX for Mac OS which you can download free from here.

iPhone manually set Cookies

3:57 AM, Posted by ruchira, No Comment

Hi guys !!! You may have encountered situations where you wish to set some cookies through your app code.
There is a very basic procedure to do it.

1. Create a dictionary with all the cookie properties
2. Create a Cookie passing the property dictionary which you created
3. Set the Cookie using the NSHTTPCookieStorage class

First create a properties dictionary.

NSDictionary *propertiesUsername = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @".yourdomain.com", NSHTTPCookieDomain,
                                        @"cookie_name", NSHTTPCookieName,
                                        @"cookie_value", NSHTTPCookieValue,
                                        @"/", NSHTTPCookiePath,
                                        [[NSDate date] dateByAddingTimeInterval:60*60*24],NSHTTPCookieExpires,
                                        nil];

Most of these are self explanatory. As you can see you have to set the domain, and path in addition to expiry date. I have set it to one day.

The next step is to create the Cookie itself. It can be done using the following method.

NSHTTPCookie *cookieUsername=[[NSHTTPCookie alloc] initWithProperties:propertiesPassword];

Then, get a handle to the NSHTTPCookieStorage and simultaneously set the cookie.

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieUsername];

If you want to check whether the Cookie was actually created, you can simply print them using the following code.

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
   
    for (NSHTTPCookie *cookie in cookies) {
        NSLog(@"----Name: %@ : Value: %@ :Expiration %@ :", cookie.name, cookie.value, cookie.expiresDate);
    }


That's it. Now you can use this method whenever you need to manually set the cookies !!!!

JavaScript library for Date formatting

10:54 PM, Posted by ruchira, No Comment

Hi all,

You must have faced situations where you need to convert a date string to a different object.

The usual procedure is to first parse the string into a Date object of that language, specifying your formatting string.
The next step is to convert the Date object back into a string specifying the new formatter.

In order to do this in JavaScript, I came across two libraries which I thought to share with you.

Moment.js which is freely distributable under the MIT license. You can download it from here.
There are two versions. You can download the minified source if you need to save space.

The following example shows how to use it to convert from one format to another.

First include the javascript in your files.

var oldFormatTime = moment('2012-10-01','YYYY-MM-dd');
var newFormatTime = oldFormatTime.parse('d-MM-YYYY');

So, the final output would be 1-10-2012

In addition to this, I also found the following library.

Datejs is an open source nice library hosted at Google code for you to use.
You can download it from here.

The following example illustrates how to do this.

Eg: Suppose you have the date as '2012-12-12' and you need to convert it to 'December 12, 2012'.

var dt = Date.parseExact('2012-12-12','yyyy-MM-dd');
var newDateString = dt.toString('MMMM d, yyyy');

View iCloud Files via Browser

3:53 AM, Posted by ruchira, No Comment

Here is a neat little way to browse all the files on your iCloud account.
Simply Browse iCloud Files using this link.
Cheers !!!


 
[source language="xml"] [/source]