Friday, November 27, 2009

Nonblocking mechanism for Android GUI

Do you realize that fetching data from the web, for example a GET request, may actually block the GUI and then creates an error? I experienced that and would want to share my thoughts on resolving it. I have not implemented a sound solution but these are the things I have in my mind. There are two components to implement in the code:
1. Thread
2. Handler

Let's see if this works :)

Wednesday, November 25, 2009

Developers are born brave

This is something motivational I want to share with the readers :)
Credits to the creative artist.

Saturday, November 21, 2009

Removing status and title bars in an Android app (suggested method)

I discovered that the status and title bars can be removed in an Android app by inserting one simple line in the manifest xml file. Feel free to try this out and let's discuss if this does not work for you.

<activity android:name=".YOUR_ACTIVITY_NAME"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Thursday, November 19, 2009

Distinct intersecting values of two arrays

I have written two different methods that gives the same output. The output is a list of distinct intersecting values of two arrays. I am amazed with the different amount of time taken to solve this problem. It would be something interesting to investigate. Here are the suggested solutions:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class ArraysIntersection {

private void findIntersectionMethod1(int[] a, int[] b){
long start = System.nanoTime();
int indexA = 0;
int indexB = 0;
ArrayList unique = new ArrayList();
while (indexA < a.length && indexB < b.length) {
if (a[indexA] == b[indexB]) {
if (!unique.contains(a[indexA])) {
unique.add(a[indexA]);
}
indexA++;
indexB++;
} else if (a[indexA] < b[indexB]) {
indexA++;
} else {
indexB++;
}
}
long elapsed = (System.nanoTime() - start);
System.out.println("Elapsed time (findIntersectionMethod1): " + elapsed + "ns");
System.out.println("Intersection of elements: " + unique);
}

private void findIntersectionMethod2(int[] arr1, int[] arr2){
long start = System.nanoTime();
ArrayList unique = new ArrayList();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
if (!unique.contains(arr1[i])) {
unique.add(arr1[i]);
}
}
}
}
long elapsed = (System.nanoTime() - start);
System.out.println("Elapsed time (findIntersectionMethod2): " + elapsed + "ns");
System.out.println("Intersection of elements: " + unique);
}

private void findIntersectionMethod3(int[] a, int[] b){
long start = System.nanoTime();
Set unique = new HashSet();
int indexA = 0;
int indexB = 0;
while (indexA < a.length && indexB < b.length) {
if (a[indexA] == b[indexB]) {
unique.add(a[indexA]);
indexA++;
indexB++;
} else if (a[indexA] < b[indexB]) {
indexA++;
} else {
indexB++;
}
}

long elapsed = (System.nanoTime() - start);
System.out.println("Elapsed time (findIntersectionMethod3): " + elapsed + "ns");
System.out.println("Intersection of elements: " + unique);
}

public static void main(String[] args) {

int a[]={1, 1, 2, 4, 4, 7, 8, 8, 9, 14, 14, 20, 20, 20};
int b[]={1, 1, 1, 2, 2, 8, 8, 20};

ArraysIntersection arrIntersect = new ArraysIntersection();
arrIntersect.findIntersectionMethod1(a, b);
arrIntersect.findIntersectionMethod2(a, b);
arrIntersect.findIntersectionMethod3(a, b);
}
}

The output of these methods are as below (I'm telling you, you'll be amazed looking at the time taken for each method):
Elapsed time (findIntersectionMethod1): 455000ns
Intersection of elements: [1, 2, 8, 20]
Elapsed time (findIntersectionMethod2): 15000ns
Intersection of elements: [1, 2, 8, 20]
Elapsed time (findIntersectionMethod3): 65000ns
Intersection of elements: [2, 8, 1, 20]

I'm sure there are much better solutions to solve this interesting problem that I have not thought of.

Wednesday, November 18, 2009

Quizzie with improved user interface

This is Quizzie v1.1 with much better user interface. I hope you enjoy watching this video. Quizzie v1.0 is available here.

Remote log management and monitoring? There's an Android app for that

I'm thinking of writing an Android app using Splunk's API. Not sure if it's possible but I think it can be done. Here is my concept about the application.

We can actually see the outputs of the activities in Android by using adb logcat. What if I am able to pipe out the stream into a file and then push it to my local Splunk web server. hhhmmm interesting ...

"Remote log management and monitoring? There's an Android app for that"

:D

Saturday, November 14, 2009

Jython in Android

I think it's possible. But why are there so limited references to use Jython in Android?

Friday, November 13, 2009

Removing status and title bars in an Android app

Here are a few options to display the status and title bars in your Android app. The first image shown below is exactly the output you are getting if you do not do any configurations.
To get this output, add this line in the onCreate method:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
To get this output, add this line in the onCreate method:
requestWindowFeature(Window.FEATURE_NO_TITLE);
To get this output, add these two lines in the onCreate method:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

Wednesday, November 11, 2009

The very music that reminds me to be innovative

I know this song is quite old but I have been listening to it ever since it was introduced to the public. This is "Here Comes Another Bubble" and it is adapted from "We Didn't Start The Fire". Being a software developer, I personally prefer the technical version of the song about coming up with innovative and brilliant ideas that change the world; not so much about the wars and scandals that took place throughout the course of history.


Monday, November 9, 2009

Java Codes in Python (Part 4)

Here is a suggested solution to get unique values from a list without using a map or set data structure

Extracting unique element(s) from a sorted list

PROBLEM: you have a list of sorted elements. All elements are sorted (it could be in ascending order or descending order). You are printing out the unique elements in the sorted list.
COMPLEXITY: O(n)

public class UniqueNumbers {
public static void main(String[] args) {
int a[]={1, 1, 1, 2, 4, 4, 7, 8, 9, 14, 14, 20, 20, 20};
//int a[]={1};
//int a[]={2, 2, 4, 5};
//int a[]={1, 1, 1, 2, 2, 8};
//int a[]={1, 1, 1};
int uniq = a[a.length-1];
boolean numberExists = false;
for (int i = 0; i < a.length; i++) {
if (a[i] == uniq) {
if (!numberExists) {
System.out.println("Position: " + i + " Unique Element => " + a[i]);
numberExists = true;
}
}
else {
System.out.println("Position: " + i + " Unique Element => " + a[i]);
uniq = a[i];
numberExists = true;
}
}
}
}
The output is shown below:

{1, 1, 1, 2, 4, 4, 7, 8, 9, 14, 14, 20, 20, 20}
Position: 0 Unique Element => 1
Position: 3 Unique Element => 2
Position: 4 Unique Element => 4
Position: 6 Unique Element => 7
Position: 7 Unique Element => 8
Position: 8 Unique Element => 9
Position: 9 Unique Element => 14
Position: 11 Unique Element => 20

{1}
Position: 0 Unique Element => 1

{2, 2, 4, 5}
Position: 0 Unique Element => 2
Position: 2 Unique Element => 4
Position: 3 Unique Element => 5

{1, 1, 1, 2, 2, 8}
Position: 0 Unique Element => 1
Position: 3 Unique Element => 2
Position: 5 Unique Element => 8

{1, 1, 1}
Position: 0 Unique Element => 1
Update: my solution is not that elegant after all. I had the opportunity to discuss the algorithm with Mark and he has a much better way to solve this problem. Thank you, Mark (you know who you are :D).

public static void main(String[] args) {
int a[]={1, 1, 1, 2, 4, 4, 7, 8, 9, 14, 14, 20, 20, 20};
//int a[]={1};
//int a[]={2, 2, 4, 5};
//int a[]={1, 1, 1, 2, 2, 8};
//int a[]={1, 1, 1};
int uniq = a[0];
System.out.println("Position: " + 0 + " Unique Element => " + uniq);
for (int i = 1; i < a.length; i++) {
if (a[i] != uniq) {
System.out.println("Position: " + i + " Unique Element => " + a[i]);
uniq = a[i];
}
}
}

Saturday, November 7, 2009

Setting defined colors for your Android UI components

