Chia sẻ code hay để dễ dàng tìm kiếm khi cần sử dụng.
Xem post chính tại đây.
Link : https://gist.github.com
- Replace fragment kèm theo Animation // By Phúc Lưu Ngọc
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(); }
2. Chuyển đơn vị dp thành pixel //Nguyễn Linh
public static int convertDpToPixel(int dp) { Resources r = Resources.getSystem(); return Math.round(dp * (r.getDisplayMetrics().densityDpi / 160f)); }
3. Lib Thư viện Utils // Tran Tien Tin
https://github.com/changer/android-utils
https://github.com/Trinea/android-common/tree/master/src/cn/trinea/android/common/util
4. PLAY MP3 FROM INTERNET + LOCAL //KHOAPHAM
- 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(); } }
5. SAVE + READ FILE TXT //KHOAPHAM
- Save file txt FileOutputStream fos = openFileOutput("khoapham.txt", Context.MODE_PRIVATE); fos.write(noidung.getBytes()); fos.close() - Read 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"); }
6. LẤY KÍCH THƯỚC CỦA MÀN HÌNH THIẾT BỊ //KHOAPHAM
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); int ori = display.getOrientation();
7. CONVERT FILE LOCAL TO BYTE[] //KHOAPHAM
public byte[] FileLocal_To_Byte(String path){ File file = new File(path); int size = (int) file.length(); byte[] bytes = new byte[size]; try { BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file)); buf.read(bytes, 0, bytes.length); buf.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bytes; }
8. Lớp Toast Utils // Huy NQ
https://goo.gl/tLf32v
9. Get TypeFace
public static Typeface getTypefaceFont(Context context) { return Typeface.createFromAsset(context.getAssets(), "Gilroy-Light.otf"); }
10. Format money Việt Nam
//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 ""; } }
11. Check EditText Empty
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; } }
12. Class MyClipboardManager
https://gist.github.com/nguyenlinhnttu/afa2c8d07074f804b7591ffb2bf7cbd3
13. Get String Resource
public static String getStringFromResources(Context context, int id) { return context.getResources().getString(id); }
14. Convert long time to simple Date
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); }
15. Check Email valid
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: // By Dong Hai
public final static boolean isValidEmail(CharSequence target) { if (TextUtils.isEmpty(target)) { return false; } else { return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); } }
16. Các lib hay trên github
- Animation: https://github.com/2359media/EasyAndroidAnimations
- MaterialDesign : https://github.com/lightSky/Awesome-MaterialDesign
- NavigationTabBar: https://github.com/DevLight-Mobile-Agency/NavigationTabBar
- Animation TextView: https://github.com/hanks-zyh/HTextView
- CircleImageView: https://github.com/hdodenhof/CircleImageView
- MPAndroidChart: https://github.com/PhilJay/MPAndroidChart
- Material-Animations: https://github.com/lgvalle/Material-Animations
17: Sử dụng RecyclerView
18: Tutorial Retrofit 2
Tiếng việt: https://goo.gl/VQ8zYt
Tiếng Anh : https://goo.gl/tcRSoQ
19: Thư viện Glide
Tiếng việt: https://goo.gl/vKRQQn
Tiếng Anh: https://goo.gl/8qsB7s
20. Thư viện bind view Butter Knife
Home: http://jakewharton.github.io/butterknife/
21: Google Play Rate app, Share App FB via package, Send GMail // By Nguyễn Chiến
https://gist.github.com/nguyenlinhnttu/c14fa16d85097db6a7b49f7402811e65
22:Privacy Nguyễn Hà Đông (Tham Khảo)
http://www.dotgears.com/privacy.html
23. Một số hàm được chia sẻ bởi // Chan Doi
24. AsyncTask Multiple param result multi value
25. Một số Intent implicit thường dùng
26. Sử dụng CountDownTimer
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();
27. Delay with Handler
final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // Do something after 5s = 5000ms } }, 5000);
Bình luận đã bị khoá.