Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tổng hợp các đoạn code hữu ích cho lập trình android - Chia sẻ code hay để dễ dàng tìm kiếm khi cần sử dụng. Xem thêm : https://goo.gl/agdWhl
Tiếng việt: https://goo.gl/vKRQQn
Tiếng Anh: https://goo.gl/8qsB7s
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int ori = display.getOrientation();
https://goo.gl/tLf32v
// 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();
}
//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 "";
}
}
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;
}
}
https://gist.github.com/nguyenlinhnttu/afa2c8d07074f804b7591ffb2bf7cbd3
public static String getStringFromResources(Context context, int id) {
return context.getResources().getString(id);
}
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);
}
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();
}
}
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
https://goo.gl/cHWRSD
Tiếng việt: https://goo.gl/VQ8zYt
Tiếng Anh : https://goo.gl/tcRSoQ
public static int convertDpToPixel(int dp) {
Resources r = Resources.getSystem();
return Math.round(dp * (r.getDisplayMetrics().densityDpi / 160f));
}
Home: http://jakewharton.github.io/butterknife/
Hướng dẫn add : https://goo.gl/uhJ5Ge
https://gist.github.com/nguyenlinhnttu/c14fa16d85097db6a7b49f7402811e65
http://www.dotgears.com/privacy.html
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();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
}
}, 5000);
https://github.com/changer/android-utils
https://github.com/Trinea/android-common/tree/master/src/cn/trinea/android/common/util
- 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();
}
}
- 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");
}
public static Typeface getTypefaceFont(Context context) {
return Typeface.createFromAsset(context.getAssets(), "Gilroy-Light.otf");
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.