I don't think the Android SDK manual is helpful enough when it comes to setting the colors that you defined in the XML file (in my example, res -> values -> color.xml). There is actually a workaround instead of following rigidly the guidelines to use the setTextColor(int color) method.

I believe you must have read the guidelines how to set the color of TextView from this URL and this URL and do not really feel like constructing another XML file as mentioned in the manual.

I also believe that you must have used this approach to set the color from the Java code,
btnInstance.setTextColor(R.color.YOUR_DEFINED_COLOR);
simply because the setTextColor asks for an Integer as the parameter but you don't see the changes taking place.

SOLUTION: this is THE workaround to set the color of your UI components from the codes.
btnInstance.setTextColor(getResources().getColor(R.color.YOUR_DEFINED_COLOR));

The 12 balls puzzle

A solution to solve the 12 ball puzzle.
Fact: you know that 11 balls are equally weighted and 1 of them is not
Constraint: you can only use the weighing machine at most 3 times to determine the heaviest ball

import java.util.Random;

public class TheHeaviestBall {

private int count = 0;
private int indexHeaviestBall = 0;
private Random myRand = new Random();
private int[] balls = {1,1,1,1,1,1,1,1,1,1,1,1};
private int[] arrangeBalls(){
balls[myRand.nextInt(12)] = 2;
for (int aBall : balls) {
System.out.print(aBall + " ");
}
return balls;
}

public void weighingCount(){
count++;
if (count > 3) {
System.out.println("Your algorithm fails");
}
}

public int theBALL(){
int myBalls[] = arrangeBalls();
weighingCount();
if ((myBalls[0]+myBalls[1]+myBalls[2]+myBalls[3]) > (myBalls[4]+myBalls[5]+myBalls[6]+myBalls[7])) {
weighingCount();
if ((myBalls[0]+myBalls[1]) > (myBalls[2]+myBalls[3])) {
weighingCount();
if (myBalls[0] > myBalls[1]) {
indexHeaviestBall = 0;
return myBalls[0];
} else {
indexHeaviestBall = 1;
return myBalls[1];
}
} else {
weighingCount();
if (myBalls[2] > myBalls[3]) {
indexHeaviestBall = 2;
return myBalls[2];
} else {
indexHeaviestBall = 3;
return myBalls[3];
}
}
}
else if ((myBalls[4]+myBalls[5]+myBalls[6]+myBalls[7]) > (myBalls[0]+myBalls[1]+myBalls[2]+myBalls[3])) {
weighingCount();
if ((myBalls[4]+myBalls[5]) > (myBalls[6]+myBalls[7])) {
weighingCount();
if (myBalls[4] > myBalls[5]) {
indexHeaviestBall = 4;
return myBalls[4];
} else {
indexHeaviestBall = 5;
return myBalls[5];
}
} else {
weighingCount();
if (myBalls[6] > myBalls[7]) {
indexHeaviestBall = 6;
return myBalls[6];
} else {
indexHeaviestBall = 7;
return myBalls[7];
}
}
}
else {
weighingCount();
if ((myBalls[8]+myBalls[9]) > (myBalls[10]+myBalls[11])) {
weighingCount();
if (myBalls[8] > myBalls[9]) {
indexHeaviestBall = 8;
return myBalls[8];
} else {
indexHeaviestBall = 9;
return myBalls[9];
}
} else {
weighingCount();
if (myBalls[10] > myBalls[11]) {
indexHeaviestBall = 10;
return myBalls[10];
} else {
indexHeaviestBall = 11;
return myBalls[11];
}
}
}
}

public static void getMyHeaviestBall(){
TheHeaviestBall hb = new TheHeaviestBall();
//Algorithm validation
if (hb.count > 3) {
System.out.println("You can do better");
} else {
System.out.println("\nThe heaviest ball [ " + hb.theBALL() + " ] is the " + (hb.indexHeaviestBall + 1) + "th ball from the left");
}
}

public static void main(String[] args) {
getMyHeaviestBall();
}
}
Update: Click here to view the solution to solve the 8 balls puzzle

Friday, November 6, 2009

The 8 balls puzzle

