Skip to content

Instantly share code, notes, and snippets.

@ekartoyev
ekartoyev / app.kt
Created July 26, 2020 11:51
Getting context application-wide #kotlin #android
import android.app.Application
import android.content.Context
class App : Application() {
companion object {
lateinit var mApp: Application
fun context(): Context {
return mApp.applicationContext
}
}
@ekartoyev
ekartoyev / catch_memory_crash.java
Created July 23, 2020 15:36
Catch out of memory crash #android #java
public class MyActivity extends Activity implements Thread.UncaughtExceptionHandler{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if(ex.getClass().equals(OutOfMemoryError.class))
@ekartoyev
ekartoyev / button_shadow_style
Last active July 22, 2020 23:56
Button shadow snippet #android #style #app #xml
<item name="elevation">4dp</item>
<item name="android:translationZ">4dp</item>
<item name="android:stateListAnimator">@anim/nothing</item>
<item name="android:outlineProvider">bounds</item>
@ekartoyev
ekartoyev / aspect_ratio_view.xml
Created July 22, 2020 11:39
View with aspect ratio
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
@ekartoyev
ekartoyev / vibrate
Created July 20, 2020 23:48
Vibrate #kotlin #android
private fun vibrate() {
val vibe = ctx.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibe?.vibrate(VibrationEffect.createOneShot(150, 10))
} else {
vibe?.vibrate(150)
};
}
@ekartoyev
ekartoyev / AutoText Style
Last active July 23, 2020 00:02
This style resource makes text autofit in the view boundaries #android #style #textview #xml
<style name="autotext_style">
<item name="autoSizeTextType">uniform</item>
<item name="autoSizeMinTextSize">10sp</item>>
<item name="autoSizeMaxTextSize">1000sp</item>
<item name="autoSizeStepGranularity">2sp</item>
</style>
@ekartoyev
ekartoyev / binding_gradle_parameter.java
Last active July 31, 2020 17:15
Data Binding Gradle Parameter
android {
//...
// Deprecated version
// dataBinding { enabled = true }
// Current version
buildFeatures {
dataBinding = true
// viewBinding = true
@ekartoyev
ekartoyev / str_to_byte_array.java
Last active June 9, 2020 00:30
Convert hex string to byte array ("10bf" ➡ 10, bf)
public static byte[] strToByteArr(String str) {
byte[] val = new byte[str.length() / 2];
for (int i = 0; i < val.length; i++) {
int index = i * 2;
int j = Integer.parseInt(str.substring(index, index + 2), 16);
val[i] = (byte) j;
}
return val;
}
@ekartoyev
ekartoyev / merge_arrays.java
Created February 28, 2019 03:41
Java - Merge two byte arrays
byte[] merge(byte[] a, byte[] b) {
int aLen = a.length;
int bLen = b.length;
byte[] c= new byte[aLen + bLen];
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);
return c;
}
@ekartoyev
ekartoyev / MZ85.java
Last active February 28, 2019 03:42
Java - Z85 encodding (MZ stands for my zed)
public class MZ85
{
private MZ85()
{
}
public static byte[] ENCODER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', '-',
':', '+', '=', '^', '!', '/', '*', '?', '&', '<', '>', '(', ')', '[', ']', '{', '}', '@', '%', '$', '#', '0' };