Sunday, July 29, 2012

How to Change Main/Start Screen on Android?

Hello! As you known, there is nothing to say about developing  and progressive Android all around the world. In this situation, we can find any idea to make application. Actually, this is not so easy :) Namely what I am trying to say is that Android provides us to create applications and be a developer in mobile world.

I am sure that most of us have played a game via our mobile phones. Things I said above were for any application actually. Because, games, software about educations, politics, health education, voluntariness, etc applications  allow Android skills.

If you want to create an application to be used by everyone (this is very assertive :) ), you have to be different than other apps. This difference should be about design, software or your idea. 

In this article, I'll show you some codes about tiny difference, "starting page" on your Android application.

I want to tell you about my mind. Users download your app and install it on the phone. The next step will be starting your application. This step is so important. Because, people who use the app decide how good the app is. For that reason, how to start the app should be very important for us. In this context, I can start to code about this.

I've got two classes: Main.java and startingPage.java

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startingPagexml);
        
        Thread MyScreen = new Thread() {
         public void run() {
          try {
           sleep(5000);
     
           startActivity(new Intent(getApplicationContext(),startingPage.class));
          } catch (InterruptedException e) {
           e.printStackTrace();
          }
          finally {
           finish();
          }
         }
        };
        MyScreen.start();
    }


The code given above is the first page will be opened on the app. Thread "MyScreen" helps to go another page. When the app opened, wait 5 second and go to the startingPage.class. startingPage.class includes startingPagexml.xml Android Xml file. This file calls the page after 5 seconds.


public class acilis2 extends Activity {

