Friday, January 9, 2015

Android: Taking control of the orientation...

It can be pretty irritating when you have a mixture of screens that should be only be appearing in a Potrait or a Landscape orientation only. Therefore here's a quick demo to show you how you can take control of the orientation of your Android App.

Here's the source code of the Main Activity of the Android Application - MainActivity.java
package com.nekyoutoTech.controloverorientation;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity {
 
 //Dummy Array of Buttons
 private Button[] btns = new Button[5];
 
 //Stores a reference of the Application Context
 private static Context context;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  //Loop through the buttons and assign the Click Listener to them
  for(int i = 0; i < 5; i ++){
   int resID = getResources().getIdentifier("btn" + i, "id", getPackageName());
   btns[i] = (Button) findViewById(resID);
   btns[i].setOnClickListener(btnClickListener);
  }
  
  context = this.getApplicationContext();
 }

 /**
  * Upon clicking on any of the buttons
  */
 OnClickListener btnClickListener = new OnClickListener(){
  @Override
  public void onClick(View v) {
   //Get the String of the button first
   Button btn = (Button) v;
   String text = (String) btn.getText();
   
   //Depending on the button that was click, let's select the orientation
   //that we would want to set for the app.
   int orientation;
   if (context.getResources().getString(R.string.btn0Str).equals(text)) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
   } else if (context.getResources().getString(R.string.btn1Str).equals(text)) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
   } else if (context.getResources().getString(R.string.btn2Str).equals(text)) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
   } else if (context.getResources().getString(R.string.btn3Str).equals(text)) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
   } else if (context.getResources().getString(R.string.btn4Str).equals(text)) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
   } else {
    orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
   }
   
   //Let's set the orientation of the app now.
         setRequestedOrientation(orientation);
  }
 };
}
* Click here for the source files of the project.

Tuesday, November 25, 2014

Javascript: Cloning an object...

In Case, you need to do a complete clone of an object in Javascript and you want the original to remain unaffected, then you probably can give this method a go. :)


* Click here to access the demo that I have created on 'JSFiddle'.
^ Click here to test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code
  editor.

Sunday, November 16, 2014

Towards the end of 2014...

Less than 8 more weeks to the end of 2014. And it's time to start the countdown for the last IT Show for the year 2014...

Image taken from 'SITEX 2014' website.

The venue for the upcoming event is pretty far, but it's
worth travelling all the way down. As compared to the
venue at 'Suntec Convention & Exhibition Centre', I prefer
the huge spacious space at Singapore Expo. Most importantly,
I can roam around the place freely and quickly, whhich is a
very big (+). :)
Venue:
  • Date: 27 - 30 November 2014
  • Opening Hours: 11am to 9pm
  • Venue: Singapore Expo Convention & Exhibition Centre
                 Halls 5 & 6

* Click here to find out more about 'SITEX 2014'.
^ Click here for the Facebook page of 'SITEX 2014' site.
~ Click here for the unofficial 'SITEX 2014' site.
  (You can gain access to all the pricelist of all the Exhibitor @ 'SITEX 2014'.
  Although it isn't available yet but it will be populated with lots of price lists and
  brochure as the 'show' approaches.)

Sunday, November 9, 2014

Javascript: Some useful sites that helps.

Frankly speaking, I don't have a fantastic memory and in case, you don't have a fantastic memory, you probably can use the following sites to search for useful examples and references. Even though these sites doesn't provide an example for all everything, but it's a great place to start searching.

W3Schools
This has been around for quite some time.
Not only it covers 'Javascript', it also covers coding environment
like 'PHP', 'SQL', 'ASP.NET', etc... Though most of the time it
covers only the basic stuff, but it's a great place to get you
started.

JavaScripture
This covers JavaScript only. But with the number of examples that
was provided on the site itself to demonstrate the usage of the
different types of JavaScript syntax, makes this a pretty good
place to start searching.

Sunday, October 26, 2014

Apache Ant: Pushing your Ant script to a whole new level...

If you have been using Apache Ant quite a bit, you will soon realize that it can be pretty annoying at times. You couldn't open up and read a file, you couldn't create Arrays, etc... Therefore there's actually a way to work around it. You can actually use Javascript + Java inside the Ant script environment. And on top of that you can actually manipulate simple basic Ant variables in the Javascript + Java environment.

 <macrodef name="getIndexHtmlContents">
  <sequential>      
   <script language="javascript">
    <![CDATA[
     //Import the necessary Java classes
     importClass(java.io.File);
     importClass(java.io.FileReader);
     importClass(java.io.BufferedReader);

     //We will be reading the value from the Ant Project Property 'filenameProp'
     var filenameProp = GWR.getProperty("filenameProp");

     //Then we will be using BufferedReader and FileReader to read the contents of the file
     var reader = new BufferedReader(new FileReader(new File(filenameProp)));

     //Next we will loop through all the contents of the file and store it in a temporary variable
     var sCurrentLine;
     var strEverything = "";
     while ((sCurrentLine = reader.readLine()) != null) {
      strEverything += sCurrentLine;
     }

     //We will printout the contents of the file/temporary variable on the ant console output
     println(strEverything);

     //Writing the value of the temporary variable and saing it in the Ant Project Property 'fileContents'
     GWR.setProperty("fileContents", strEverything);
    ]]>
   </script>
  </sequential>
 </macrodef>
Though you have specified that you will be writing some script in 'Javascript' syntax, it actually allows Java codes to be written inside that <script> block too.

* Click here to find out more about 'Apache Ant'.

Friday, October 17, 2014

Linux: Mass find and replace a folder of files

In the Linux environment, it is pretty easy to loop through a particular type of files to find and replace a particular string using shell script. I hope this helps.

The following will give you an idea how to get it done.
You need to replace all the {value} with the corresponding values.
#!/bin/bash

varReplace={string_to_replace}
find ./{the_folder}/ -name \*.{file_extension} -type f -print0 | while read -d $'\0' file; do
    echo "Processing $file"
    sed -i 's/{string_to_look_for}/'$varReplace'/g' $file
done
The following example will go through all the files with extension (.js)
in the folder 'output', find the string '{my_version}' and replace it with
the string 'v1.0.122'.
#!/bin/bash

varReplace=v1.0.122
find ./output/ -name \*.js -type f -print0 | while read -d $'\0' file; do
    echo "Processing $file"
    sed -i 's/{my_version}/'$varReplace'/g' $file
done

Friday, October 10, 2014

Simple Javascript Timer Implementation

Well basically this is a post meant for myself. I keep forgetting how to get this done. Basically this is a simple implementation of a Timer in Javascript.


* Click here to access the demo that I have created on 'JSFiddle'.
^ Click here to test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code
  editor.