Monday, July 7, 2014

Android snippets

  • Thiết kế giao diện đáp ứng nhiều kích cỡ màn hình khác nhau: dùng ảnh 9-patch png
  • Thiết lập màu nền bằng Resource thì không thiết lập bằng Color được
  • Thiết lập màu nền: viewVariable.setBackgroundColor(Color.parseColor("#fafafa"));
  • Tìm ID chưa được cấp phát khi thêm view mới động (dynamic):
        int findUnusedId(int startID) {
int fID = startID;  // i.e: startID = 555;
   while( findViewById(++fID) != null );
   return fID;
}
  • Tắt focus EditText mặc định: xóa dòng <requestFocus /> dưới EditText. Nếu chưa được, thêm thuộc tính vào thẻ:
        <RelativeLayout
        ...       
        android:focusable="true"
    android:focusableInTouchMode="true"
        ... >

  • MultiTouch
class gameTouchManager implements OnTouchListener {
     public gameTouchManager() {}

     @Override
     public boolean onTouch(View v, MotionEvent event) {
          int idx = event.getActionIndex();
          switch(event.getActionMasked()) {
               case MotionEvent.ACTION_DOWN:
               case MotionEvent.ACTION_POINTER_DOWN:
                    // do sth ...
               break;
               default: break;
          }
          return false;
     }

}

  • MultiThread

// declare in main activity
class ArrowHandler extends Handler {
     @Override
     public void handleMessage(Message msg) {
          super.handleMessage(msg);
          int val1 = msg.arg1;
          int val2 = msg.arg2;
          // do sth ...
     }
}

// declare in main activity
class PipeManager implements Runnable {
     int val1;

     public PipeManager(int val1) {
          this.val1 = val1;
     }

     @Override
     public void run() {
          while(!stopBoolean) {     // stopBoolean variable is declared before
               Message msg = arrowhandler.obtainMessage();
               msg.arg1 = 123;
               msg.arg2 = 1234;
               SystemClock.sleep(ConstantStorage.GAME_ARROW_SLEEPING_TIME);
               // do sth ...
          }
     }
}

// somewhere in main activity
arrowhandler = new ArrowHandler();
for(int i=0;i<4; i++) {
     Thread thr = new Thread(new PipeManager(i));
     thr.start();
}

  • Set text size programmatically
theButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizeInPixel);
  • Inflate new view via xml
LayoutInflater myLayout = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = myLayout.inflate(R.layout.bone_mainscreen_item, null);
  • TimePicker, lấy giá trị không chính xác khi người dùng gõ text
// force the timepicker to loose focus and the typed value is available !
timePicker.clearFocus();
// re-read the values, in my case i put them in a Time object.
time.hour   = timePicker.getCurrentHour();
time.minute = timePicker.getCurrentMinute();
** Nguồn: http://stackoverflow.com/questions/3992820/android-how-to-get-the-time-from-a-timepicker-when-it-is-typed-in
  • Tạo thư mục "fonts" trong thư mục "assets"
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/theFont.ttf");
button_or_textview.setTypeface(myFont,Typeface.BOLD);

** Nội dung còn tiếp. Link tham khảo cập nhật sau!

No comments:

Post a Comment