-
-
Save Haosvit/123d04342a6c1a7afca2fd58fc2af869 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Chia sẻ code hay để dễ dàng tìm kiếm khi cần sử dụng. | |
1.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 | |
- 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 | |
- 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Ị | |
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[] | |
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 | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment