2012年12月17日月曜日

How to create a simple list menu for android 4.2

I just started blogging on these days so I will be very appreciate if there is feedback comment on it.

My develop environment is jdk1.7 , android 4.2, and eclipse 4.2.1

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.

1. Create listView layout

Following source code shows how the layout file looks like.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ListView      
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Create a new android application project and add the listView in to it. Note that don't miss any line written above. If you miss it, maybe there is error occur.

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.

2. Set Array Adapter on to listView layout

Enter the following source code to your MainActivity class and extends ListActivity class
Note that don't extends Activity class.


public class MainActivity extends ListActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 
  String[] values = new String[] { "Android", "iPhone", "WindowsMobile"};
  // First parameter - Context
  // Second parameter - Layout for the row
  // Third parameter - ID of the TextView to which the data is written
  // Forth - the Array of data
  
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, android.R.id.text1, values);  
  // Assign adapter to ListView
  setListAdapter(adapter);  
 } 
}

By extending ListActivity class, you are able to adapt the String[] to listView via setListAdapter() method.

Android platform provide defaults layout for rows. Code above use is one of  example of defaults layout, android.R.layout.simple_list_item_1 contains default id android.R.id.text1. 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.

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.

3. Create onListItemClick

By adding following new method on to MainActivity class. When you click on the list item, toast will pop up.



@Override
public void onListItemClick(ListView l,View v,int position,long id){
 String item = (String)getListAdapter().getItem(position);
 Toast.makeText(getApplicationContext(),
        item+"", Toast.LENGTH_SHORT)
        .show();
  
}

String item is to get the string what you clicked. Toast.makeText is to show what your get on String item.

4. Done !

Now, debug it and you will get what I said at the first.

Here's the complete MainActivity class look like.

import android.os.Bundle; 
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 
  String[] values = new String[] { "Android", "iPhone", "WindowsMobile"};
  // First parameter - Context
  // Second parameter - Layout for the row
  // Third parameter - ID of the TextView to which the data is written
  // Forth - the Array of data
  
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, android.R.id.text1, values);  
  // Assign adapter to ListView
  setListAdapter(adapter);  
 }
 
 @Override
 public void onListItemClick(ListView l,View v,int position,long id){
  String item = (String)getListAdapter().getItem(position);
  Toast.makeText(getApplicationContext(),
         item+"", Toast.LENGTH_SHORT)
         .show();
  
 }
}






2012年12月15日土曜日

3D Printers

It doesn't relate to my projects. I found this video and it sound very interesting. I hope your guys enjoy it. It is talking about print a 3D object out from the 3D printers. Sound cool right ^^


Google Drive Sharing File Download

Here's some guide to show your how to download file from Google drive file sharing

First, click the link that you want to download.


Click the picture to enlarge it

 Second, click the file on upper-left side
Click the picture to enlarge it

Last, click Download on the file menu, and it will start download.

Quick Tips: Shortcut key for download is Ctrl+S, as you can see on the beside of Download


Thank you for reading.

Box Robot


This is how the robot look like


Simple terminal I created. The program run in form of console, it might make you misunderstood that it is easy to be created but in fact, DIFFICULT!!!


Its is how its work, when I input the command something like 'j' from keyboard. Robot will rotate
When I input 'k', robot will move forward.
Simple

Its the bluetooth - Serial wireless module I used. 
It's very simple to use, just wipe up your wire and enter the pairing code only.
Simple

For those who are interest with my project, Here's the link for download
It includes 3 source file of mcu and terminal, it's also contain the debug file of terminal.
If you are looking for the software that only doing a simple serial communication, you can try to use the one i created. Its is included inside the file named "SimpleTerminal.exe",
just click it then enter your output port and communication is started.
Simple

Its my first post, any feedback is welcome (^^)



Hi! I'm new to here

I'm not only new to blogger, and also new to blog.

Well, I have post a few post on my japanese blog, but everything was just started on these days.

Let's me introduce myself first, I am doing foreign study at Japan right now. My major is electronic, electric and programming. It sound like I'm doing many study XD Well, mainly, I am doing programming with microcontroller and winAPI as well as android.

I am ready to post something relate to my works like microcontroller, android, and of course winAPI programming on my blog.

Hope it do help the others developer who ready to start learning programming and welcome the experienced programmer to point out the unfriendly program.

At the last, I am not really good at my english so forgive me if there is any strange grammar or something that making you confuse.

Thanks for reading.