Last active
August 10, 2022 08:25
-
-
Save nguyenlinhnttu/41b9ab26e09f28c0970a38297c270cac to your computer and use it in GitHub Desktop.
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
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
Tiếng việt: https://goo.gl/vKRQQn | |
Tiếng Anh: https://goo.gl/8qsB7s |
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
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); | |
int width = display.getWidth(); | |
int height = display.getHeight(); | |
int ori = display.getOrientation(); |
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
https://goo.gl/tLf32v | |
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
// 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(); | |
} |
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
//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 ""; | |
} | |
} | |
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
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; | |
} | |
} | |
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
https://gist.github.com/nguyenlinhnttu/afa2c8d07074f804b7591ffb2bf7cbd3 |
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
public static String getStringFromResources(Context context, int id) { | |
return context.getResources().getString(id); | |
} | |
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
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); | |
} |
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
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(); | |
} | |
} | |
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
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 | |
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
https://goo.gl/cHWRSD |
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
Tiếng việt: https://goo.gl/VQ8zYt | |
Tiếng Anh : https://goo.gl/tcRSoQ |
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
public static int convertDpToPixel(int dp) { | |
Resources r = Resources.getSystem(); | |
return Math.round(dp * (r.getDisplayMetrics().densityDpi / 160f)); | |
} |
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
Home: http://jakewharton.github.io/butterknife/ | |
Hướng dẫn add : https://goo.gl/uhJ5Ge |
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
http://www.dotgears.com/privacy.html |
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
https://goo.gl/sJk8D1 |
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
https://goo.gl/XtXHQs |
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
https://goo.gl/IFx609 |
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
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(); |
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
final Handler handler = new Handler(); | |
handler.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
// Do something after 5s = 5000ms | |
} | |
}, 5000); |
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
https://github.com/changer/android-utils | |
https://github.com/Trinea/android-common/tree/master/src/cn/trinea/android/common/util |
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
- 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(); | |
} | |
} |
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
- 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"); | |
} |
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
public static Typeface getTypefaceFont(Context context) { | |
return Typeface.createFromAsset(context.getAssets(), "Gilroy-Light.otf"); | |
} | |
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
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