tag:blogger.com,1999:blog-43656701082042892032024-02-26T16:01:27.098+09:00My ProjectAnonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.comBlogger29125tag:blogger.com,1999:blog-4365670108204289203.post-77290223905841315302014-06-12T23:01:00.002+09:002014-08-21T21:19:28.650+09:00Constraints of multiple layouts listview for androidI have migrate my blog to new blog. Click the link below to redirect.<br />
<br /><br />
<a href="http://edisonthk.wordpress.com/2014/06/12/constraints-of-multiple-layouts-listview-for-android/">http://edisonthk.wordpress.com/2014/06/12/constraints-of-multiple-layouts-listview-for-android/</a>Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-64809998419553262672014-04-21T21:53:00.001+09:002014-08-21T21:24:16.064+09:00Setup egit for eclipse ADT by install new softwareI have migrate my blog to new blog. Click the link below to redirect.<br />
<br />
<br />
<a href="http://edisonthk.wordpress.com/2014/04/21/setup-egit-for-eclipse-adt-by-install-new-software/">http://edisonthk.wordpress.com/2014/04/21/setup-egit-for-eclipse-adt-by-install-new-software/</a>Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-56847232818708841482014-04-16T02:17:00.004+09:002014-04-16T02:17:56.185+09:00Login ssh to server without including usernameLet's say you want to login to ssh in very short command. For instance,<br />
<pre class="brush:ruby">ssh myserver
</pre>
<br />
<h3>
How to do it?</h3>
<div>
No configuration on server, just do some configuration on client side.</div>
<div>
<br /></div>
<div>
On client side terminal, open ssh config file. If it doesn't exists, create a new one.</div>
<pre class="brush:ruby">nano ~/.ssh/config</pre>
<br />
<div>
Then, type in following. </div>
<pre class="brush:ruby">host myserver
hostname myserverdomain.com
user myuser
</pre>
<br />
And save it. No restart is required, just try <b>ssh myserver</b>, and you are login as <b>myuser@myserverdomain.com</b><br />
<div>
<br /></div>
<div>
<b><br />
</b></div>
<div>
<b><br />
</b></div>
Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-42747939937445260952014-01-28T18:58:00.000+09:002014-01-28T18:58:21.954+09:00LED Blinky with BGScript ProgrammingIf you are not yet setup the development environment. Check the last post.<br />
<br />
<a href="http://edisontus.blogspot.jp/2014/01/getting-started-with-development.html">http://edisontus.blogspot.jp/2014/01/getting-started-with-development.html</a><br />
<br />
<br />
I am going to guide you how to make LED blink (turn on and turn off) in every 1 seconds with BGScript.<br />
<br />
<br />
<h3>
Create 3 config files (gatt.xml, hardware.xml, project.xml)</h3>
Every file have it's own function and contains the information needed when compile to binary file. These 3 files are config files that is needed and you must always remember to import these in every project. What you needs to do is copy following source code and paste it.<br />
<br />
<b>hardware.xml</b> is configuration file that related to ble112 hardware configuration.<br />
<pre class="brush:xml"><?xml version="1.0" encoding="UTF-8" ?>
<hardware>
<sleeposc enable="true" ppm="30" />
<usb enable="false" endpoint="none" />
<txpower power="15" bias="5" />
<usart channel="1" alternate="1" baud="57600" endpoint="api" />
<wakeup_pin enable="true" port="0" pin="0" />
<port index="0" tristatemask="0" pull="down" />
<pmux regulator_pin="7" />
</hardware></pre>
<br />
<b>gatt.xml</b> exposes the Bluetooth services and profiles.<br />
<pre class="brush:xml"><?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<service uuid="1800">
<description>Generic Access Profile</description>
<characteristic uuid="2a00">
<properties read="true" const="true" />
<value>BG Demo</value>
</characteristic>
<characteristic uuid="2a01">
<properties read="true" const="true" />
<value type="hex">4142</value>
</characteristic>
</service>
<service type="primary" uuid="9000" id="manufacturer">
<characteristic uuid="9100">
<properties read="true" const="true" />
<value type="hex">000780c0ffeef00d</value>
</characteristic>
<characteristic uuid="9101">
<properties read="true" const="true" />
<value>modelnumber</value>
</characteristic>
<characteristic uuid="9106">
<properties read="true" const="true" />
<value>Bluegiga</value>
</characteristic>
</service>
<service uuid="e001">
<description>Battery status</description>
<include id="manufacturer" />
<characteristic uuid="e101" id="xgatt_battery">
<properties read="true" notify="true" />
<value type="hex">ABCD</value>
</characteristic>
</service>
<service uuid="00431c4a-a7a4-428b-a96d-d92d43c8c7cf">
<description>Bluegiga demo service</description>
<characteristic uuid="f1b41cde-dbf5-4acf-8679-ecb8b4dca6fe">
<properties read="true" write="true"/>
<value type="hex">coffee</value>
</characteristic>
</service>
</configuration></pre>
<br />
<b>project.xml </b>tell bgbuild what roles played by every xml file.<br />
<pre class="brush:xml"><project>
<gatt in="gatt.xml" />
<hardware in="hardware.xml" />
<script in="blink.bgs" />
<image out="out.hex" />
</project></pre>
<br />
<br />
<h3>
Create blink.bgs</h3>
<div>
I will explain detail on blink.bgs later. Copy following code and paste to blink.bgs</div>
<div>
<pre class="brush:xml">dim flag
dim interval
# Boot event listener
event system_boot(major ,minor ,patch ,build ,ll_version ,protocol_version ,hw)
flag = 0
interval = $8000
# Configure the P0_0 as output
call hardware_io_port_config_direction(0, 1)
# Enable P0_0 pin
call hardware_io_port_write(0, 1, 1)
# Start a 5 second, one stop timer
call hardware_set_soft_timer(interval, 0 ,1)
end
# Timer event listener
event hardware_soft_timer(handle)
#declare hardware_set_soft_timer to allow it work as forever loop as recursive
if flag = 0 then
# When timer expires disable P0_0 pin
call hardware_io_port_write(0, 1, 0)
call hardware_set_soft_timer(interval, 0 ,1)
flag = 1
else
# When timer expires enable P0_0 pin
call hardware_io_port_write(0, 1, 1)
call hardware_set_soft_timer(interval, 0 ,1)
flag = 0
end if
end</pre>
</div>
<div>
<br /></div>
<div>
<br /></div>
<h3>
Debug with bgbuild.exe</h3>
<div>
bgbuild.exe can be found at {ble_directory} -> bin directory. </div>
<div>
<br /></div>
<div>
Open command prompt and go to blink directory that created at beginning of this article, and run following command.</div>
<div>
<pre class="brush:js">C:\Bluegiga\ble-1.2.1-91\bin\bgbuild.exe project.xml</pre>
</div>
<div>
<br /></div>
<div>
Replace {ble_directory} to your own path. You should get the result look as below.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhibznoxxrcMIpBqfQyE5kakEQPL3AbjmavLHpIzE9-JUOybl_gLUEBdZNl_Am6FfxFkx30bec1JNN_YgWQVhyphenhyphen-gttS-aDferoFQ3PkjPrvPCmzNWmpITt2GJ3SJjw99e-wGC-opKldU48/s1600/img7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhibznoxxrcMIpBqfQyE5kakEQPL3AbjmavLHpIzE9-JUOybl_gLUEBdZNl_Am6FfxFkx30bec1JNN_YgWQVhyphenhyphen-gttS-aDferoFQ3PkjPrvPCmzNWmpITt2GJ3SJjw99e-wGC-opKldU48/s1600/img7.png" height="271" width="320" /></a></div>
<div>
<br /></div>
<div>
<br />
Then, binary file out.hex is generated. It is the file we needed to flash into ble112.<br />
<br />
<h3>
BLE SW update tool</h3>
</div>
<div>
Bluegiga prepared a flash tool called Bluegiga BLE SW Update tool. This tool can be found at BleUpdate directory, it is placed at different place with bgbuild.exe.</div>
<div>
<br /></div>
<div>
Click Refresh and Info to choose the debugger. Then click browse to the out.hex that generated at last section inside blink directory.</div>
<div>
<br /></div>
<div>
Then click update and it will write the binary file into ble112.</div>
<div>
<br /></div>
<h3>
Last job</h3>
<div>
Connect LED to P0_0 pin. Then you will see LED blink at every 1 seconds.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiM7JdM-NHB1gTJ1-1wd-u38708qaqkBSXIHVgM8B5eAvH8NgLj_BoUfWPL4qHcY_wqT8r_6YaMLfsLWsKnJv3GJQGvVxaUtwvgx0lGn0R0N7DODFRjCEbDivIuC-LH0wDyGcDOcM1aTls/s1600/DSC_0782.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiM7JdM-NHB1gTJ1-1wd-u38708qaqkBSXIHVgM8B5eAvH8NgLj_BoUfWPL4qHcY_wqT8r_6YaMLfsLWsKnJv3GJQGvVxaUtwvgx0lGn0R0N7DODFRjCEbDivIuC-LH0wDyGcDOcM1aTls/s1600/DSC_0782.jpg" height="180" width="320" /></a></div>
<div>
<br /></div>
<h3>
Explaination on blink.bgs</h3>
<div>
<i>event system_boot</i> is something like <i>int main()</i> in c language. It is always the first part to be executed after global variables.</div>
<div>
<br /></div>
<div>
dim is used to declare variable. Usually, it means int or char in c language. </div>
<div>
<br /></div>
<div>
$ mark in bgscript used to declared the following number is hexadecimal. In this case, interval = $8000 is same as interval = 0x8000 in c language. Following equation is used to calculate the time for timer. Based on the equation below, clock of ble112 is 32.768kHz, in order to set 1 seconds, interval needed to be set to 32768 ( 8000 in hexdecimal ).<br />
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXmxk44Xd6iF07KnSNuuwWGT_lsZ-fu8U63-7juSsm0Y_QLU_tvCPRJz9wvjT-CRHLwVXlun8OD3JzxmgY8eEJJVIaIeGLoMg6ORLLhr9BK14x0M3UHrZJbg0y55_kdymOefVhrkuQokw/s1600/img91.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXmxk44Xd6iF07KnSNuuwWGT_lsZ-fu8U63-7juSsm0Y_QLU_tvCPRJz9wvjT-CRHLwVXlun8OD3JzxmgY8eEJJVIaIeGLoMg6ORLLhr9BK14x0M3UHrZJbg0y55_kdymOefVhrkuQokw/s1600/img91.png" height="57" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
You might reliased that there is not for or while loop in my script. How can I achieve forever loop? The answer is recursive between hardware_set_soft_timer and hardware_soft_timer. </div>
<div>
<br /></div>
<div>
There is no built in delay event so recursive method is the only way to do forever loop.</div>
<div>
<br /></div>
<div>
hardware_set_soft_timer set the timer start and hardware_soft_timer is the handler event after the time specified in 1st parameter of hardware_set_soft_timer is passed.</div>
<div>
<br /></div>
<div>
By adding hardware_set_soft_timer into hardware_soft_timer, the timer will started again when timer is stop.<br />
<br /></div>
<h3>
End</h3>
<div>
<br /></div>
<div>
Thank you for reading.</div>
Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-44738979957429518502014-01-22T02:49:00.002+09:002014-01-22T03:04:48.303+09:00Simple guide to ExecutorService Android<b>ExecutorService</b> is almost same as ThreadPoolExecutor, it allows you to carry out multiple background thread in sequentially.<br />
<br />
What can ExecutorService do? Do you know that powerful <b>AsyncTask </b>method is only allows to be executed one time in single activity. If you execute AsyncTask more then one time in single activity, you will get the errors.<br />
<br />
So it is where ExecutorService comes, it help you handle multiple background task which AsyncTask cannot be done.<br />
<br />
<br />
<h3>
Try it out!!</h3>
<div>
It is always easy to understand if you get something work first. </div>
<div>
<br /></div>
<div>
First, prepare server script. </div>
<div>
<pre class="brush:php"><?php
echo "hello! I am server.";
?>
</pre>
</div>
<div>
<br /></div>
<div>
Remember the url of your server. Then, copy following android code and replace the url to your own and give permission to internet.</div>
<div>
<br /></div>
<div>
<pre class="brush:java">package com.example.apps1;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mButton;
private ExecutorService pool;
public static final String URL = "http://path_to_server/";
private static final String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mButton = new Button(this);
mButton.setText("Press");
addContentView(mButton, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
pool = Executors.newSingleThreadExecutor();
mButton.setOnClickListener(mClickListener);
}
private View.OnClickListener mClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
pool.execute(new NetworkService());
}
};
private class NetworkService implements Runnable {
private Handler mHandler;
public NetworkService(){
mHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message inputMessage){
String msg = (String) inputMessage.obj;
Toast.makeText(getApplicationContext(), ""+msg, Toast.LENGTH_SHORT).show();
}
};
}
@Override
public void run() {
String recv = HttpRequest(URL);
sendMessage(1, recv);
}
public void sendMessage(int what, String msg){
Message message = mHandler.obtainMessage(what , msg);
message.sendToTarget();
}
}
// Doing all Networking task, if failed to get message from server, it will return null
//
public String HttpRequest(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try{
HttpGet httpGet = new HttpGet(url);
response = client.execute(httpGet);
return EntityUtils.toString(response.getEntity());
}catch(ClientProtocolException e){
Log.e(TAG,"ClientProtocolException : "+e);
}catch(IOException e){
Log.e(TAG,"IOException : "+e);
}
return null;
}
}
</pre>
</div>
<div>
<br /></div>
<div>
Press the button and toast will show you the message from server.</div>
<div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihH0kofjOJjLflr_GwA9m97rfGSdg1Kle5ATmDCg1KZ48bfGVq-n18HsY3c1tU5TFwzTgLFJOepI3VBpdjtDhovWK69vwaqWQFDwrBtgoxMuOdGms-VP8Enp69sbELCI4C-cHa_uOgG9c/s1600/h15.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihH0kofjOJjLflr_GwA9m97rfGSdg1Kle5ATmDCg1KZ48bfGVq-n18HsY3c1tU5TFwzTgLFJOepI3VBpdjtDhovWK69vwaqWQFDwrBtgoxMuOdGms-VP8Enp69sbELCI4C-cHa_uOgG9c/s1600/h15.png" height="320" width="181" /></a></div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<h3>
Explanation</h3>
<div>
ExecutorService can be instanced in few different way.<br />
<br />
<ol>
<li>Executors.newSingleThreadExecutor();</li>
<li>Executors.newFixedThreadPool(10); </li>
<li>Executors.newScheduledThreadPool(10);</li>
</ol>
<br />
Based on how ExecutorService is instanced, ExecutorService will be working in different way. For more detail please checking <a href="http://developer.android.com/reference/java/util/concurrent/Executors.html">Executors API</a>. In this guide, I am using newSingleThreadExecutor, it only allows one task in the same time.<br />
<br />
NetworkService is implemented with Runnable. NetworkService is almost same as AsyncTask, contains UI thread callback method, doInBackground method.<br />
<br />
In this case, same method of AsnycTask and NetworkService<br />
<br />
<ul>
<li>doInBackground == run</li>
<li>onPostExecute == handleMessage</li>
</ul>
<div>
<pre class="brush:java">private class NetworkService implements Runnable {
private Handler mHandler;
public NetworkService(){
// Handler looper constructor to declare this handler
// is going to link to UI Thread
mHandler = new Handler(Looper.getMainLooper()){
// handleMessage method is callback of background work.
// All the tasks in handleMessage will be pass to UI thread
//
// This method is same as AsyncTask.onPostExecute
@Override
public void handleMessage(Message inputMessage){
String msg = (String) inputMessage.obj;
Toast.makeText(getApplicationContext(), ""+msg, Toast.LENGTH_SHORT).show();
}
};
}
// Runnable.run method doing all tasks in background
// It is same as AsyncTask.doInBackground
@Override
public void run() {
String recv = HttpRequest(URL);
// Get the result from HttpRequest and past it to handler.
sendMessage(1, recv);
}
public void sendMessage(int what, String msg){
Message message = mHandler.obtainMessage(what , msg);
message.sendToTarget();
}
}
</pre>
</div>
<div>
<br /></div>
<div>
Every time user press the button, NetworkService will be created and added to background's pool. If there is no task queue in front the NetworkService task, it will be executed.</div>
<div>
<br /></div>
<div>
<pre class="brush:java">pool.execute(new NetworkService()); </pre>
</div>
<div>
<br /></div>
<div>
Background task is not an easy task. There are much more work then the sample code on above such as shutdown or pause the ExecutorService. What I have explain in this article is to get you works on your apps quickly. </div>
<div>
<br /></div>
<h3>
End</h3>
<div>
Hope this article help you in coding. Thank you for reading.</div>
</div>
Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com3tag:blogger.com,1999:blog-4365670108204289203.post-26650294905946172512014-01-21T15:47:00.000+09:002014-01-22T02:52:28.553+09:00Quick Guide to Android Networking by Request Message from ServerA quick guide to programming Android request Server and getting the message. On this guide, I am going to show you how to getting message "hello! I am server." and show it on Android.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD_QHD_M623cz3TgsWNyqa4MFkowcOMGuZNLLzWRYUtTkAyM9ur-sGbUz7uoF3YVjgX59auvH_4G1ruNJTCZtNaQ1s_jmQmBYczZWbr9va0aS9w6QW0lytn2OVRdn1x3b7uEkKYoTGpW8/s1600/h12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD_QHD_M623cz3TgsWNyqa4MFkowcOMGuZNLLzWRYUtTkAyM9ur-sGbUz7uoF3YVjgX59auvH_4G1ruNJTCZtNaQ1s_jmQmBYczZWbr9va0aS9w6QW0lytn2OVRdn1x3b7uEkKYoTGpW8/s1600/h12.png" height="320" width="185" /></a></div>
<br />
<br />
<h3>
On Server side</h3>
<div>
Before programming android, prepare some simple php script on server. </div>
<div>
<pre class="brush:php"><?php
echo "hello! I am server";
?></pre>
<br />
Script it as simple as you can.<br />
<br />
<h3>
On Android side</h3>
Android is quiet complicated with Internet. You needs to enable android to access to internet and work your internet thread on background. Before we get start with our tutorial, copy the code below and debug it. It is always simple to understand if you get the things work first.<br />
<br />
<pre class="brush:java">package com.example.apps1;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView;
private static final String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
addContentView(mTextView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
new ConnectToServer().execute();
}
public class ConnectToServer extends AsyncTask<Void,Void,String>{
@Override
protected String doInBackground(Void... params) {
// replace the url to your own
return HttpRequest("http://path_to_server/");
}
@Override
protected void onPostExecute(String result){
if(result == null){
mTextView.setText("Failed to connecting to server");
}else{
mTextView.setText(result);
}
}
public String HttpRequest(String url)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try{
HttpGet httpGet = new HttpGet(url);
response = client.execute(httpGet);
return EntityUtils.toString(response.getEntity());
}catch(ClientProtocolException e){
Log.e(TAG,"ClientProtocolException : "+e);
}catch(IOException e){
Log.e(TAG,"IOException : "+e);
}
return null;
}
}
}
</pre>
<br />
<br />
Replace the url "http://path_to_server" to your own in HttpRequest.<br />
<br />
Don't debug it, we haven't done yet. As I mentioned above, you need to configure android to able to access internet. What you needs to do is simple, go to your_project_directory and open <b>AndroidManifest.xml</b><br />
<b><br /></b>
Add following uses-permission to in front of <uses-sdk> tag<br />
<pre class="brush:xml"><uses-permission android:name="android.permission.INTERNET" /></pre>
<br />
Then, debug it and you will see the requested message from server.<br />
<br />
<h3>
<b>Explanation</b></h3>
There are 2 things you need to be careful when programming across internet.<br />
<br />
<ol>
<li>Networking thread must be on background</li>
<li>Configure internet permission in AndroidManifest.xml</li>
</ol>
<div>
<br /></div>
<br />
Android is doing a good job for developer, there are built-in background thread method prepared by Android called <b>AsyncTask</b>.<br />
<br />
AsyncTask easy us to doing background task coding by put all the background code in <b>doInBackground</b> method. On the code above, HttpRequest method is the internet task and you will find it I put it on doInBackground method. It means the all the task in HttpRequest will be carried out in background thread.<br />
<br />
<br />
<b>onPostExecute</b> method is a callback method for background thread. After HttpRequest getting the messages, it needs to show it on UI. Here's where onPostExecute comes, the message returned by HttpRequest method is assign to onPostExecute 1st parameter "result". TextView set text of 1st parameter result.<br />
<br />
<pre class="brush:java">@Override
protected String doInBackground(Void... params) {
// doing all networking thread here
//
// 1st parameter of HttpRequest is the url to server. Replace the url to your own
return HttpRequest("http://path_to_server/");
}
@Override
protected void onPostExecute(String result){
// 1st parameter result is the result returned by HttpRequest which doInBackground
if(result == null){
mTextView.setText("Failed to connecting to server");
}else{
mTextView.setText(result);
}
}
// Doing all Networking task, if failed to get message from server, it will return null
//
public String HttpRequest(String url){
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try{
HttpGet httpGet = new HttpGet(url);
response = client.execute(httpGet);
return EntityUtils.toString(response.getEntity());
}catch(ClientProtocolException e){
Log.e(TAG,"ClientProtocolException : "+e);
}catch(IOException e){
Log.e(TAG,"IOException : "+e);
}
return null;
}
</pre>
<br />
<br />
<br />
After coding AsyncTask method, you needs to execute it by<br />
<pre class="brush:java">new ConnectToServer().execute();</pre>
<br />
<br />
<b>Be caution here, AsyncTask is easy but it can only be executed one time in single Activity.</b><br />
<b><br /></b>
It means if you execute AsnycTask for twice, exception will be thrown and you will get errors.<br />
Luckily, Android have prepared another method called ExecutorService. It allows multiple network task can be executed in single activity.<br />
<br />
Check it out with my <a href="http://edisontus.blogspot.jp/2014/01/simple-guide-to-executorservice.html">next post</a>, if you are interesting with it.<br />
<h3>
End</h3>
</div>
<div>
Thank you for reading and hope my blog will help you.</div>
Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-62671242139015858822014-01-19T01:38:00.002+09:002014-08-21T22:21:16.395+09:00Getting started with Developing Bluetooth 4.0 LE and Android with BLE112 Bluetooth moduleI have migrate to new blog. Click the link below to redirect.<br />
<br />
<a href="http://edisonthk.wordpress.com/2014/01/18/getting-started-with-developing-bluetooth-4-0-le-and-android-with-ble112-bluetooth-module/">http://edisonthk.wordpress.com/2014/01/18/getting-started-with-developing-bluetooth-4-0-le-and-android-with-ble112-bluetooth-module/</a>Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-36350372167322621142013-11-10T00:46:00.000+09:002013-11-10T00:47:04.620+09:00Daemon Thread ["http-bio-8080"-exec-2] (Suspended (exception RuntimeException)) on Eclipse for Tomcat 7Developing Tomcat application on Eclipse IDE, and if you are facing the problem that RuntimeException keep poping out and you can't get rid of it without stopping the server. Based on <a href="http://stackoverflow.com/questions/6290470/eclipse-debugger-always-blocks-on-threadpoolexecutor-without-any-obvious-excepti">stackoverflow's answer</a>, it can be easily solved by uncheck the option in Preferences.<br />
<br />
<h3>
Solution</h3>
Uncheck <b>suspend execution on uncaught exceptions options</b> in Preferences. Go to Window > Preferences > Java > Debug and uncheck Suspend execution on uncaught exceptions.<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-66859710622820987182013-09-29T20:12:00.000+09:002014-08-21T22:20:14.459+09:00How to inject javascript in android webview like chrome javascript consoleI have migrated to new blog. Click the link below to redirect.<br />
<br />
<a href="http://edisonthk.wordpress.com/2013/09/29/how-to-inject-javascript-in-android-webview-like-chrome-javascript-console/">http://edisonthk.wordpress.com/2013/09/29/how-to-inject-javascript-in-android-webview-like-chrome-javascript-console/</a>Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-28700436110383018152013-09-23T17:24:00.001+09:002013-09-24T01:27:08.580+09:00Simple introduction about what is Ruby on Rails<a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=9584834" rel="tag" style="display: none;">CodeProject</a>
<br />
<br />
I begin to learn ruby on rails since begin of this August. I found out that ruby on rails is awesome, it speed up all my works and make it simple to maintain. So, I wrote this article to introduce about how awesome rails is.<br />
Back to our topic, what is ruby on rails? Web application development framework with ruby language. For more details, you can do some searching on what is ruby on rails, there are many sources you can found on internet.<br />
<br />
<h3>
</h3>
<h3>
Installation</h3>
It is always easier to be understood by running up the things.<br />
To install rails on your windows or mac PC. There is a quick way to install it by download <a href="http://railsinstaller.org/en">rails installer</a> and install it.<br />
To install rails on linux os such as ubuntu, debian might needs a little bit more works then windows and mac. There are sources on internet, check it out.<br />
<br />
<h3>
</h3>
<h3>
To begin a new project</h3>
Before getting start to develop a new web application. You need to setup database, directory of your new project, .htaccess to control the traffic. Maybe you will spent almost half an hours to one hours to setup all those things. But …<br />
<em>Rails, will do it automatically for you. </em><br />
By typing a few commands for it, rails will do it for you. Here’s the commands.<br />
<pre class="brush:js">rails new MyProject
cd MyProject
rails generate model Item name:string price:integer description:text
rails generate controller items index new create show edit update destroy
rake db:migrate
rails server</pre>
<br />
On first command, create new project and name it "NewProject". Second command create table name item and columns of name, price and description. Third command create directory for html file, ruby file and routes. Fourth command to startup database and last command to startup the server. <br />
<br />
Everything will be setup within 1 minutes. It means, you can begin your programming after 1 minutes. Cool right! <br />
<br />
Go to <a href="http://localhost:3000/">http://localhost:3000</a> and you will see the welcome message of rails.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhTrkRj9BgFYQYEyjdY6E10GppDdWSTOVGtyZ1OV8-rC3o8Nlq_xVkfcjkXHWGxbKJ6JJHZr7cB9rzy2n-nAILu-APqD8NXXE4_eXgPl_C3YN_lNwErFKYN9U65roeNJbpeQMt-H0cn_k/s1600-h/rails2%25255B2%25255D.png"><img alt="rails2" border="0" height="244" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkY1HrfiaGHyaWIm6_yAfmPbE2wjVUkdXDHMCKoGV1XJoThDVUqbBrs8e55OiGQSHgrROoIMiOszGh9ewypFMFyuH8zyvPc4NMzqkMXomDQGIBJcezzaEHXUXho9eMXekwZ5O_0sVgWNk/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="rails2" width="244" /></a> <br />
<br />
To check with the project generated, go to <a href="http://localhost:3000/items">http://localhost:3000/items</a><br />
<br />
The page is blank. This is because all you have generated is database, system and routes. There will nothing to show unless html code is written.<br />
<br />
<h3>
</h3>
<h3>
Scaffold</h3>
A powerful tool that prepared by rails called Scaffold. Scaffold will generate everything including database, simple system code, traffic, html layout code. It means, you can begin to coding from half way instead of blank file. <br />
<br />
How to use it? By a few simple command<br />
<br />
First create a new project. In this case, named NewProject<br />
<pre class="brush:js">rails new NewProject</pre>
<br />
Then, generate scaffold with following command and startup server.<br />
<pre class="brush:js">rails generate scaffold item name:string price:integer description:text
rake db:migrate
rails server</pre>
<br />
Here’s the result generated by scaffold. Check it out<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxocVcAC7VIg3qBQGFWlmDpCKRxdZdhIPYKbgjGbXN5Z0bSQwf8ksePf0co58MyTIruvCyUM4vFGcDWeyMTLiC5E9_EtSZGKRUOz98QSC3lKGK6oUhee3k3tYucs3GEblmxlijVrLLuVY/s1600-h/rails1%25255B2%25255D.png"><img alt="rails1" border="0" height="203" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWB0Qnv66qiG25nrrYKvaPmIYHuETr8ECEwEwGsW8OmbMK6ogJugSIb9lxR65cMlxpc2ISIUfBG0fnqOqxWY3lgWYGlxYEXzUNFndSg58cLNJ5xBScP8zd1ASawusUK75I5ukVm0QIQvY/?imgmax=800" style="border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; display: inline;" title="rails1" width="244" /></a> <br />
<br />
Instead of blank project, scaffold will generate a template for us to make a quick way to start. <br />
<br />
<h3>
</h3>
<h3>
End</h3>
It is the end of the article, do you think that ruby on rails is awesome?Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com5tag:blogger.com,1999:blog-4365670108204289203.post-78371470151591394372013-09-14T17:13:00.001+09:002013-09-14T17:14:43.896+09:00Event prevent default is not working<br />
If you are working on key action with jquery and having problems on event.preventDefault is not working. Let’s me explain something simple to you.<br />
<h3>
event.preventDefault</h3>
As given example on <a href="http://api.jquery.com/event.preventDefault/">docs</a>, it shows us how it works on click action. For instances, prevent default action of anchor request the given url when clicked, action of submit button clicked to submit the form. <br />
So far so good.<br />
<br />
<h3>
bind.( “keydown” , function( event ){ return false; } );</h3>
preventDefault is good but when it comes to key action such as enter key on submit form and tab key on switching the focus of input element. It lose it effects. You will found out that enter key are still performing default action and others key as well. <br />
So… what is the solution? Adding return false on handler. Snippets below shows an simple example on this solution.<br />
<pre class="brush:javascript">$(document).bind('keydown', function(e){
return false;
}); </pre>
<br />
It’s works right! <br />
<br />
What happens if you want only ‘enter’ key to be prevent default action. Here’s the solution.<br />
<pre class="brush:javascript">$(document).bind('keydown', function(e){
var keyPressed = ('which' in e) ? e.which : e.keyCode;
if( keyPressed == 13){
return false;
}else{
return true;
}
}); </pre>
<br />
Enter keycode is 13. Return true on event handler will allow the key to continue with default action and return false will stop event handler from continue to default action.<br />
<br />
<br />
<br />
<h3>
It’s still not working</h3>
<br />
There are a few quite common mistake make by people, especially for me. Either you too?<br />
<br />
<ul><br />
<li>Errors inside the handler function</li>
</ul>
<br />
If there is errors (exp. syntax errors) occured inside the handler function, default action will be carry out even though there is return false in handler function. Don’t tried to ignore the errors that shows on your browser, it might be the core of your problems.<br />
<br />
<ul><br />
<li>Mess up with JQuery instances and native Javascript instances</li>
</ul>
<br />
What does it means? Check it out with the differences at following codes.<br />
<pre class="brush:javascript">$("ul").children("li")[0].html("");
$("ul").children("li")[0].innerHTML = "";</pre>
<br />
It is quite tricky problems, html() is jquery properties and innerHTML is javascript properties. But on this case, children(“li”) returns native javascript element array, so there is no method for html(“”) in returned element array. So, second code is the correct answer.<br />
<br />
If you have do some checking by Object.keys( $(“ul”).children(“li”)[0] ); you will found out that the properties is different from jquery properties.<br />
<br />
<h3>
</h3>
<br />
<h3>
Conclusions</h3>
<br />
High level language doesn’t means you don’t have to understand the basic of it. Understanding basic sometimes will be the helper on solving problems.<br />
<br />
Good luckAnonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-26655255281591862972013-09-06T21:04:00.000+09:002013-09-06T21:04:11.021+09:00How to open select input method option menu programmatically on AndroidHere's the snippets for Android to open the select input method option menu programmatically.<br />
<br />
<pre class="brush:java">InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (mgr != null) {
mgr.showInputMethodPicker();
}</pre>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFC3i-XUPSU2YiDxQPEuIOd2q3uBYovOXYv4owVYXrqgs1NoIcPjlrY-DBFGiI6WEtKhHtJBZ_Pc0AMP9ezo9Rtsm-wwgR_uK0N4Gkkcfr7ItMTE5o1nGAC3kRLFW_A_B_xl4ppqt5Ru8/s1600/b3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFC3i-XUPSU2YiDxQPEuIOd2q3uBYovOXYv4owVYXrqgs1NoIcPjlrY-DBFGiI6WEtKhHtJBZ_Pc0AMP9ezo9Rtsm-wwgR_uK0N4Gkkcfr7ItMTE5o1nGAC3kRLFW_A_B_xl4ppqt5Ru8/s320/b3.png" width="261" /></a></div>
<br />Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-46887249184018946342013-08-29T00:05:00.001+09:002013-09-07T15:43:24.259+09:00Install ruby on rails for Aptana Studio 3 on Windows<br />
I spent almost half of my day to install ruby on rails on my Windows PC. After a long fighting on installing, I found an <strong>EASY</strong> way for windows. Could be said as DAMN EASY.<br />
<br />
What you need to do is download the latest version from <a href="http://railsinstaller.org/en">Rails installer site</a> and run it. Don’t need to download Ruby Installer, as it is included inside Rails installer. What you need to do is waiting until it’s done.<br />
<br />
After installed, you can test it by open command prompt and go to whatever testing directory and type <br />
<pre class="brush:ruby">rails new TestApps
cd TestApps
rails server</pre>
<br />
Then, open browser and go to <a href="http://localhost:3000/">http://localhost:3000</a> .If you are able to see the page. Congratulation, you are done with ruby on rails install.<br />
<br />
Last things is to download <a href="http://www.aptana.com/products/studio3/download">Aptana Studio 3</a>. <br />
<br />
Caution here, make sure you install rails first instead of Aptana studio. Otherwise, you have to configure the path for rails on Aptana studio.<br />
<br />
If you received error "Unable to find 'rdebug-ide' binary script. May need to install 'ruby-debug-ide' gem, or may need to add your gem executable directory to your PATH", when debug ruby in Aptana studio.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXen3ksPDzuHBqMVHnRx4n8e-Sxk1j19QwFdK38fFjSMDG7J25The9EgyFW2Faz_4e7opoLkVonyHV58_DMb__yae23BJMKbvbMuxqbpwckQTvU0TnMSJIcSXk7_zqH76Qmpa1B6xylLk/s1600/b4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="169" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXen3ksPDzuHBqMVHnRx4n8e-Sxk1j19QwFdK38fFjSMDG7J25The9EgyFW2Faz_4e7opoLkVonyHV58_DMb__yae23BJMKbvbMuxqbpwckQTvU0TnMSJIcSXk7_zqH76Qmpa1B6xylLk/s320/b4.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
Install ruby-debug-ide by following command<br />
<pre class="brush:js">gem install ruby-debug-ide</pre>
<br />
Then, restart your Aptana studio. You should be able to debug ruby file.<br />
<br />
Okay! Hope you are already in coding. Thank you for reading.Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-83273826640490239312013-08-21T01:26:00.001+09:002013-08-21T01:59:57.444+09:00Crop Images into pieces for java appletBy using CropImageFilter class, images can be cropped easily with few lines. According to <a href="http://download.java.net/jdk7/archive/b123/docs/api/java/awt/image/CropImageFilter.html">Java docs</a>, parameter of CropImageFilter constructor is<br />
<pre class="brush:java"> CropImageFilter(int x, int y, int w, int h)</pre>
<br />
First, load the image from folder. Here's is the method I prepared.<br />
<pre class="brush:java"> private Image loadImage(String img){
return getImage(app.getDocumentBase(),"img/"+img);
}</pre>
<br />
For eclipse IDE, I put my images file inside the src/img package.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc52f8GkOpMZ7fNX9nhEwMqFEJVIoEGoGiDxWrg3Tad4UFGSgd_gL7aTTyaHIZ4XtMQLWh0Q3WxwMjjf_MJ02cxzTFe6o7YK7oT_piRlsLx6MiAeIdYHJ8O2xzsqkXil0zBtItqHT8_Ks/s1600/load1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc52f8GkOpMZ7fNX9nhEwMqFEJVIoEGoGiDxWrg3Tad4UFGSgd_gL7aTTyaHIZ4XtMQLWh0Q3WxwMjjf_MJ02cxzTFe6o7YK7oT_piRlsLx6MiAeIdYHJ8O2xzsqkXil0zBtItqHT8_Ks/s1600/load1.png" /></a></div>
<br />
<br />
After load the image, let's say "image_want_chop.png" is loaded and chop it with following code. <br />
<pre class="brush:java"> Image originalImage = loadImage("image_want_chop.png");
ImageFilter imageFilter = new CropImageFilter(x,y,width,height);
Image choppedImage = createImage(new FilteredImageSource(originalImage.getSource(),
imageFilter ));</pre>
<br />
Thank you for reading.Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-58720673871558505922013-08-20T15:30:00.001+09:002013-08-20T15:39:06.421+09:00Split MySQL select data into multiple pages by using LIMIT<br />
Our goal is to split the selected MySQL data into multiple pages and display current page and the data show on specify page.<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYNnwH0_RUeauzPZhaStBsKo4C2E3Efv6RRSeLPfrMHwqopmkCI00KJC_tLxt3F0-XaPihTnqkt6HU_AqfLLLNifFBGTbDuK32EJzV4jxC_I3Lt2NPNPNKm5OP0wjaG9-fJ2talRnKYxk/s1600-h/mysql_page1%25255B2%25255D.png"><img alt="mysql_page1" border="0" height="40" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhs0Vh88vNUbrkkBbJ-SdUNuFhArap0WeBHjziSwnUZofYx12eIfgychbAEz0s4VoodyCWfCBu2wA2J8-QwCU7LeJCY2bOkW1rfkcmOHwQ1iN88-f7FJSKkEk2K5NI5Z7VosVbrZnZbrYQ/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="mysql_page1" width="244" /></a> <br />
<h3>
<strong><br /></strong></h3>
<h3>
<strong>Introduce to LIMIT clause</strong></h3>
Let’s say the limit of data on single page is 10 rows, and current page is page 2. It means I need to show the data begin from 20th to 30th. So what I need to do is to set the offset value and length of data selected.<br />
<pre class="brush:php"> $offset = 0;
$length_of_data = 10;
$query = "SELECT * FROM mytable LIMIT {$offset} ,{$length_of_data}";</pre>
<br />
The parameter of LIMIT clause is LIMIT offset, length<br />
<br />
<h3>
Optimize the query</h3>
<br />
To prevent sql injection, we need to modify the code above into bottom.<br />
<pre class="brush:php"> $length_of_data = 10;
$query = "SELECT * FROM mytable LIMIT :offset ,{$length_of_data}";
$page = intval(trim($_GET["page"]));
$offset = $page * $length_of_data;
try{
$stmt = $db->prepare($query);
$stmt->bindParam(':offset',$offset,PDO::PARAM_INT);
$stmt->execute();
}catch(PDOException $ex){
die ($ex);
}</pre>
<br />
Caution : According to <a href="http://stackoverflow.com/questions/2269840/php-pdo-bindvalue-in-limit">stackoverflow</a>, seem like there is a bug when using execute(array(“:offset”=>0)) or bindParam(“:offset”,trim($offset),PDO::PARAM_INT) without intval function. So it is better using what I given on above. I hope it works for your guys.<br />
<br />
You can test it by add ?page=0 to your end of the url. It will show you the first 10 rows data without offset.<br />
<br />
<h3>
Display current page and all available pages</h3>
<br />
It is the last things we need to do. First get the total rows number from table. Then show the current page with special view and other pages with normal view.<br />
<pre class="brush:php">
$query = "SELECT * FROM mytable";
try{
$stmt = $db->prepare($query);
$stmt->execute();
}catch(PDOException $ex){
die ($ex);
}
$rows = $stmt->fetchAll();
$count = count($rows);
for($num = 0;$num < $count / $limit_row;$num++){
if($page == $num){
echo '<a href="forum_main.php?page'.$num.'"><span class="page_number current">'. ($num+1).'</span></a>';
}else{
echo '<a href="forum_main.php?page='.$num.'"><span class="page_number">'.($num+1).'</span></a>';
}
}</pre>
<br />
Here’s we done! Thank you for reading. Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-7780327938121571422013-07-06T20:12:00.001+09:002013-07-06T20:31:41.675+09:00Simple Custom Dialog for Android<p>Android developers have provided us based on how to create a custom dialog by using alertdialog.builder method. If you are interesting with it, here’s the link. <a href="http://developer.android.com/guide/topics/ui/dialogs.html">http://developer.android.com/guide/topics/ui/dialogs.html</a></p> <p>I am going to shows another way instead of using alertdialog.builder. What is the benefit of this method is you can update the content of dialog view dynamically and directly.</p>
<p>If you are thinking changing the content of dialog when the button is pressed without closing dialog. You might give a try on this method.</p>
<p>First, create the layout for custom dialog</p>
<pre class="brush:java"><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"<br /> android:orientation="vertical" ><br /> <br /> <TextView<br /> android:id="@+id/dialog_text"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="this is custom dialog"/><br /> <br /> <LinearLayout <br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:orientation="horizontal"><br /> <Button<br /> android:id="@+id/dialog_positive_button"<br /> android:layout_width="0dip"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="0.5"<br /> android:text="positive"/><br /> <Button<br /> android:id="@+id/dialog_negative_button"<br /> android:layout_width="0dip"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="0.5"<br /> android:text="negative"/><br /> <br /> </LinearLayout><br /><br /></LinearLayout><br /></pre><br /><p>Then, create the custom dialog class</p>
<pre class="brush:java">
class SimpleCustomDialog extends AlertDialog{
TextView text;
SimpleCustomDialog(Context ctx){
super(ctx);
setTitle("Custom Dialog");
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.alertdialog_layout, null);
text = (TextView)view.findViewById(R.id.dialog_text);
text.setText("this is custom dialog");
Button positiveButton = (Button)view.findViewById(R.id.dialog_positive_button);
Button negativeButton = (Button)view.findViewById(R.id.dialog_negative_button);
positiveButton.setOnClickListener(new PositiveButtonListener());
negativeButton.setOnClickListener(new NegativeButtonListener());
setView(view);
}
class PositiveButtonListener implements View.OnClickListener{
@Override
public void onClick(View view) {
text.setText("Button clicked");
}
}
class NegativeButtonListener implements View.OnClickListener{
@Override
public void onClick(View view) {
// dialog wouldn't dismiss itself, you have to call dismiss method
//dismiss dialog
SimpleCustomDialog.this.dismiss();
}
}
}
</pre>
<br /><p>finally call show method to show dialog</p><pre class="brush:java">SimpleCustomDialog mDialog = new SimpleCustomDialog(this);<br />mDialog.show();<br /></pre><br /><p>If you want to dismiss the dialog, just call dismiss method.</p><br /><p>Hope this article helps you. Thank your for reading.</p> Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-74767681798168913222013-06-16T20:11:00.001+09:002014-08-21T21:27:57.595+09:00Installation Ubuntu into MK808BI have migrate my blog to new blog. Click the link below to redirect.<br />
<br />
<br />
<a href="http://edisonthk.wordpress.com/2013/06/16/installation-ubuntu-into-mk808b/">http://edisonthk.wordpress.com/2013/06/16/installation-ubuntu-into-mk808b/</a>Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-14714532052951913662013-06-10T19:38:00.001+09:002013-06-25T22:53:05.729+09:00How to customize your code on blogger ( SyntaxHighlighter )<br />
<h4>
Introduction </h4>
I want to show everyone how to make your source code looks neat and tidy like the sample code below.<br />
<pre class="brush: js"> /**
* SyntaxHighlighter
*/
function foo()
{
if (counter <= 10)
return;
// it works!
}</pre>
<br />
<br />
What your have to do is pretty simple. There are only 3 steps.<br />
<ol><br />
<li>Configure your blog by copy and paste the links I given into your template header </li>
<li>Copy your source code you want to show on your blog and paste it on HTML </li>
<li>Wrap your source code with <pre> tag</li>
</ol>
<br />
Well, if you are still don’t understand what we are going to do. Just follow my instruction.<br />
<br />
<h4>
1st step</h4>
<br />
As what I have said on above, <strong>copy the following links </strong>.<br />
<pre class="brush:html"><link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushAS3.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushColdFusion.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDelphi.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDiff.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushErlang.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushGroovy.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJavaFX.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPowerShell.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushScala.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script language='javascript' type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script></pre>
<br />
Where to paste? Template header ! First login into your blogger dashboard.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi4zDAAKRoWaLAl54Jc3ItbiLL1N-nHRjaf_WPu3UaG_htEfSLCZEnSo42jS0sXBkiKe6saFvoqj_oyL2hvwjnQO2rrXOU40dXPB_rNWWhexW4LTPbgBdRoIVs7BnNHUyCWBcw7F9n6cE/s1600-h/1%25255B10%25255D.png"><img alt="1" border="0" height="304" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgs5LVL-t5KzIL7m0u5LmDOLr0y-FlS_aqaJ28BrnsgYTf8KJ_KjNAkagmPQhszWjC8MRd0I0WOc0bNwjiudgcotuE9zC_hfDQYY9utFNYAMPd1cnf8wIPqbqD3iG7wV1MCiaz0R1ZuEVY/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="1" width="542" /></a> <br />
<br />
Then, click “template” on left-hand-side.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaSUpxEMbvitgUGz-Xbb4_6SVNpDq_EnG1XGm8ZZzBMAikrkr_fAMp0GfX4Lg3KxkyZS2mhQHsnUAvD3iTOXOiEJoQHUG1Sh8VvFe-sUj56uPqvxFpx57_dVGR02K94tSceMjY_OVgumI/s1600-h/2%25255B3%25255D.png"><img alt="2" border="0" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihuk-dnneIzI0AUoxIVxgCGTTYWAXRKxpAPk0-DU2eCOAfokm5tRKDjRIdlZ2IUk3t61ctFlOhbDSZq8sV-p7j7BadUcyhtuAD4bYYVCQ7Ngny2v9ufqzQP4CBfH0qfnWyDl5jCmB6RN8/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="2" width="547" /></a> <br />
<br />
Then, click “Edit HTML” <br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQOCI61lFd0Xd_K1BHsZ67wI0EJp-P_uTMPiSpprN_4W3BEmoA4su8sAaCWfDqoY9T9Os7YhOtfy9CZFW81mYnAK41LkI_VPYIsYhKgvkXPOpXnQvCBHCau_np0QzR_CA0T49v1UWifsc/s1600-h/3%25255B4%25255D.png"><img alt="3" border="0" height="257" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVJfmcv5NU-abCM7IAfWXg549zpIt81Lz95Hw7CKmQDxQjbc41jiDYc3e8vuDu4Plwe12dah4ZOLi0PtwpQpCSCtddfLslG-Zg945xR36o0GpzXdD5d5HhUt7xvPv26oL8wp80-HVYfG8/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="3" width="552" /></a> <br />
<br />
<br />
Find the header close tag </head> and paste the code above the header. <br />
<br />
Here’s we are done with configuration. Let’s do some simple test on it.<br />
<br />
<h4>
Second Step</h4>
<br />
Open new post. And switch your edit mode from compose to HTML.<br />
<br />
Let’s say you have following source code.<br />
<pre class="brush: js">
/**
* SyntaxHighlighter
*/
function foo()
{
if (counter <= 10)
return;
// it works!
}</pre>
<br />
Paste the source code into your content. Remember, you must paste it in HTML mode but not Compose mode. <br />
<br />
If there is any symbol like <> in your code, you must change them to &lt; and &gt; respectively.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjImnHZJgI_jb3GI3nT7k-i18H3Sgw33PNt9Z493AnEeCukOePUFIsdZT5n2ViYRs1RoRopMEVYCuGC-NTE2eRRpJXWMgBiNAh6vnPuj9DBrUfhax9uyQ_WxsZn2nn7j5apGJ57gHjtdBo/s1600-h/4%25255B3%25255D.png"><img alt="4" border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo9q1M8V9t1nmO18n95ksR8leCCvcTuZDGSqS1ZCv0Z-4CAdqSUwAwb3PjFMOoME8Dk9s5YHqn3YTYgsneKsn-YW8goZNMf89snIDXzkU1l2HRKr5bKa5iHgdQd3wH__Cv2jTXhU3B1dU/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="4" width="505" /></a> <br />
<br />
It should looks like above.<br />
<br />
<h4>
Final Step</h4>
<br />
After you have paste your source code into content, we have to wrap your source code with <pre> tag. <br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl1-z2OWp8yw6U_exyTi0WVNc0OjcMhRBoDs9WpPO1DMV0T0Dr84kgEU5l0BCdmOOytDYUwZRPCscoBVpKd-29behRo6Nb-q4Ebpi8hkmv_x_Wno4jkfZ3P8IwvUhXtJendzFBDxnyGq0/s1600-h/5%25255B3%25255D.png"><img alt="5" border="0" height="268" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgavD1lPp6snkvjIa0G2bH2oqdXz-OlP2yWy3X7BpSadFFIA6cxdjOQr09xEsXZQjjWGuOzqy4bfg4HidYMsmvqiemQqNXS_fj4jAjxoqwjlLcVn2ZaHDHX2a99jukeib7E9laecRhM5DM/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="5" width="512" /></a> <br />
<br />
<br />
Here’s we done! Save it and view it.<br />
<h4>
Link</h4>
The source code and library above is provided by <a href="http://alexgorbatchev.com/SyntaxHighlighter/">SyntaxHighlighter</a>. Check it out for more details.<br />
<br />
Thank you.Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-77847288021069178462013-05-01T03:21:00.001+09:002014-08-21T22:29:51.970+09:00Networking and Socket programming Tutorial on C<a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=9584834" rel="tag" style="display: none;">CodeProject</a>
<br />
I have migrate my blog to new site. Click link below to redirect.<br />
<br />
<a href="http://edisonthk.wordpress.com/2013/04/30/networking-and-socket-programming-tutorial-on-c/">http://edisonthk.wordpress.com/2013/04/30/networking-and-socket-programming-tutorial-on-c/</a>Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com4tag:blogger.com,1999:blog-4365670108204289203.post-89267751211232671162013-04-18T18:27:00.001+09:002014-08-21T22:38:51.213+09:00Undo Algorithm for Java SwingI have migrate my blog to new site. Click the link below to redirect.<div>
<br /></div>
<div>
<a href="http://edisonthk.wordpress.com/2013/04/18/undo-algorithm-for-java-swing/">http://edisonthk.wordpress.com/2013/04/18/undo-algorithm-for-java-swing/</a></div>
Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-77953394462009080692013-04-10T11:38:00.001+09:002013-04-10T11:38:38.272+09:00Beginner Question - What is SSCCE?<p>For beginner programmer, you will found out this words comes out very often on java software developing forum such as stackOverflow. What does SSCCE mean?<br><br>If you have looks at the <a href="http://sscce.org/">SSCCE link</a>. 5 big words is highlighted on the top of the page. </p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-E2Iu65jvedkcFQQu3SFaisvgl9T6HPfAuMciNs-fkgOMeEbUXhhDr47i7HcrxJto5P34NWZozDJ_P0aiIqZyGpXgJM91x-PbXjmUGg0h1t_EyKHe5B1NYhyguuZ_XpotOeIOPvINc6M/s1600-h/sscce13.png"><img title="sscce1" style="border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; float: none; margin-left: auto; display: block; border-top-width: 0px; margin-right: auto" border="0" alt="sscce1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-d-gDY50_i6toiZrfUxBOS9G9nAMeuKx0_8lTWDbLbEpdzgYp9DNFVC491_BpC-cLuuEzJX0IfQ6E449okvw1-zN7EbWWJF3CBOgtIEPFVKOEGhdkp2G1T9gHHIHCf6jtT0mpMBySuK4/?imgmax=800" width="378" height="67"></a> </p> <p>And the following things is the content that filled with dozens of words to explain what does it means and why do we need to do it.</p> <p>I know many programming beginner is short of patient and want their answer comes out immediately instead of reading a long long article to understand the content. <strong>SO,</strong></p> <p>What is SSCCE? It is</p> <ul> <li>Make your question as short as possible <li>Make your question easy to be understand <li>Post compilable code instead of parts of your work ( Simple code applying your problems. exp. public static void main )</li></ul> <p>Usually, people say “<strong>please post sscce</strong>”. It means post a runnable code that show the problems you are having and can be copy and paste and straight to be compiled on IDE (IDE means the software that used of developing application, exp. eclipse IDE, visual studio)</p> <h3>Why do we needs to do so?</h3> <p>No matter how well your English is, the best way to tell people what wrong on your code always is allow others compile you works and find the actual error.</p> <p>For example,</p> <p>Pretend that you are developing Java Swing application and you are having problems of label is not showing on windows. Here’s the code provided.</p><pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; width: 400px; background-color: #fbfbfb"><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 1: <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> createLabel extends JLabel {<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 2: <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 3: <span style="color: #0000ff">public</span> createLabel() {<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 4: super();<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 5: setText("<span style="color: #8b0000">HELLO WORLD</span>"); <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 6: setLayout(null);<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 7: }<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 8: <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 9: }<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 10: </pre></pre><br /><p>And the problems look like following.</p><br /><p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFQwyw1KvVVeIMab7m5l_GezWXSN-c8QoVAInUQDxzoi_zhfmWyJmJFEXCkrQ9p21H8VlWvcCymMBj7XFaos5QO533hl8dBAnIWle0CPScwGx0IRxksLkL2WZnMo1xmKSLHdL8vBsLrpw/s1600-h/eeccs22.png"><img title="eeccs2" style="border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px" border="0" alt="eeccs2" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjM-Ey7CRGj6vs8SFJ3zlZRzr2RKMULM6-MHEf_AfXMbC5DrwF910knskWOIZq_sYpbvTZComI44afFz5nbcrCkOdhFdqGpBSTh0qd5Alr8XdndtCb_uhQ8fDxbUMZI6ousnz7KuC_JNZo/?imgmax=800" width="202" height="132"></a> </p><br /><p>Maybe you might think that label is not showing because there is an error on your label code so you only post the part of the label. However, you wouldn’t be able to solve it if you only post label code. Let’s me show you why. Take a look at following whole code.</p><br /><p> </p><pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; width: 400px; background-color: #fbfbfb"><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 1: import javax.swing.JFrame;<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 2: import javax.swing.JLabel;<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 3: <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 4: <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> createLabel extends JLabel {<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 5: <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 6: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> void main(<span style="color: #0000ff">String</span>[] args) {<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 7: JFrame frame = <span style="color: #0000ff">new</span> JFrame("<span style="color: #8b0000">Title</span>"); <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 8: JLabel label = <span style="color: #0000ff">new</span> createLabel();<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 9: <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 10: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 11: frame.setLocation(300,200);<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 12: frame.setSize(200, 130); <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 13: frame.setVisible(<span style="color: #0000ff">true</span>);<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 14: }<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 15: <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 16: <span style="color: #0000ff">public</span> createLabel() {<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 17: super();<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 18: setText("<span style="color: #8b0000">HELLO WORLD</span>"); <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 19: setLayout(null);<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 20: }<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 21: <br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 22: }<br /></pre><pre style="font-size: 11px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"> 23: </pre></pre><br /><p>If you tried to provide the compilable code instead of part of the code. We are able to help you find out what exactly you are having.</p><br /><p>From the source above, we can understand that the reason why label is not showing on windows is due to frame.add(label) is not added. There is no problems on label code. So if you are provided a part of the code, you wouldn’t be able to found out what exactly problems you are having.</p> Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-35543404718700314582013-04-01T18:05:00.001+09:002013-04-15T09:27:26.620+09:00Square Root algorithm for C<a href="http://http//www.codeproject.com/script/Articles/BlogFeedList.aspx?mid=9584834" rel="tag" style="display: none;">CodeProject</a>
<br />
<h3>
Introduction</h3>
<h5>
</h5>
<h1>
</h1>
For those who are looking for how square root algorithm is working and “YES !, Welcome to my blog”, but for those who are simply looking for performing square root your programming and I have to tell you, what you have to do is do some google searching on “math.h” header file to look for square root function that prepared inside it. <br />
Well, let’s begin our lesson “How square root algorithm is working on c”<br />
It’s not very difficult once you have understand how following equation is calculated<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2Kt4TMjv3vGVJw8uKpuhNF3ZrTJ5hQmDKsQNRBPpf9x_LAiZg_NZBrgj9Ew7ADkW2EQQD2yvKQbJQgfsdwhsp83LQMOtdk8biKhkbeXeeswM8bbzgpfM9_eXV2hSf3YPD87Lvp2fVOYc/s1600-h/eq2%25255B3%25255D.png"><img alt="eq2" border="0" height="196" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNEQdnQsJDu1s5iBRAMoU7mauXwKpW4q2LQDr09Lpkj6WhJWl3Bo-2F4XurqBkrOLmL8Vcd4tVQr5sMRp4uIvLEQ9xFNo7TVsAFzzXqFFCpiL4RlfifPNqoElPnwu0hu9Yzh8rxY-Gzfc/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="eq2" width="286" /></a> Left hand side of the diagram shows the square root of 152.2756 and right hand side shows the square root of 2.<br />
For those who have understand how this calculation is been done, please skip to the code as I am going to explain the how to solve this equation.<br />
<h2>
</h2>
<h3>
Basic Principle of Square Root</h3>
Based on the question given above, we understand that square root of 152.2756 is 12.34 and the square root of 2 is 1.4142. If you do not believe, try calculator to find the answer.<br />
Suppose we are finding the square root of 2 and the answer is 1.4142. We can expressed it such that<br />
<div align="center">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPljERvsGhWj2kiVUPzm4C1M0rcEU3GGkg-8AzSYa7BO_RSWnt-ZqUXC7NudECI4j9dCrAtSJTkYP3N0Hm0PgK8-GTjz4a87imSlZx_TLomrqrQlytpIctxzjDT2CcDdpZDMNMaMZbIlY/s1600-h/clip_image002%25255B6%25255D%25255B2%25255D.gif"><img alt="clip_image002[6]" border="0" height="17" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5pZL2U-tBcMengYVu_LVUekEfdWZo6iY0u-hQWrQoDwCaI5Q83PGZNruSDG5ulkCMxV8ZSkNIrc1V9kNumNGiYpvelLGW99lL2GaMHPtN3h3O8GA4OK5lkMrY325CgLqWK86tpAp1bsQ/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="clip_image002[6]" width="238" /></a></div>
If we expressed it to algebra expression as following. <br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO1DJI0mV_jIKK1YmQa0sYyr87Z7LosMDFuohzEPtaqj6Mbx32inExHauCSaV2UfkZ9rbcpDfo7giTeMjD8z-1PTp9cLCa3l1JqcqedxyzPPAGoXKhdqasV5qgR4JRqyrviUGnd4tOZJk/s1600-h/clip_image002%25255B8%25255D%25255B2%25255D.gif"><img alt="clip_image002[8]" border="0" height="27" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEif5gPg4nN0RhfIkr-kPnYtvz8O5zJyeHCDWLXM-iQgPGM4j0CEiYM0Tpzf-aWEufpvrtVD4L0bZu1Ysh-Dfgj-jVVbbHz61os8tV0IRMmiooh4yrM-qZTKPPf2UQHz6nl22z0hmA0acBc/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="clip_image002[8]" width="190" /></a><br />
Suppose a is continue for n times, it can be expressed as following<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj60fF5JG3v4IZiff47xRFsHwwtDpmNTaRDVoh9fWN3ysxr4Ht_zbiK5EU90JdZBdKC0JHVwTTkWYNyY72tsAvumQF0iy5F-e_g7UZE0bs_4CSBhDlv5g7mdOUka0dcxXe1QtUXS3m4wD4/s1600-h/ex3%25255B3%25255D.png"><img alt="ex3" border="0" height="21" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGIDg7xM76Sg2XC6Cfdp4zMVv2g1VTdVqQgGI5TI5taORtz41P73YuzanUtrS2cqgise9leIYek_ZP79oJShWqc0_c13aLFli2uJTWCA5nvpdvWl23UdVoE-g8sq5iyBAUOqgj0pMcaaY/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="ex3" width="240" /></a> Why I am treating it continue as n times but not 5 times? Because we cannot ensure that all positive number will give us a 5 digits answer after square root it. So we have to suppose there are n digits after square root. <br />
Right-hand-side term can be expanded as following<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaiB2xdsz7cdOUJ0Ba013s9vHHYyxL_uU5hMmzybQJwtaNrLMuLXaCd9RJtrdkKHJ9oDpmJD3xn0Wj2SdI60tJkuyaj2y2KKqgwONmyXZLlP1HGFYPxNXoPSKKmnoGo2scTWzeKKi3wis/s1600-h/ex4%25255B9%25255D.png"><img alt="ex4" border="0" height="193" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHXeYt57VUOxOooq7AnEGvGewGqRvLL7qM-m5LEfpaOZBU5_Q0yy_4rp17pRe7maDROj-JAik7L8wdunlt2oLC7X530SBXdc1Uhoyhp5n6TEfCtmKgfffczfGQuFq5dR2nFQSNVtxOjKw/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="ex4" width="503" /></a><br />
From the equation above, we can understand square root equation can be solved by following equation. <br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXL-fVp6ZDdSmyBUJEQQZtjHnN3ZtOqI7px3s5H0dwbOzCQyZPAEhwvuvdGaV0cuik-_HieD0YfpDjvom1Qa3cByUKYAOdYUGJTT7nCH4SwhRd3kODzRexjtLWXyMhi5F5Q2q9TvOFUIo/s1600-h/ex5%25255B3%25255D.png"><img alt="ex5" border="0" height="72" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwpzCKPAcZnyysdNWFXjDE707JbCxroLNnYCllSbvxUImYyZHC28APST56hD0opE7bxmVU_feqR36ADua8GtRg8oyBsovQJhDA2CDWaziF3UqBtfgavDsexdePwypwg6BCZj5KMJyKo3o/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="ex5" width="161" /></a>The most important rule of this equation is we pick a number from 1,2,3,4,5,6,7,8,9 and multiple it with 10^m, where m = Integer (exp. 3,2,1,0,-1,-2,-3,...) and substitute into <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiobX65oFTIFNh4BS2NqeBm3oy143ABURl_n5nK8uWelygtJf_xLkSofGC3ch1wO72nUqG3h_aoZrodt09RRE8PwOjGeMqaoWmvoHwSqzpngu7KgRj36-5WMLL2Wj_PFzE_YiOBnuGtMpg/s1600-h/clip_image002%25255B12%25255D%25255B2%25255D.gif"><img alt="clip_image002[12]" border="0" height="16" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg873v__9vYYgqNtONjCM5bUs6rkzOFWlaZyYoohBgL8afM7lPhgLOXuxS4sSTXD-GeTvjYOVW8ROqaWWJcO8d4MBmEV0ULboK6iePy9BWcAC1C0Th1jq13199_Q2a9S7K1i_JX7H4I4Yg/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="clip_image002[12]" width="14" /></a>that must be smaller then the number we are going to minus or equal to it. It’s the most difficult part on square-root equation so lets understand it by solving square root of 2. <br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4OFbE7GK2w4_smbHqr-t5Ls1bSBQLIaDSeryhkfdxzda1S7I2wRhUnXw3PfWvGVNVBvtGky9hnPzpIwHShMnAK9z8jyBPftmy8cWZ-MUFwesw472LSCuBdd1NyGCnJP2lGYu76XLDjr8/s1600/eq6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="255" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4OFbE7GK2w4_smbHqr-t5Ls1bSBQLIaDSeryhkfdxzda1S7I2wRhUnXw3PfWvGVNVBvtGky9hnPzpIwHShMnAK9z8jyBPftmy8cWZ-MUFwesw472LSCuBdd1NyGCnJP2lGYu76XLDjr8/s400/eq6.png" width="400" /></a></div>
<br />
So, from the solution above, we understand that <br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEit2ytSCUsU2adj5sNsFGvXnmaMiN2shHGzg7Vlf1ekyuU5IA_HdJsShbDVPCriZoHAebQ3Vm1GNlvOAotKlGM76aHf2T_GVhqQbuO6NFDvJZyumpu6cEze_LW6kwJWdE9JUS-dI6VVCd8/s1600-h/eq8%25255B7%25255D.png"><img alt="eq8" border="0" height="110" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsz-eXqYPeoYTxMG69lsT59EU63mbDtMyq1urgdM1KFzRsIXqGea3T9g6vpReMiTYMJzdPiUohiIYvyOiEgBaGmG-iunmQDbyrJJOkDoxYeNwOqN0feml4jLFDebcf-pUd87e4SwSvBqY/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline;" title="eq8" width="123" /></a><br />
And we sum up all the value, we get the answer 1.414 approximate to 1.4142<br />
<h3>
Algorithm</h3>
Based on the method above, what kind of works should we applied on algorithm as following<br />
<ul>
<li>Create equation </li>
<li>Create function of power of ten </li>
<li>Find suitable number that bigger then 0 for variable <strong>a</strong> from loop </li>
<li>Find suitable number that smaller then 0 but positive for variable <strong>a</strong> from loop </li>
<li>Sum all variable <strong>a </strong>and return it as double </li>
</ul>
First, we are going to create the equation as we mentioned above<br />
<img alt="ex5" border="0" height="72" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwpzCKPAcZnyysdNWFXjDE707JbCxroLNnYCllSbvxUImYyZHC28APST56hD0opE7bxmVU_feqR36ADua8GtRg8oyBsovQJhDA2CDWaziF3UqBtfgavDsexdePwypwg6BCZj5KMJyKo3o/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="ex5" width="161" /><br />
We can expressed it as code as following<br />
<pre style="background-color: #fbfbfb; border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; border-right: #cecece 1px solid; border-top: #cecece 1px solid; min-height: 40px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 650px;"><pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;">(( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i))</pre>
</pre>
<br />
<br />
where <strong>rst</strong> and <strong>j</strong> is declared as double and <strong>i</strong> is int. powerOfTen is a function that return a value of i times multiple of 10. <br />
<br />
It’s code of function powerOfTen<br />
<pre style="background-color: #fbfbfb; border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; border-right: #cecece 1px solid; border-top: #cecece 1px solid; min-height: 40px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 400px;"><pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 1: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 2: <span style="color: blue;">double</span> powerOfTen(<span style="color: blue;">int</span> num){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 3: <span style="color: blue;">double</span> rst = 1.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 4: <span style="color: blue;">if</span>(num >= 0){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 5: <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 0; i < num ; i++){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 6: rst *= 10.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 7: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 8: }<span style="color: blue;">else</span>{</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 9: <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 0; i < (0 - num ); i++){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 10: rst *= 0.1;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 11: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 12: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 13: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 14: <span style="color: blue;">return</span> rst;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 15: }</pre>
</pre>
<a href="http://11011.net/software/vspaste"></a><br />
We have to judge that variable <strong>a </strong>must be smaller then upper number. It’s how it looks like<br />
<pre style="background-color: #fbfbfb; border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; border-right: #cecece 1px solid; border-top: #cecece 1px solid; min-height: 40px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 650px;"><pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"><span style="color: blue;">if</span>(z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
</pre>
<br />
where <strong>z</strong> is declared as double and treat as upper number.<br />
<br />
There are two loops, one loop for finding the suitable number from 10000,1000,100,10… and another one loop for find suitable number from 1,2,3,4,5… <br />
<pre style="background-color: #fbfbfb; border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; border-right: #cecece 1px solid; border-top: #cecece 1px solid; min-height: 40px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 400px;"><pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 1: <span style="color: blue;">for</span>(i = max ; i > 0 ; i--){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 2: // value must be bigger <span style="color: blue;">then</span> 0</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 3: <span style="color: blue;">if</span>(z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 4: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 5: <span style="color: blue;">while</span>( z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 6: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 7: j++;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 8: <span style="color: blue;">if</span>(j >= 10) break;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 9: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 10: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 11: j--; //correct the extra value by minus one <span style="color: blue;">to</span> j</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 12: z -= (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)); //find value <span style="color: blue;">of</span> z</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 13: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 14: rst += j * powerOfTen(i); // find sum <span style="color: blue;">of</span> a</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 15: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 16: j = 1.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 17: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 18: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 19: } </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 20: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 21: }</pre>
</pre>
<br />
<br />
<br />
Same loop as above but this times we are going to find the decimal number.<br />
<pre style="background-color: #fbfbfb; border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; border-right: #cecece 1px solid; border-top: #cecece 1px solid; min-height: 40px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 400px;"><pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 1: <span style="color: blue;">for</span>(i = 0 ; i >= 0 - max ; i--){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 2: <span style="color: blue;">if</span>(z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 3: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 4: <span style="color: blue;">while</span>( z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 5: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 6: j++;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 7: <span style="color: blue;">if</span>(j >= 10) break; </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 8: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 9: j--;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 10: z -= (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)); //find value <span style="color: blue;">of</span> z</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 11: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 12: rst += j * powerOfTen(i); // find sum <span style="color: blue;">of</span> a </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 13: j = 1.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 14: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 15: }</pre>
</pre>
<br />
This is how the whole squareRoot algorithm looks like<br />
<pre style="background-color: #fbfbfb; border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; border-right: #cecece 1px solid; border-top: #cecece 1px solid; min-height: 40px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 400px;"><pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 1: <span style="color: blue;">double</span> squareRoot(<span style="color: blue;">double</span> a)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 2: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 3: <span style="color: green;">/*</span></pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 4: find more detail of this method on wiki methods_of_computing_square_roots</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 5: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 6: *** Babylonian method cannot get exact zero but approximately value of the square_root</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 7: */</pre>
</pre>
<br />
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 8: <span style="color: blue;">double</span> z = a; </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 9: <span style="color: blue;">double</span> rst = 0.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 10: int max = 8; // <span style="color: blue;">to</span> define maximum digit </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 11: int i;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 12: <span style="color: blue;">double</span> j = 1.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 13: <span style="color: blue;">for</span>(i = max ; i > 0 ; i--){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 14: // value must be bigger <span style="color: blue;">then</span> 0</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 15: <span style="color: blue;">if</span>(z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 16: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 17: <span style="color: blue;">while</span>( z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 18: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 19: j++;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 20: <span style="color: blue;">if</span>(j >= 10) break;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 21: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 22: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 23: j--; //correct the extra value by minus one <span style="color: blue;">to</span> j</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 24: z -= (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)); //find value <span style="color: blue;">of</span> z</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 25: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 26: rst += j * powerOfTen(i); // find sum <span style="color: blue;">of</span> a</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 27: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 28: j = 1.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 29: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 30: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 31: } </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 32: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 33: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 34: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 35: <span style="color: blue;">for</span>(i = 0 ; i >= 0 - max ; i--){</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 36: <span style="color: blue;">if</span>(z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 37: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 38: <span style="color: blue;">while</span>( z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 39: {</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 40: j++;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 41: <span style="color: blue;">if</span>(j >= 10) break; </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 42: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 43: j--;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 44: z -= (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)); //find value <span style="color: blue;">of</span> z</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 45: </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 46: rst += j * powerOfTen(i); // find sum <span style="color: blue;">of</span> a </pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 47: j = 1.0;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 48: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 49: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 50: // find the number <span style="color: blue;">on</span> <span style="color: blue;">each</span> digit</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 51: <span style="color: blue;">return</span> rst;</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 52: }</pre>
<pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 11px; margin: 0em; width: 100%;"> 53: </pre>
<br />
<h3>
Reference</h3>
<br />
I am referring to this <a href="http://en.wikipedia.org/wiki/Methods_of_computing_square_roots">wiki website</a>. For more details, please refer on this website. Thank you.Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com1tag:blogger.com,1999:blog-4365670108204289203.post-69639160492203782432013-02-23T20:50:00.001+09:002013-02-25T03:00:17.306+09:00Simple Sync Apps<h3>
About Download</h3>
<div>
<a href="https://www.dropbox.com/s/1e5hqab4ahn0cxf/SyncApps.zip">Click here to download</a>. The zip file contains 2 projects and 1 php file.</div>
<h3>
<a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=EdisonHeng" rel="tag" style="display: none;">CodeProject</a>
<a href="http://edisontus.blogspot.com/"><span class="postTitle" id="d8799def-6a5e-4040-a642-d5b2e757b849" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span></a>Introduction</h3>
I begin to learn programming is still half of a years ago and I begin to write this application is about one months ago. Begin from 0 knowledge to network and server. So I couldn’t make sure that I have wrote a very perfect program. But, this application do exactly what I need for that is synchronizing between android and desktop. <br />
It is how it’s look like. Check it out with the following video.<br />
<br />
<div class="wlWriterEditableSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:60a95ea6-3fe7-4bce-b894-78c0f642b148" style="display: inline; float: none; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div id="5bb8ca5a-e12f-42b7-8a55-19e86d7a44d2" style="display: inline; margin: 0px; padding: 0px;">
<div>
<a href="http://www.youtube.com/watch?v=LMIeynf6NUg" target="_new"><img alt="" galleryimg="no" onload="var downlevelDiv = document.getElementById('5bb8ca5a-e12f-42b7-8a55-19e86d7a44d2'); downlevelDiv.innerHTML = "<div><object width=\"425\" height=\"355\"><param name=\"movie\" value=\"http://www.youtube.com/v/LMIeynf6NUg&hl=en\"><\/param><embed src=\"http://www.youtube.com/v/LMIeynf6NUg&hl=en\" type=\"application/x-shockwave-flash\" width=\"425\" height=\"355\"><\/embed><\/object><\/div>";" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-HazrazydkAh3XatDBQ8vud0Pn3d2DQAvXPUH9QvmC0sEpBxJMxFeLAyhiKkxDt2bT9qiyBzCbqn1u-eqPjEQDPJ-YQzvFBLsfZnlW3g1ePAqsmjFqxNKVvFULVUN8ogxQLFbyQWpZqU/?imgmax=800" style="border-style: none;" /></a></div>
</div>
</div>
<br />
<br />
<h3>
How it's work</h3>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXE0ft58c498_YN4Q3RMFoUFbLtwyrmdxIOEhqyJlGbuN8KTIFcwBB_WsksatCJTzole91jisWVbFNVq8T48A_PPs1QsiM9u7wjFu3NOePIxo27Za1ahT082RHQQ8EvYYu2PmPPkBD6Kw/s1600-h/sync_pic1.png"><img alt="sync_pic1" border="0" height="104" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMDUvmMeCqm19O9DJvHM4F6dLjbvPQVYyek2IOsAbJA6d3L6OJHV0Haz8-6eEounYUhYeKTbjGa6EjfXiB3dltTmIJIiQmqW29VM318_RofVKt_QHHmyTHcmOEIUsSIqyef3HKPoIRqQg/?imgmax=800" style="border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="sync_pic1" width="411" /></a>Picture above shows how the data is transferred. Database server is a bridge to allow data move from android to desktop and move from desktop to android. That’s all, simple right!<br />
Of course that’s other way such as cloud computing or others. Based on my knowledge, this is easier way I know. <br />
<h3>
Develop Environment</h3>
My develop environment is Java JDK version 1.7.0_13, Eclipse SDK (Juno version 4.2.1), XAMPP 1.8.1, MySQL Connector/J 5.1.23, android 4.2 <br />
Because every software I used to develop this application is open-source, I spent a zero cost on this project.<br />
<h3>
Program Flow Chart </h3>
<table border="0" cellpadding="2" cellspacing="0" style="width: 517px;"> <tbody>
<tr> <td valign="top" width="200"><br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMdoucWk4J2RNEdh2GMBrURm1svUkU9aoJnMBjHslifv8ofsYdRVI4AlJHoQ0EtDbwhXDdfYvKTBwBEjmHnRozBt4E2cbE2JCVBAnAMwl2WHqtbavfj1XVk5Y8vUsOCqCzWEsUraJLp68/s1600-h/sync_pic227.png"><img alt="sync_pic2" border="0" height="394" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGC0WwkV0_fRQBY9mer0_TDMiIpr2cFqsfX9B1Cr-pRzmmuktCuNR09OH8_AAUkDq4Q1bA1hGkZwvzBdEgndALfmQzgJ-siPNiNm0OtAqdmS9-3XcAT3Gd3Fq_fd3m92zRPWzWCdpn6So/?imgmax=800" style="border-width: 0px; display: inline;" title="sync_pic2" width="310" /></a></td> <td valign="top" width="315"><br />
Flow chart beside shows how data is passing through between android and desktop. <br />
When user press a button on UI thread to ask android read what desktop have written, android will begin access to MySQL server.<br />
In server side, 2 php files ( update database and read database php files) is created and located at htdocs. After android will success to access into server, then android will begin searching the php files requested (either update or read php files) through HTTP protocol. </td></tr>
</tbody></table>
Request ask for read database php file to work. When read database php file is executed, php file will begin it’s work by log in to database and retrieve the data requested that what desktop have written into database. After, php get the data it wants from database, it will pass it again to android and android get the data and put it on UI thread to show user what data have retrieved from database. <br />
If the desktop ask for what android have written, it (desktop) will do exactly same things as what android did. desktop will going into database adapter request for data it wants. Then database adapter will log in to database server through Java-MySQL connector and search for what android have written into database. After database adapter get what their want, it will pass the data to desktop and desktop will put it on screen to show user what data it ( desktop ) got.<br />
<br />
<h3>
Source Code</h3>
<div>
<ul>
<li>mysqlAndroid is an android project. Inside this project contains three java source code. (JsonParser, JsonPost and MainThread)</li>
<li>JsonParser.java contains HttpGet method. There is no input data to url, only retrieve data from php file.</li>
<li>JsonPost.java contains HttpPost method. This method allow data post into php file.</li>
<li>MainThread.java is a UI thread work that called the button or textView method allow user talk with machine.</li>
</ul>
<div>
<br /></div>
</div>
<div>
<ul>
<li>mysqlDesktop is an java Swing project. Inside this project contains three java source code and one external jar that is DatabaseAdapter.java, jConnector.java and MainFrame.java and <a href="http://dev.mysql.com/downloads/connector/j/">mysql-java Connector</a>.</li>
<li>jConnector.java contains the source code that allow java project connect to MySQL database.</li>
<li>DatabaseAdapter.java used to make database work getting easier to carry out.</li>
<li>MainFrame.java is a source code allow user to talk with device through window form</li>
</ul>
</div>
Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0tag:blogger.com,1999:blog-4365670108204289203.post-3773072707356201092013-01-03T01:24:00.002+09:002014-08-21T22:38:09.331+09:00Simple notepad for android I have migrate my blog to new blog. Click the link below to redirect.<br />
<br />
<a href="http://edisonthk.wordpress.com/2013/01/02/simple-notepad-for-android/">http://edisonthk.wordpress.com/2013/01/02/simple-notepad-for-android/</a>Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com1tag:blogger.com,1999:blog-4365670108204289203.post-80199956845184318702012-12-17T21:58:00.000+09:002012-12-18T09:14:57.183+09:00How to create a simple list menu for android 4.2<div style="text-align: center;">
<div style="text-align: left;">
I just started blogging on these days so I will be very appreciate if there is feedback comment on it.</div>
<br />
<div style="text-align: left;">
My develop environment is jdk1.7 , android 4.2, and eclipse 4.2.1</div>
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
This tutorial describe how to create a simple menu using default android layout and id .It is how is works. When you click on the list, toast will pop up on the screen. If it is difficult to image, figure below shows how it looks like.</div>
<div style="text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl9-HIw2dQne8Gu_VBG1d5iSCaCn3vnopBrbf43Al3yi6BU2bvbcc2cuj3c-ynTnpIO2Np-3tjPhoyNdmnidkaeAKw4fnZN-ciI4DjMLueNZ2Wo0TAUkxQfBgTZqDwF2G3V8HMUeAyNpw/s1600/listadapter.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl9-HIw2dQne8Gu_VBG1d5iSCaCn3vnopBrbf43Al3yi6BU2bvbcc2cuj3c-ynTnpIO2Np-3tjPhoyNdmnidkaeAKw4fnZN-ciI4DjMLueNZ2Wo0TAUkxQfBgTZqDwF2G3V8HMUeAyNpw/s320/listadapter.png" width="215" /></a></div>
<div style="text-align: left;">
<h3>
1. Create listView layout</h3>
Following source code shows how the layout file looks like.<br />
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #507090;"><?xml version="1.0" encoding="utf-8"?></span>
<span style="color: #007000;"><LinearLayout</span> <span style="color: #0000c0;">xmlns:android=</span><span style="background-color: #fff0f0;">"http://schemas.android.com/apk/res/android"</span>
<span style="color: #0000c0;">android:layout_width=</span><span style="background-color: #fff0f0;">"match_parent"</span>
<span style="color: #0000c0;">android:layout_height=</span><span style="background-color: #fff0f0;">"match_parent"</span>
<span style="color: #0000c0;">android:orientation=</span><span style="background-color: #fff0f0;">"vertical"</span> <span style="color: #007000;">></span>
<span style="color: #007000;"><ListView</span>
<span style="color: #0000c0;">android:layout_width=</span><span style="background-color: #fff0f0;">"match_parent"</span>
<span style="color: #0000c0;">android:layout_height=</span><span style="background-color: #fff0f0;">"wrap_content"</span> <span style="color: #007000;">></span>
<span style="color: #007000;"></ListView></span>
<span style="color: #007000;"></LinearLayout></span>
</pre>
</div>
<div style="text-align: left;">
<br />
Create a new android application project and add the <span style="color: #38761d;">listView </span>in to it. Note that don't miss any line written above. If you miss it, maybe there is error occur.<br />
<br />
Now, you got the listView on your layout but you couldn't see anything as you don't have set any data on to it. So, what we are going to do next is to set data on to listView layout.<br />
<br />
<h3>
2. Set Array Adapter on to listView layout</h3>
Enter the following source code to your <b><span style="color: #990000;">MainActivity </span></b>class and extends ListActivity class<br />
Note that don't extends Activity class.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: #b00060; font-weight: bold;">MainActivity</span> <span style="color: green; font-weight: bold;">extends</span> ListActivity <span style="color: #303030;">{</span>
<span style="color: #505050; font-weight: bold;">@Override</span>
<span style="color: green; font-weight: bold;">protected</span> <span style="color: #303090; font-weight: bold;">void</span> <span style="color: #0060b0; font-weight: bold;">onCreate</span><span style="color: #303030;">(</span>Bundle savedInstanceState<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">super</span><span style="color: #303030;">.</span><span style="color: #0000c0;">onCreate</span><span style="color: #303030;">(</span>savedInstanceState<span style="color: #303030;">);</span>
String<span style="color: #303030;">[]</span> values <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">new</span> String<span style="color: #303030;">[]</span> <span style="color: #303030;">{</span> <span style="background-color: #fff0f0;">"Android"</span><span style="color: #303030;">,</span> <span style="background-color: #fff0f0;">"iPhone"</span><span style="color: #303030;">,</span> <span style="background-color: #fff0f0;">"WindowsMobile"</span><span style="color: #303030;">};</span>
<span style="color: grey;">// First parameter - Context</span>
<span style="color: grey;">// Second parameter - Layout for the row</span>
<span style="color: grey;">// Third parameter - ID of the TextView to which the data is written</span>
<span style="color: grey;">// Forth - the Array of data</span>
ArrayAdapter<span style="color: #303030;"><</span>String<span style="color: #303030;">></span> adapter <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">new</span> ArrayAdapter<span style="color: #303030;"><</span>String<span style="color: #303030;">>(</span><span style="color: green; font-weight: bold;">this</span><span style="color: #303030;">,</span>
android<span style="color: #303030;">.</span><span style="color: #0000c0;">R</span><span style="color: #303030;">.</span><span style="color: #0000c0;">layout</span><span style="color: #303030;">.</span><span style="color: #0000c0;">simple_list_item_1</span><span style="color: #303030;">,</span> android<span style="color: #303030;">.</span><span style="color: #0000c0;">R</span><span style="color: #303030;">.</span><span style="color: #0000c0;">id</span><span style="color: #303030;">.</span><span style="color: #0000c0;">text1</span><span style="color: #303030;">,</span> values<span style="color: #303030;">);</span>
<span style="color: grey;">// Assign adapter to ListView</span>
setListAdapter<span style="color: #303030;">(</span>adapter<span style="color: #303030;">);</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span>
</pre>
</div>
<br />
By extending ListActivity class, you are able to adapt the String[] to listView via setListAdapter() method.<br />
<br />
Android platform provide defaults layout for rows. Code above use is one of example of defaults layout, <span style="color: blue;">android.R.layout.simple_list_item_1</span> contains default <span style="color: blue;">id android.R.id.text1. </span>Of course you can create your own rows layout but on this tutorial, we are teaching how to create adapter by using Android defaults layout.<br />
<br />
Now, you got "Android","iPhone","WindowsMobile" on your list. What we got to do next is to make the list function when we click on it.<br />
<br />
<h3>
3. Create onListItemClick</h3>
By adding following new method on to MainActivity class. When you click on the list item, toast will pop up.<br />
<br />
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #505050; font-weight: bold;">@Override</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: #303090; font-weight: bold;">void</span> <span style="color: #0060b0; font-weight: bold;">onListItemClick</span><span style="color: #303030;">(</span>ListView l<span style="color: #303030;">,</span>View v<span style="color: #303030;">,</span><span style="color: #303090; font-weight: bold;">int</span> position<span style="color: #303030;">,</span><span style="color: #303090; font-weight: bold;">long</span> id<span style="color: #303030;">){</span>
String item <span style="color: #303030;">=</span> <span style="color: #303030;">(</span>String<span style="color: #303030;">)</span>getListAdapter<span style="color: #303030;">().</span><span style="color: #0000c0;">getItem</span><span style="color: #303030;">(</span>position<span style="color: #303030;">);</span>
Toast<span style="color: #303030;">.</span><span style="color: #0000c0;">makeText</span><span style="color: #303030;">(</span>getApplicationContext<span style="color: #303030;">(),</span>
item<span style="color: #303030;">+</span><span style="background-color: #fff0f0;">""</span><span style="color: #303030;">,</span> Toast<span style="color: #303030;">.</span><span style="color: #0000c0;">LENGTH_SHORT</span><span style="color: #303030;">)</span>
<span style="color: #303030;">.</span><span style="color: #0000c0;">show</span><span style="color: #303030;">();</span>
<span style="color: #303030;">}</span>
</pre>
</div>
<div style="text-align: left;">
<br />
String item is to get the string what you clicked. Toast.makeText is to show what your get on String item.<br />
<h3>
4. Done !</h3>
<div>
Now, debug it and you will get what I said at the first.</div>
<div>
<br /></div>
<div>
Here's the complete MainActivity class look like.</div>
<div>
<br /></div>
</div>
<div style="background: #ffffff; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">android.os.Bundle</span><span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">android.app.ListActivity</span><span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">android.view.View</span><span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">android.widget.ArrayAdapter</span><span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">android.widget.ListView</span><span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">android.widget.Toast</span><span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: #b00060; font-weight: bold;">MainActivity</span> <span style="color: green; font-weight: bold;">extends</span> ListActivity <span style="color: #303030;">{</span>
<span style="color: #505050; font-weight: bold;">@Override</span>
<span style="color: green; font-weight: bold;">protected</span> <span style="color: #303090; font-weight: bold;">void</span> <span style="color: #0060b0; font-weight: bold;">onCreate</span><span style="color: #303030;">(</span>Bundle savedInstanceState<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">super</span><span style="color: #303030;">.</span><span style="color: #0000c0;">onCreate</span><span style="color: #303030;">(</span>savedInstanceState<span style="color: #303030;">);</span>
String<span style="color: #303030;">[]</span> values <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">new</span> String<span style="color: #303030;">[]</span> <span style="color: #303030;">{</span> <span style="background-color: #fff0f0;">"Android"</span><span style="color: #303030;">,</span> <span style="background-color: #fff0f0;">"iPhone"</span><span style="color: #303030;">,</span> <span style="background-color: #fff0f0;">"WindowsMobile"</span><span style="color: #303030;">};</span>
<span style="color: grey;">// First parameter - Context</span>
<span style="color: grey;">// Second parameter - Layout for the row</span>
<span style="color: grey;">// Third parameter - ID of the TextView to which the data is written</span>
<span style="color: grey;">// Forth - the Array of data</span>
ArrayAdapter<span style="color: #303030;"><</span>String<span style="color: #303030;">></span> adapter <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">new</span> ArrayAdapter<span style="color: #303030;"><</span>String<span style="color: #303030;">>(</span><span style="color: green; font-weight: bold;">this</span><span style="color: #303030;">,</span>
android<span style="color: #303030;">.</span><span style="color: #0000c0;">R</span><span style="color: #303030;">.</span><span style="color: #0000c0;">layout</span><span style="color: #303030;">.</span><span style="color: #0000c0;">simple_list_item_1</span><span style="color: #303030;">,</span> android<span style="color: #303030;">.</span><span style="color: #0000c0;">R</span><span style="color: #303030;">.</span><span style="color: #0000c0;">id</span><span style="color: #303030;">.</span><span style="color: #0000c0;">text1</span><span style="color: #303030;">,</span> values<span style="color: #303030;">);</span>
<span style="color: grey;">// Assign adapter to ListView</span>
setListAdapter<span style="color: #303030;">(</span>adapter<span style="color: #303030;">);</span>
<span style="color: #303030;">}</span>
<span style="color: #505050; font-weight: bold;">@Override</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: #303090; font-weight: bold;">void</span> <span style="color: #0060b0; font-weight: bold;">onListItemClick</span><span style="color: #303030;">(</span>ListView l<span style="color: #303030;">,</span>View v<span style="color: #303030;">,</span><span style="color: #303090; font-weight: bold;">int</span> position<span style="color: #303030;">,</span><span style="color: #303090; font-weight: bold;">long</span> id<span style="color: #303030;">){</span>
String item <span style="color: #303030;">=</span> <span style="color: #303030;">(</span>String<span style="color: #303030;">)</span>getListAdapter<span style="color: #303030;">().</span><span style="color: #0000c0;">getItem</span><span style="color: #303030;">(</span>position<span style="color: #303030;">);</span>
Toast<span style="color: #303030;">.</span><span style="color: #0000c0;">makeText</span><span style="color: #303030;">(</span>getApplicationContext<span style="color: #303030;">(),</span>
item<span style="color: #303030;">+</span><span style="background-color: #fff0f0;">""</span><span style="color: #303030;">,</span> Toast<span style="color: #303030;">.</span><span style="color: #0000c0;">LENGTH_SHORT</span><span style="color: #303030;">)</span>
<span style="color: #303030;">.</span><span style="color: #0000c0;">show</span><span style="color: #303030;">();</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span>
</pre>
</div>
<div style="text-align: left;">
<br />
<br />
<br />
<br />
<div style="text-align: center;">
<br /></div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/18125797009643745575noreply@blogger.com0