Wednesday, June 03, 2015

Ignore the #LittleMan - Michael Dell - #Entspresso

Tuesday, February 17, 2015

iOS Development - URL Encoding in SWIFT

When consuming web services from endpoints, many a times, the input data for the service is sent using GET methods. In other words, the data is part of the request URL.

For the URL to be valid, the data should be encoded, so that the combined end point URL, and the data part does not make the overall URL string invalid.

In the new apple programming language, SWIFT, this can be achieved as follows:

let endPointURL:String  = "https://mutationevent-qr-code-generator.p.mashape.com/generate.php";
let endUrl = endPointURL+"?content="+txtData.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())!;

The method : stringByAddingPerfentEncodingWithAllowedCharacters  takes in a set of allowed character set, and converts the owner string to an encoded form. In this case, txtData variable holds the text entered by the user in a text box. Without using the above encoding, if the user entered a text with spaces, the "endURL" will be an invalid URL, since spaces are NOT allowed in a URL.

However, with the above method, all spaces in txtData will be replaced with "%20", and so will any other special characters be encoded appropriately. This will result in an overall valid URL, which can then be used to dispatch a web request using NSURLSession()

Saturday, July 26, 2014

Android Development : Changing theme of Current Activity during runtime (eg: during Button Click event)

Introduction

There may be situations where the entire look of the screen (current activity) needs to be changed as a result of some event. The event may be as simple as a button click, or may be a sensor feedback, or even as a result of streaming data from internet. 

Many articles explain how to set a theme in android manifest file, during design time. However, doing this will result in the application/activity itself starting with the theme and not as a response to an "event".

The Finished Demo





Repository Link

https://github.com/gopalnair/Android-Style-Demo

Key Points On Code

In method MainActivity.onCreate , make sure the setTheme() is called BEFORE call to parent. In this example, when the activity starts for the first time from launcher, there are no extras in the launcher intent. Hence, application start with default theme applied.

However, when user clicks on one of the "Style" buttons, we call the SAME activity (MainActivity), and package the target theme information in intent extras like so:


Notice that after calling startActivity() to start the Main Activity again, we are calling "finish()" method, so that the current instance of Main Activity will be killed, and we will be left with the just "triggered" main activity. If we do not call the "finish", by default, multiple main activities will stack one above other (Unless android manifest file is updated to force the activity to have a single instance).