Tuesday, June 26, 2012

Android : ListView with fast scroll using alphabets in the right side of the screen


Android ListView with fast scroll using alphabets in the right side of the screen

We have lot of questions like
Alphabetical sorted listview with Alphabetic section using Adapter in Android application.
Listview alphabetical index programmatically
In IPhone we are having Indexed UITableView
Android Listview like iphone contacts list (alphabets in right side of the list)

We are not having similar functionality in android but we are having android:fastScrollEnabled and alphaIndexer.
Without using android:fastScrollEnabled and alphaIndexer. Just with touch recognition, alphabetically sorting can be achieved easily.

If you want to implement the same feature in android and solution for all the above questions, we can try this approach

Code: indexTableCode

Screen Shot:





















Thanks for reading :)
If you have any other quick thoughts/hints that you think people will find useful, feel free to leave a comment.

Saturday, June 9, 2012

IPhone / IPAD : Custom Language Settings for NSString & UIImage

By Changing Language settings in Settings >General >International >Change Language. We can achieve the localization to the Phone and Application.

If we want to handle the localization only for the application but not to the Phone.
Then we can customize application as follows

We know how to use the localization by using NSLocalizedString, 
which returns a localized version of a string. by using following command
NSString *NSLocalizedString(NSString *key, NSString *comment) 

We have localization for Image as follows

NSString* imageName = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"];
self.image = [UIImage imageWithContentsOfFile:imageName];

But if you want to localize Image using your custom settings then we can try the following snippet code.

For Eg: If you’re App supports English and German.
But, If your Phone default language is using French and 
Your app should use as per your language settings.
 
How to achieve following use case?

Provide Language Settings in your app using settings.bundle 

By Using the above changes in your settings.bundle file, 
we can see as follows language settings in your app.
Now, How to get the selected language?
There is code snippet. one way, where we can achieve localization 
/*
 * languageSelectedStringForKey used to get the localized value using key.
 */
NSUserDefaults* defaults;

- (NSString *)getLocalizationPath
{
    NSString *path;
    defaults = [NSUserDefaults standardUserDefaults];
    [defaults synchronize];
  
    NSString *defPhoneLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
  
    NSString *langCode = [defaults stringForKey:@"testLanguage"];
  
    if([langCode isEqualToString:@"0"])
    {
        if([defPhoneLanguage isEqualToString:@"de"] || [defPhoneLanguage isEqualToString:@"en"])
        {
            path = [[NSBundle mainBundle] pathForResource:defPhoneLanguage ofType:@"lproj"]; 
        }
        else
        {
            path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]; 
        }
    }
    else if([langCode isEqualToString:@"German"] || [langCode isEqualToString:@"de"])
    {
        path = [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"]; 
    }
    else
    {
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];      
    }
    return path;
}

-(NSString*) getLocaleStringForKey:(NSString*) key
{
    NSString *path = [self getLocalizationPath];
  
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
  
    return str;
}

-(NSString*) getLocaleImageForKey:(NSString*) key
{
    NSString *path = [self getLocalizationPath];
  
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString *str = [[languageBundle resourcePath] stringByAppendingPathComponent:key];
  
    return str;
}

Custom Locale Image:
[UIImage imageWithContentsOfFile:[self getLocaleImageForKey:@"sample.png"]]


Have a nice day... Enjoy :)

Friday, June 8, 2012

Android Robotium : Capture Screen or ScreenShot

We are having one question in mind, when we are using Robotium for testing.

Can I take screenshots from inside of Robotium?

Ans: No. we are not having direct API's.

You can find the details information from the following link http://code.google.com/p/robotium/wiki/QuestionsAndAnswers

But we are having alternative approach, where we can achieve taking snapshot of the screen.

Android Robotium ScreenShot Code


By using the above code snippet, we can achieve taking snapshot in Robotium, when your test case fail or success.

Enjoy :)