Skip to content

Instantly share code, notes, and snippets.

@vtthach
vtthach / UnitTest.java
Created May 25, 2018 05:37
PowerMockito Mockito cheatseet
@RunWith(PowerMockRunner.class) – Tell Junit that run this test using PowerMockRunner
@PrepareForTest(A.class) – This is needed when we need to test static methods of A class
AService mock = PowerMockito.mock(A.class) – Creating a mock for A class
PowerMockito.when(mock.mockedMethod()).thenReturn(value) – When mockedMethod is called in the code, then return the value specified here.
@vtthach
vtthach / Proguard.java
Last active October 3, 2018 03:29
Proguard Meaning
android {
defaultConfig {
multiDexEnabled true // Multiple dex enable only for Android Version >21 (prior v21 see more at: https://developer.android.com/studio/build/multidex.html)
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
// Keep file into main dex
multiDexKeepFile file('multidex-config.txt')
multiDexKeepProguard file('multidex-config.pro')
}
buildTypes {
@vtthach
vtthach / Data binding example.java
Last active October 15, 2018 18:49
MVVM LiveData ViewModel Gradle Data Binding
private lateinit var binding: ActivityMainBinding
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
// Set view model
binding.viewModel = viewModel
// Execute pending binding data
binding.executePendingBindings()
// View model
val viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)
@vtthach
vtthach / SYSTEM_ALERT_WINDOW
Last active March 9, 2018 15:22
Draw over other app - SYSTEM_ALERT_WINDOW
Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store
(version 6.0.5 or higher is required), will have granted the permission automatically.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
// Show alert dialog to the user saying a separate permission is needed
// Launch the settings activity if the user prefers
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(myIntent);
}
@vtthach
vtthach / UserProfileFragment.java
Created September 4, 2017 12:31
Core leak with dialog fragment
package com.tcsdev.tcsapp.features.usermenu.userprofile;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@vtthach
vtthach / dependencies.gradle
Created August 31, 2017 07:42
Sample group of dependencies : i.e librarires.dagger
allprojects {
repositories {
jcenter()
}
}
ext {
//Android
androidBuildToolsVersion = "26.0.1"
androidMinSdkVersion = 15
@vtthach
vtthach / HiddenPassTransformationMethod.java
Created August 22, 2017 10:51
EditText - HiddenPassTransformationMethod
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// example of usage
((TextView) findViewById(R.id.password)).setTransformationMethod(new HiddenPassTransformationMethod());
}
private class HiddenPassTransformationMethod implements TransformationMethod {
@vtthach
vtthach / Transition with share element
Last active August 15, 2017 09:38
Transition with share element
We couldn’t find that file to show.
@vtthach
vtthach / Android Design Support Library
Last active August 14, 2017 09:44
Android Design Support Library - Style, Thems, Colors and Typography
app_themes.xml https://codelabs.developers.google.com/codelabs/material-design-style/img/5ffbda8697aa2b0f.png
@vtthach
vtthach / ImageBrowsePresenter.java
Created July 20, 2017 04:21
Notify gallery insert an image with path
// 其次把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(view.getMyContext().getContentResolver(), file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
view.getMyContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getPath())));