Thứ Năm, 13 tháng 9, 2018

Android SnackBar

- SnackBar cung cấp 1 cái phản hồi ngắn gọi thông qua 1 message dưới màn hình

- Thêm thư viện vào build.gradle(Module: app)

compile 'com.android.support:design:27.1.1'
or
implementation 'com.android.support:design:27.1.1'

compile - android studio phiên bản cũ
implementation - android studio phiên bản mới

27.1.1 là version

- viết code
+ ở layout:
tôi kéo 1 button và gán sự kiện click cho nó.
chúng ta sẽ code Snackbar trong sự kiện click này.
Snackbar.make(v, "Nguyen The Lam", Snackbar.LENGTH_SHORT)
        .setAction("Action", new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Toast nay", Toast.LENGTH_SHORT).show();
            }
        })
        .setActionTextColor(Color.RED)
        .show();

ý nghĩa:

Đối tượng Snackbar gọi hàm make
tham số: v - view
"Nguyen The Lam" - chuỗi show ra màn hình
"Snackbar.LENGTH_SHORT" - thời gian show lên màn hình

ở đây, tôi set thêm action cho snackbar. khi snackbar hiện lên
sẽ có thêm 1 action click và hiển thị Toast với nội dung như trên.
Tôi set thêm màu cho action này

và cuối cùng là call hàm show() để hiển thị snackbar này.




Thứ Hai, 10 tháng 9, 2018

Android measure unit: px, in, dip, dp, sp, pt

1. px - pixels
corresponds to actual pixels on the screen
tương ứng với điểm ảnh thực trên màn hình

2. in - inches
based on the physical size of the screen
dựa trên size vật lý của màn hình
1 inch = 2.54 centimeters (cm)

3. mm - millimeters
based on the physical of the screen
dựa trên size vật lý của màn hình

4. pt - points
1/72 of an inch based on the physical size of the screen
pt = 1/72 in

5. dp or dip - density
independent pixels - an abstract unit that is based on the physical density of the screen.
pixels độc lập - 1 đơn vị trừu tượng mà dựa trên tỷ trọng vật lý của màn hình.

6. sp - scale
independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference.
pixels độc lập - giống với dp, nhưng nó có thể co giãn bởi thiết lập cỡ font của user.

reference (tham khảo): https://developer.android.com/guide/topics/resources/more-resources#Dimension