Các đoạn code hữu ích
Chuyển đơn vị dp thành pixel Tác giả: Nguyễn Linh
1 2 3 4 5 |
public static int convertDpToPixel(int dp) { Resources r = Resources.getSystem(); return Math.round(dp * (r.getDisplayMetrics().densityDpi / 160f)); } |
Lấy kích thước màn hình thiết bị Tác giả: KHOAPHAM
1 2 3 4 |
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); int ori = display.getOrientation(); |
Convert long time to simple Date
1 2 3 4 5 |
public static String convertLongToDay(long timeStamp) { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = new Date(timeStamp); return dateFormat.format(date); } |
Get String Resource
1 2 3 |
public static String getStringFromResources(Context context, int id) { return context.getResources().getString(id); } |
Check EditText Empty
1 2 3 4 5 6 7 8 9 |
public static boolean isEmpty(EditText etText) { if (etText.getText().toString().trim().length() > 0) { return true; } else { etText.requestFocus(); etText.setError("Vui lòng điền thông tin!"); return false; } } |
Format money Việt Nam
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Convert long to money type public static String formatNumber(long number) { if (number < 1000) { return String.valueOf(number); } try { NumberFormat formatter = new DecimalFormat("###,###"); String resp = formatter.format(number); resp = resp.replaceAll(",", "."); return resp; } catch (Exception e) { return ""; } } |
Check Email valid
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static boolean isEmailValid(String email) { boolean isValid = false; String expression = "[a-zA-Z0-9._-]+@[a-z]+(\\.+[a-z]+)+"; CharSequence inputStr = email; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); if (matcher.matches()) { isValid = true; } return isValid; } |
hoặc (bởi Dong Hai)
1 2 3 4 5 6 7 |
public final static boolean isValidEmail(CharSequence target) { if (TextUtils.isEmpty(target)) { return false; } else { return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); } } |
Play MP3 file from
Đọc tiếp