Showing posts with label Code Snippets android. Show all posts
Showing posts with label Code Snippets android. Show all posts

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

Monday, April 15, 2013

Đọ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_FULLSCREENWindowManager.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();

[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:eek: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"
/>

[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):

Popular Posts