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 Internet + local
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
- From local: MediaPlayer song = MediaPlayer.create(MainActivity.this, R.raw.nuocmat); song.start(); Stop: onPause(); song.pause(); - From Internet: public void PlayNhacMp3(String url){ //url = "http://khoapham.vn/download/vietnamoi.mp3"; MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(url); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); } catch (IOException e) { e.printStackTrace(); } } |
Lưu và đọc file .txt bởi KHOAPHAM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- Lưu file txt FileOutputStream fos = openFileOutput("khoapham.txt", Context.MODE_PRIVATE); fos.write(noidung.getBytes()); fos.close() - Đọc file txt FileInputStream fis = openFileInput("khoapham.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(fis))); String line = ""; while( (line = br.readLine()) != null ){ txtvNoiDung.append(line); txtvNoiDung.append("\n"); } |
Sử dụng CountDownTimer
1 2 3 4 5 6 7 8 9 10 11 12 |
new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); //here you can have your logic to set text to edittext } public void onFinish() { mTextField.setText("done!"); } }.start(); |
Delay with Handler
1 2 3 4 5 6 7 8 |
final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // Do something after 5s = 5000ms } }, 5000); |
Replace fragment kèm theo Animation bởi Phúc Lưu Ngọc
1 2 3 4 5 6 7 8 |
private void replaceFragment(Fragment fragment) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left) .replace(R.id.content_main, fragment) .addToBackStack(null) .commit(); } |
Get TypeFace
1 2 3 |
public static Typeface getTypefaceFont(Context context) { return Typeface.createFromAsset(context.getAssets(), "Gilroy-Light.otf"); } |
Nguồn: androidcoban.com