Here is an interesting puzzle about selecting the heaviest ball among 7 other equally weighted balls. For example a collection of the 8 balls would look like this [2,1,1,1,1,1,1,1].

The goal is to choose the heaviest ball by performing only two weighings.
Fact: You know that all 7 other balls are equally weighted but you could not figure out the heaviest ball.
Solution: In order to do so, you are using the weighing machine but as a challenge you can only use the weighing machine at most twice

import java.util.Random;

public class TheHeaviestBall {

private int count = 0;
private int indexHeaviestBall = 0;
private Random myRand = new Random();
private int[] balls = {1,1,1,1,1,1,1,1};
private int[] arrangeBalls(){
balls[myRand.nextInt(8)] = 2;
for (int aBall : balls) {
System.out.print(aBall + " ");
}
return balls;
}

public void weighingCount(){
count++;
if (count > 2) {
System.out.println("Your algorithm fails");
}
}

public int theBALL(){
int myBalls[] = arrangeBalls();
weighingCount();
if ((myBalls[0]+myBalls[1]+myBalls[2]) > (myBalls[3]+myBalls[4]+myBalls[5])) {
weighingCount();
if (myBalls[1] == myBalls[2]) {
indexHeaviestBall = 0;
return myBalls[0];
} else {
if (myBalls[1] >= myBalls[2]) {
indexHeaviestBall = 1;
return myBalls[1];
} else {
indexHeaviestBall = 2;
return myBalls[2];
}
}
}
else if ((myBalls[3]+myBalls[4]+myBalls[5]) > (myBalls[0]+myBalls[1]+myBalls[2])) {
weighingCount();
if (myBalls[4] == myBalls[5]) {
return myBalls[3];
} else {
if (myBalls[4] >= myBalls[5]) {
indexHeaviestBall = 4;
return myBalls[4];
}
else {
indexHeaviestBall = 5;
return myBalls[5];
}
}
}
else {
weighingCount();
if (myBalls[6] >= myBalls[7]) {
indexHeaviestBall = 6;
return myBalls[6];
}
else {
indexHeaviestBall = 7;
return myBalls[7];
}
}
}

public static void getMyHeaviestBall(){
TheHeaviestBall hb = new TheHeaviestBall();
//Algorithm validation
if (hb.count > 2) {
System.out.println("You can do better");
} else {
System.out.println("\nThe heaviest ball [ " + hb.theBALL() + " ] is the " + (hb.indexHeaviestBall + 1) + "th ball from the left");
}
}

public static void main(String[] args) {
getMyHeaviestBall();
}
}
Update: Click here to view the solution to solve the 12 balls puzzle

Quizzie - my first Android application

Behold ... my very first fully functional Android application. The development process is very short. I always have this philosophy that if you know what you want to do and achieve, you will know what to do and how to achieve it. Therefore the design process is shortened.

This game is all about Mathematics. Seemingly simple mathematical expressions but good enough to induce carelessness to choose the wrong answer.

Wednesday, November 4, 2009

Splunk in Movie Characters Timeline

These are the graphs that could be possibly (fictitiously) generated using Splunk to search for the characters in movies. Take a look at this:Click here to view the original source of the picture from xkcd.

Saying that something is wrong to someone

This is a hilarious way to express to someone if something is wrong. By the way if Cartman thinks that it is wrong, it must be really really wrong. :))

Monday, November 2, 2009

Sound Management in Android Application

This is a suggestion from myself about adjusting the volume of the sound effects or background music in your Android Activity(ies). The sample code is as below:onKeyDown method is called when a button is pressed and onKeyUp is called when the button is released. In this example specifically, we are raising and lowering the volumes when the volume keys are pressed and retain the values when the buttons are released.

Adding Vibration to your Android Application

Below are the simple steps to enable the vibration feature for your Android application. There are two things you will need to take care of - the AndroidManifest.xml file and the Vibrate class.

[1] Add this line in manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
. . .
. . .
. . .
<uses-permission android:name="android.permission.VIBRATE">permission>
</manifest>

[2] Add these two lines in your class definition.