 public void onCreate(Bundle savedInstanceState) {
  Toast.makeText(this, "Welcome to My Application", Toast.LENGTH_LONG).show();
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }


Toast.makeText(this, "Welcome to My Application", Toast.LENGTH_LONG).show();


This code introduces how to give user message after 5 second. If you want to see and learn more information about Toast notification, you can visit android developer official guide in here.


See you next article!

Thursday, July 19, 2012

Multithreading in Fuzuli Programming Language

Since our last commit, Fuzuli supports multithreading. This is the latest milestone that Fuzuli reached in revision tree of 0.1.x.

Fuzuli's multithreading capability stands on the well known boost threading library which will be a built-in package in next C++ standard.

Creating and running a thread in Fuzuli is easy. Define a function and create a thread for this function then call the thread_join method. The join method will wait until the function finishes its job. More than one threads can run at the same time in a time-sharing manner. Multithreading functions are stored thread.nfl, which is now a standard in Fuzuli API.

Lets give an example:


(require "/usr/lib/fuzuli/nfl/thread.nfl")

(function f (params)
 (block
  (print "F started\n")
  (foreach i in (: 1 10)
   (print "f\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(function g (params)
 (block
  (print "G started\n")
  (foreach i in (: 1 10)
   (print "g\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(function h (params)
 (block
  (print "H started\n")
  (foreach i in (: 1 10)
   (print "h\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(let t0 (thread "f"))
(let t1 (thread "h"))
(let t2 (thread "g"))

(thread_join t0)


In the example given above, we have three function f(), g() and h(). Those functions print messages "F started", "G started" and "H started" at the top of their bodies. A foreach loop then count from 1 to 10 and wait 100 milliseconds after each step. The output is shown below:

F started
H started
G started
g
h
f
g
h
f
g
h
f
g
h
f
g
h
f
f
g
h
f
h
g
g
f
h
g
h
f
f
g
h

Functions in this example run simultaneously. thread_join was called on t0, so whenever the function f() finishes its job, program ends. thread_yield is called for giving a chance to run to another threads. thread_sleep waits for given milliseconds if needed.That's all.


Sunday, June 17, 2012

Getting Contents of a DIV With PHP's DOM

Hi! Almost all of us use XML in our web sites. We can get contents with parsing XML files. But sometimes, we need another ways. The reason of this is that we want to get only one dom's content. For example an img element's content or it's value of id


Well, I will introduce how to get contents of element in this article. That's why I'll show you the sample given belown.


For my example, I got two files here:
  1. GetContent.php
  2. test.html
GetContent.php
<?php
$content="test.html";
$source=new DOMdocument();
$source->loadHTMLFile($content);
$path=new DOMXpath($source);
$dom=$path->query("*/div[@id='test']");
if (!$dom==0) {
   foreach ($dom as $dom) {
      print "<br>The Type of the element is: ". $dom->nodeName. "<br><b><pre><code>";
      $getContent = $dom->childNodes;
      foreach ($getContent as $attr) {
         print $attr->nodeValue. "</code></pre></b>";
      }
   }
}
?>
So, If you analyze the sample given above, you can see the point easily. The point is the content of div element which is id=test. The reason of existing test.html page is to get div's content.

test.html
<div id="test">This is my content</div>

What we have just seen up there, should be like on this demo page.

The Type of the element is: div
This is my content

The result is the text above. We'll see you guys next article!

Saturday, June 16, 2012

List Operations in Fuzuli Programming Language

Fuzuli, our new programming language and interpreter has several internal functions for list operations. Arrays are prominent objects of programming languages. Although many other programming languages use brackets for setting and getting values of array, this operator is not defined in Fuzuli. Lists are similar to Lisp's lists but they are different. As we said before, Fuzuli is neither a Lisp nor an Algol family, but it is something like a combination of them.
Any one dimensional Fuzuli list can be created using the list keyword. This keyword corresponds to internal ListExpression and can take infinite number of parameters to hold. An example for use of list keyword is given below:

(let a (list 12 3 4 5 "Hello" 5 4 2 10 2.13))

In the code above, a is list of elements 12, 3, 4, 5, "Hello", 5, 4, 2, 10, and 2.13, respectively. As we can see, the list a can hold elements from any type.

The keyword nth corresponds to the function nth for accessing elements using their indices. An example for use of nth is given below:

(let element (nth a 4))

The variable element now holds the value "Hello" because it has the indices of 4. Note that the index of first elements is zero. 4th element of list a can be changes as

(set a 4 "Fuzuli")

and variable a contains these elements:

12 3 4 5 "Fuzuli" 5 4 2 10 2.13


Lists can be constructed automatically in an increasing manner from an integer a to integer b. The code shown below is for creating a list from 1 to 1000:

(let biglist (: 1 1000))

 Ok! We borrowed this command from R because it is very nice and easy! Let's get the length of this list:

(let mylen (length biglist))

and mylen carries the value of 1000, the number of elements contained by biglist. One may need to append or prepend elements to lists. For those, we have append and prepend keywords for appending and prepending.

# Creating a list
(let mylist (list 1 2 3 4 5 6))

# Appending 7 to the end of mylist
(append mylist 7)

# Put a zero at the beginning
(prepend mylist 0)

# Print out the list
(print mylist)

The output is

[0, 1, 2, 3, 4, 5, 6, 7]

Well! How about removing elements? Lets remove the "4" from this list:

# Removing 4
(remove mylist 4)

The output is

[0, 1, 2, 3, 5, 6, 7]

There is also a find keyword for determining location of a given element in a list. Look at the code:

(let mylist (list "Jan" "Jun" "Aug" "Sep"))
(let index (find mylist "Jun"))
(print index)


The output is 1 because "Jun" has the index of 1 in array mylist.

There are extra functions in utils.nfl package for list operations. Those functions are not built-in but shipped within Fuzuli. Current utils.nfl package contains shuffle, sorta and sortb functions for mixing, ascending sorting and descending sorting of elements of a given list.

Another important point of Fuzuli lists is multi-dimensionality. We mentioned that Fuzuli lists can contain any type of objects. These object can exactly be an other list! And those list can take lists as their elements and so on... Let's create a 2x2 matrix of elements.

(let matrix
    (list
        (list 1 2)
        (list 3 4)
    )
)

(print matrix)

The output is


[[1, 2], [3, 4]]

The matrix given below has a dimension of 3x5:

(let matrix
    (list
        (list 1 2 3 4 5)
        (list 6 7 8 9 10)
        (list 11 12 13 14 15)
    )
)

(print matrix)

The output is

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]

Accessing elements of multi-dimensional lists is easy! Follow the code:

(let matrix
    (list
        (list 1 2 3 4 5)
        (list 6 7 8 9 10)
        (list 11 12 13 14 15)
    )
)

(print (nth (nth matrix 0) 0) "\n")
(print (nth (nth matrix 1) 3) "\n")
(print (nth (nth matrix 2) 3) "\n")
(print (nth (nth matrix 2) 4) "\n")


The output is

1
9
14
15


becase matrix[0][0] is 1, matrix[1][3] is 9, matrix[2][3] is 14 and matrix[2][4] is 15 in C or Java notation.

Have fun with Fuzuli lists!







Sorting data with Fuzuli

In this article, I want to show how to sort data vectors using Bubble sort and Quick sort. Bubble sort is known to be one of the most in-efficient sorting algorithms. Quick sort has lower order of magnitude and it is known to be the most efficient one. There are tons of articles about these algorithms around the Internet and I don't want to give a detailed description and comparison of them.


Let me show how to write them in Fuzuli.



Bubble Sort in Fuzuli:


(def mylist LIST)
(let mylist (list 5 6 4 2 3 9 1 10 5))

(print "Original List:\n")
(print mylist "\n")

(for (let i 0) (< i (length mylist)) (inc i)
    (for (let j (clone i)) (< j (length mylist)) (inc j)
        (if (> (nth mylist i) (nth mylist j))
            (block
            (let temp (nth mylist i))
            (set mylist i (nth mylist j))
            (set mylist j temp)
            )
        )
    )
)

(print "After sorting:\n")
(print mylist "\n")


The output is :

Original List:
[5, 6, 4, 2, 3, 9, 1, 10, 5]

After sorting:
[1, 2, 3, 4, 5, 5, 6, 9, 10] 

 The code is similar to one written in both Lisp, C or Java! Hope the taste of code is good for you. Next one is the Quick sort:

Quick Sort in Fuzuli:


(def mylist LIST)
(let mylist (list 5 6 4 2 3 9 1 10 5))

(print "Original List:\n")
(print mylist "\n")



(function partition (params arr left right)
 (block
  (def i INTEGER) (def j INTEGER)(def tmp INTEGER)
  (def pivot INTEGER)
  (let i (clone left)) (let j (clone right))
  
  (let pivot (nth arr (/ (+ left right) 2)))
  

  (while (<= i  j)
   (block
             (while (< (nth arr i) pivot)(inc i))
                (while (> (nth arr j) pivot) (-- j)) 
       (if (<= i  j) 
     (block
            (let tmp  (nth arr i)) 
               (set arr i (nth arr j))
               (set arr j  tmp)
               (++ i)
               (-- j)
        )
    )
   )
  )
  (return i)
 )
)


(function quicksort (params arr left right) 
 (block
  (def index INTEGER)
  (let index (partition arr left right))
  (if (< left  (- index 1))
     (block
             (quicksort arr left  (- index 1))
   )
  )

       (if (< index right)
   (block
             (quicksort arr  index  right)
   )
  )
 )
)

(quicksort mylist 0 (- (length mylist) 1))
(print "After sorting:\n")
(print mylist "\n")


The output is :

Original List:
[5, 6, 4, 2, 3, 9, 1, 10, 5]

After sorting:
[1, 2, 3, 4, 5, 5, 6, 9, 10]

which is the same for bubble sort but the latter is faster.

Hope you like the code. See you next!