Fragment in Android is very useful for responsive layout design. In the
post, I will give you a full detailed example of using FramentActivity
in android application development.
We will produce a demo application which have two different layouts in different device.
Tuesday, December 24, 2013
Monday, December 23, 2013
[Android] Session 2 - Application Fundamentals & Activity
Application Fundamentals & Activity
Việc hiểu được các thành phần (component) tạo nên một ứng dụng Android là rất cần thiết cho việc lập trình. Các thành phần này được chia làm 6 loại bao gồm:
1.Activity: hiểu một cách đơn giản thì Activity là nền của 1 ứng dụng. Khi khởi động 1 ứng dụng Android nào đó thì bao giờ cũng có 1 main Activity được gọi, hiển thị màn hình giao diện của ứng dụng cho phép người dùng tương tác.
Thursday, May 16, 2013
[Android Nâng Cao] Sử Dụng ViewPager Cho Chuyển Đổi View
Xin chào các bạn, hôm nay mình sẽ hướng dẫn các bạn cách sử dụng
ViewPager cho việc chuyển Activity đồng thời khi vuốt tay ( giống như
khi vào Play Store các bạn vuốt tay qua trái phải để xem Top Paid apps
> Top Free apps....).
I. Giới Thiệu ViewPager
Theo android.com, thì ViewPager là một trình quản lý Layout cho phép người dùng vuốt tay qua trái hay qua bên phải để chuyển sang nội dung của trang khác một cách đồng thời. Điểm khác nhau của ViewPager và cách sử dụng Gesture đó là, gesture sẽ chỉ thực hiện khi người dùng thực hiện xong thao tác vuốt tay, trong khi ViewPager thực hiện chuyển màn hình đồng thời với cử chỉ tay người dùng.
II. Code Now
I. Giới Thiệu ViewPager
Theo android.com, thì ViewPager là một trình quản lý Layout cho phép người dùng vuốt tay qua trái hay qua bên phải để chuyển sang nội dung của trang khác một cách đồng thời. Điểm khác nhau của ViewPager và cách sử dụng Gesture đó là, gesture sẽ chỉ thực hiện khi người dùng thực hiện xong thao tác vuốt tay, trong khi ViewPager thực hiện chuyển màn hình đồng thời với cử chỉ tay người dùng.
II. Code Now
Tuesday, April 23, 2013
Nền tảng Android – bức tranh nhìn từ trên cao
Vài nét về quá trình ra đời
Ngày nay, điện thoại di động hiện diện ở khắp mọi nơi. Một người có thể không có máy vi tính nhưng gần như không thể không có điện thoại di động. Điện thoại di động ngày càng thông minh và chúng đang dần thay thế các thiết bị di động cá nhân khác như: đồng hồ, máy tính cầm tay, máy ảnh du lịch, máy nghe nhạc MP3, hoặc các thiết bị có khả năng kết nối internet khác. Rõ ràng thị trường di động là một miếng bánh khổng lồ mà không ai có thể một mình nuốt gọn. Các nhà phát triển cũng cảm thấy sức hấp dẫn mà miếng bánh tỏa ra. Nhưng việc phát triển phần mềm cho các thiết bị di động chưa bao giờ là công việc đơn giản. Có hàng loạt các vấn đề mà các nhà phát triển phần mềm cho thiết bi di động trước kia phải đối mặt:- Không có mã nguồn của các nền tảng để giúp họ hình dung ra được cơ chế hoạt động đằng sau đó.
- Giá thành để phát triển một sản phẩm cao do phải trả phí mua bản quyền API hoặc công cụ phát triển.
- Vướng phải những ràng buộc hoặc giới hạn nhất định về tính năng khi phát triển kể cả khi họ có giấy phép phát triển.
- Không có một bộ công cụ phát triển (SDK) nào thực sự dễ dàng cho việc xây dựng ứng dụng di động.
- Không có một cơ chế phân phối sản phẩm hiệu quả đến người dùng cuối cũng như cách thức cài đặt phần mềm đơn giản lên thiết bị của họ.
Hình 1: Các thành viên trong liên minh OHA
Vào tháng 10-2008, thiết bị đầu tiên chạy Android được Google và HTC
phối hợp phát triển đã ra đời với tên gọi G1. Từ đó đến nay, Android đã
trải qua nhiều phiên bản với nhiều cải tiến, đã có vô số các thiết bị
chạy nền tảng này được tung ra thị trường với mức độ đa dạng chưa từng
thấy. Hiện Samsung đang là hãng sản xuất và phân phối hàng đầu các thiết
bị chạy Android trên toàn cầu.Nền tảng Android về cơ bản đáp ứng được những đòi hỏi của OHA:
Monday, April 15, 2013
Socket trong Android
Trong bài này mình sẽ giới thiệu với các bạn về lập trình Socket(bài này
mình chỉ nói đến lập trình Socket cho TCP/IP) trong Android.
Lập trình Socket chắc hẳn rất quen thuộc với các bạn học CNTT, vì thế mình chỉ nói lại một số vấn đề chính liên quan đến Socket:
- Socket là một phương pháp để thiết lập kết nối truyền thông giữa một chương trình yêu cầu dịch vụ ( client) và một chương trình cung cấp dịch vụ (server) trên mạng LAN, WAN hay Internet. Một socket trên máy yêu cầu dịch vụ có địa chỉ mạng được cấp sẵn để gọi một socket trên máy cung cấp dịch vụ. Một khi socket đó được thiết lập phù hợp, hai bên có thể trao đổi dịch vụ và dữ liệu.
-Tóm váy lại là thằng Server thì phải tạo một Server luôn luôn lắng nghe (luôn luôn thấu hiểu) ở một cổng nào đó ví dụ 8888 chẳng hạn. Thằng Client muốn kết nối được đến Server để gửi yêu cầu thì phải xác định được cổng đó và IP của Server.
Lập trình Socket chắc hẳn rất quen thuộc với các bạn học CNTT, vì thế mình chỉ nói lại một số vấn đề chính liên quan đến Socket:
- Socket là một phương pháp để thiết lập kết nối truyền thông giữa một chương trình yêu cầu dịch vụ ( client) và một chương trình cung cấp dịch vụ (server) trên mạng LAN, WAN hay Internet. Một socket trên máy yêu cầu dịch vụ có địa chỉ mạng được cấp sẵn để gọi một socket trên máy cung cấp dịch vụ. Một khi socket đó được thiết lập phù hợp, hai bên có thể trao đổi dịch vụ và dữ liệu.
-Tóm váy lại là thằng Server thì phải tạo một Server luôn luôn lắng nghe (luôn luôn thấu hiểu) ở một cổng nào đó ví dụ 8888 chẳng hạn. Thằng Client muốn kết nối được đến Server để gửi yêu cầu thì phải xác định được cổng đó và IP của Server.
Đọc nội dung file .txt trong sdcard [ For Newbie ]
try{ File f = new File(Environment.getExternalStorageDirectory()+"/test.txt"); fileIS = new FileInputStream(f); BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS)); String readString = new String(); //đọc theo từng dòng while((readString = buf.readLine())!= null){ // hiển thị ra LOGCAT Log.d("line: ", readString); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); }Khi chạy bạn nhớ bật Logcat để xem kết quả. Đoạn code đặt ngay trong hàm onCreate() nhé.
Full screen Android app
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
[Code snippets] Download 1 HTTP file với progess notification
try {
// Khai báo url
URL url = new URL("http://somewhere.com/some/webhosted/file");
//tạo HTTP connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//setup phương thức cho HTTP connecton
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// kết nối
urlConnection.connect();
// Khai báo url
URL url = new URL("http://somewhere.com/some/webhosted/file");
//tạo HTTP connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//setup phương thức cho HTTP connecton
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// kết nối
urlConnection.connect();
[Code Snippets] Lấy thông tin về Battery
main.xml
[JAVA]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android
rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
[JAVA]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android

android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
[Code Snippets] Một cách kiểm tra phiên bản mới của ứng dụng
Có nhiều cách để cho người dùng kiểm tra phiên bản mới ứng dụng. Phương
pháp sau đây không cần phải có server riêng để kiểm tra phiên bản mà
dùng chính Android Market server (đòi hỏi ứng dụng phải có trên Market).
Nguyên lý là bạn truy cập trang thông tin ứng dụng của mình để trích
xuất thông tin phiên bản, sau đó so sánh với phiên bản hiện tại trong
PackageInfo của ứng dụng.
Đoạn code sau đây mình lấy từ chương trình OptiNews (định danh là vn.zerox.optinews, các bạn sẽ thay bằng cái khác):
Đoạn code sau đây mình lấy từ chương trình OptiNews (định danh là vn.zerox.optinews, các bạn sẽ thay bằng cái khác):
Wednesday, April 10, 2013
Android Google Map View Example Tutorial
Step 1 : Obtaining Google API Key
To make use of Google Map or any other Google API you need to have a Google API key.It is extremely easy to get a Google API Key. You see below I have made a Video which is made up of step-by-step procedure for obtaining the Google API key.
Detailed Steps
- Proceed to the location exactly where JDK installed.
- Certainly, there you will see KeyTool.
- Then simply Copy the path.
- Start a command prompt then change the directory to the keytool path.
- copy and paste the below code snippet to command prompt
keytool -list -alias androiddebugkey -keystore
- At this point we need to copy the path of the debug.keystore
- Head over to windows/users/[your computer user name]/.android/
- It’s simple to find the debug.keystore, so copy the path.
- Next paste the keystore path surrounded with “” combined with the step 5 code snippet.
keytool -list -alias androiddebugkey -keystore "C:.android.keystore" -storepass android -keypass android
- Last but not least press Enter. You will notice the Certificate Finger Print.

- At this moment visit this link to sign up to Google API.
- Enter the Finger print using only number and digits. Stay away from including “:” .
- After that Tick the Agreement.
- Simply click Generate API key. You should see the API key now. Copy the actual URL intended for future reference.

Step 2 : Hands-on Google Map in Emulator
Before start to code our Google Map Alert App, we are now going to test it, to ensure we are ready to go.- Start a Sample Project named GoogleMapDemo.

- Choose the Choose bulid SDK to Google API. I choosed Google API Level 16.
- Then Click Next for the rest, Finally press Finish.

- Copy the below code to your Layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.google.android.maps.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="true" android:apiKey="06Q7KxHWP0w3-i0IpzRr8sSRnDLyHWAfJ_lk8mQ" /> </RelativeLayout>
Here you should enter your apikey otherwise it won’t work.
- Ok in case you are so curious, then simply run it. You will find the below stopped message.

Just what exactly is the mistake we certainly have done. We have setup
every little thing as well as enter the MapView component within our
layout.
But It is not sufficient
to really make it do the job. We have seen lots of android tutorial for
beginners. Therefore by now you should capable of finding out, exactly
what do we have to do in order to succeed.
Absolutely yes, you are right, we should tell to the Manifest file that
we are going to make use of Google Map for this application, and also
Internet access.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.googlemapdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/google_map_demo" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps"/> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>
Do you believe that this time, it’s going to run the app and show the map.
I am not going to say anything at the moment, just proceed as well as run the project.
I am not going to say anything at the moment, just proceed as well as run the project.


So above is the answer. So for we have extends only Activity, but in
order to make use of Google Map, Our Java Code should extends
MapActivity. 

- Then finally include the unimplemented methods.
package com.example.googlemapdemo; import com.google.android.maps.MapActivity; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; public class MainActivity extends MapActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }

Thursday, February 21, 2013
Ứng dụng Ajax, Json để Upload file trong CodeIgniter
Chào các bạn, hôm nay mình sẽ hướng dẫn các bạn sử dụng ajax khi chúng ta upload file trong CodeIgniter!
Để nắm rõ dc TUT này, các bạn phải có kiến thức cơ bản về Json nha
! Tham thảo jsontại :http://www.qhonline.info/forum/showt...highlight=json.
+++Ở đây, chúng ta sẽ nạp vào thư viện AjaxFileUpload. Các bạn có thể tải tại đây:http://www.phpletter.com/DOWNLOAD/.
->Các bước cb đã xong !giờ chúng ta bắt tay vào viết ứng dụng nào
.
Để nắm rõ dc TUT này, các bạn phải có kiến thức cơ bản về Json nha

+++Ở đây, chúng ta sẽ nạp vào thư viện AjaxFileUpload. Các bạn có thể tải tại đây:http://www.phpletter.com/DOWNLOAD/.
->Các bước cb đã xong !giờ chúng ta bắt tay vào viết ứng dụng nào

Caching website với CodeIgniter
Nội dung :
1.Tại sao cần phải caching website:
- Trước hết , bạn hiểu như thế nào là caching : Web caching là việc lưu trữ bản sao của những tài liệu web sao cho gần với người dùng, cả về mặt chức năng trong web client hoặc những web caching servers riêng biệt
- Ưu điểm :
- Tại sao cần phải caching website
- Sự khác biệt giữa caching và không caching
- Làm thế nào để caching trong CodeIgniter
1.Tại sao cần phải caching website:
- Trước hết , bạn hiểu như thế nào là caching : Web caching là việc lưu trữ bản sao của những tài liệu web sao cho gần với người dùng, cả về mặt chức năng trong web client hoặc những web caching servers riêng biệt
- Ưu điểm :
- Load nội dung nhanh hơn
- Hạn chế việc truy vấn trực tiếp vào Databse
Monday, January 28, 2013
Combo box / Drop down list box in Android
As usual doing something simple in Android is frustrating - this time: a simple Combo (or DropDownList). The little and important tweak - calling "setDropDownViewResource" to set the style of the items in the dialog box.
XML:
<spinner id="@+id/spinner" layout_width="wrap_content" android:layout_height="wrap_content" />
Code to add the data:
- Spinner spinner = (Spinner)this.findViewById(R.id.spinner);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- this,
- android.R.layout.simple_spinner_item,
- new String[] { "1", "2", "3" });
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spinner.setAdapter(adapter);
- Spinner spinner = (Spinner)this.findViewById(R.id.spinner);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- this,
- android.R.layout.simple_spinner_item,
- new String[] { "1", "2", "3" });
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spinner.setAdapter(adapter);
Tuesday, January 22, 2013
Using Google Maps in Android
Google Maps is one of the many applications bundled with the Android
platform. In addition to simply using the Maps application, you can also
embed it into your own applications and make it do some very cool
things. In this article, I will show you how to use Google Maps in your
Android applications and how to programmatically perform the following:
- Change the views of Google Maps
- Obtain the latitude and longitude of locations in Google Maps
- Perform geocoding and reverse geocoding
- Add markers to Google Maps
Saturday, January 19, 2013
How to set environment variables in Windows 7 for Java
Environmental variables are used by the operating system to save
settings (default values, locations of resources) to be used by Windows
or by processes launched by users.
There are two types of environmental variables:
Even if these variables are usually defined and initialized automatically when you install the system or other applications, there are situations in which the user must manually define them to put at the disposal of applications.
The considered scenario is to set environment variables to enable the compilation and execution of Java applications from the command line (command prompt) or by using an IDE like Eclipse. By installing the Java SDK, system variables about the location of executables (compiler, java virtual machine) are not defined or initialized automatically.
Testing is done by opening command prompt (Start -> cmd) and trying to launch the compiler with the command
There are two types of environmental variables:
- user variables that are specific to a particular Windows user account;
- system variables are always visible, regardless of the used user account.
Even if these variables are usually defined and initialized automatically when you install the system or other applications, there are situations in which the user must manually define them to put at the disposal of applications.
The considered scenario is to set environment variables to enable the compilation and execution of Java applications from the command line (command prompt) or by using an IDE like Eclipse. By installing the Java SDK, system variables about the location of executables (compiler, java virtual machine) are not defined or initialized automatically.
Testing is done by opening command prompt (Start -> cmd) and trying to launch the compiler with the command
Thursday, January 3, 2013
Hàm lấy ảnh thumbnails từ hình ảnh trong bài viết,featured image và youtube
Chèn vào file functions.php trong thư mục theme
Code:
// Note that your theme must support post thumbnails for this function to work. // If you are getting an error try adding add_theme_support('post-thumbnails'); to your functions. php file function vp_get_thumb_url($text, $size){ global $post; $imageurl=""; // Check to see which image is set as "Featured Image" $featuredimg = get_post_thumbnail_id($post->ID); // Get source for featured image $img_src = wp_get_attachment_image_src($featuredimg, $size); // Set $imageurl to Featured Image $imageurl=$img_src[0]; // If there is no "Featured Image" set, move on and get the first image attached to the post if (!$imageurl) { // Extract the thumbnail from the first attached imaged $allimages =&get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID ); foreach ($allimages as $img){ $img_src = wp_get_attachment_image_src($img->ID, $size); break; } // Set $imageurl to first attached image $imageurl=$img_src[0]; } // If there's no image attached or inserted in the post, look for a YouTube video if (!$imageurl){ // look for traditional youtube.com url from address bar preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/watch(\?v\=|\/v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2); $youtubeurl = $matches2[0]; if ($youtubeurl) $imageurl = "http://i.ytimg.com/vi/{$matches2[3]}/1.jpg"; // look for YouTube embed else preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/(v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2); $youtubeurl = $matches2[0]; if ($youtubeurl) // Get the thumbnail YouTube automatically generates // '0' is the biggest version, use 1 2 or 3 for smaller versions $imageurl = "http://i.ytimg.com/vi/{$matches2[3]}/1.jpg"; } // If there is no image attached to the post, look for anything that looks like an image and get that if (!$imageurl) { preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i' , $text, $matches); $imageurl=$matches[1]; } // If there is no youtube thumbnail, show a default image if (!$imageurl) { $imageurl= "http://killthebeat.com/wp-content/themes/buzznews/images/thumbz.jpg"; } // Spit out the image path return $imageurl; }
Wednesday, January 2, 2013
Tạo File DEB và Source Cydia
Chúng ta hãy khái niệm sơ qua về cách thức hoạt động của Cydia và cách vận hành của nó!
Cydia được viết bởi Jay Freeman (saurik) cũng là 1 trong những thành viên trong iphone dev team dựa trên nền tảng unix có tính tương thích cao với HĐH MAC OS của iPhone giúp dễ dàng quản lý cũng như cài đặt các chương trình của hãng thứ 3 (liên quan tới System, nên ko được cấp phép bới Apple) nói cách khác Cydia được cài cho những máy đã Jailbreak.
Những gói cài đặt trên Installer là dạng .zip khi được down về thì Installer sẽ giải nén nó ra vào đường dẫn tương ứng. Cydia cũng giống vậy nhưng gói cài đặt của nó là dạng deb (DEBIAN) 1 dạng của Linux.
Cydia hoạt động bằng cách Down 1 file Packages.gz hoặc Packages.bz2 của 1 host bất kỳ và phân tích giải nén nó ra được 1 file Packages bao gồm những thông tin của toàn bộ những Packages trên Source.
Ta có 1 host làm Source cho Cydia Cydia Repo khi ta add Source này vào Cydia thì Cydia sẽ Down file Packages.bz2 của host này về giải nén được file Packages và phân tích thông tin của từng gói cài đặt bao gồm:
Cydia được viết bởi Jay Freeman (saurik) cũng là 1 trong những thành viên trong iphone dev team dựa trên nền tảng unix có tính tương thích cao với HĐH MAC OS của iPhone giúp dễ dàng quản lý cũng như cài đặt các chương trình của hãng thứ 3 (liên quan tới System, nên ko được cấp phép bới Apple) nói cách khác Cydia được cài cho những máy đã Jailbreak.
Kiếm tiền từ wapThay thế Installer: như chúng ta đã biết Installer 1 chương trình quá quen thuộc với những người dùng iPhone FW 1.x. Nay khi FW 2.x 3.x ra đời Cydia đã thay thế cho Installer 1 cách toàn diện.
Những gói cài đặt trên Installer là dạng .zip khi được down về thì Installer sẽ giải nén nó ra vào đường dẫn tương ứng. Cydia cũng giống vậy nhưng gói cài đặt của nó là dạng deb (DEBIAN) 1 dạng của Linux.
Cydia hoạt động bằng cách Down 1 file Packages.gz hoặc Packages.bz2 của 1 host bất kỳ và phân tích giải nén nó ra được 1 file Packages bao gồm những thông tin của toàn bộ những Packages trên Source.
Ta có 1 host làm Source cho Cydia Cydia Repo khi ta add Source này vào Cydia thì Cydia sẽ Down file Packages.bz2 của host này về giải nén được file Packages và phân tích thông tin của từng gói cài đặt bao gồm:
Subscribe to:
Posts (Atom)
Popular Posts
-
I spent a long time to config them. so tired, but now it run very good. It's work. Step 1: Download Source PhoneGap at PhoneGap.com...
-
Để chuyển 1 file .app sang .ipa đầu tiên các bận cần có là 1 file .app, trong hướng dẫn mình sử dụng file Tuvi.app 1. Mở xcode len 2. Men...
-
Trong bài này mình sẽ giới thiệu với các bạn về lập trình Socket(bài này mình chỉ nói đến lập trình Socket cho TCP/IP) trong Android. Lập ...
-
Xử lý ngày tháng trong PHP Trừ ngày $date = "2009-03-02"; $new_date = strtotime ( '-2 day' , strtotime ( $date ) ) ; ...