Skip to content

Instantly share code, notes, and snippets.

View andreikastsiuk's full-sized avatar
🤓
Student forever

Andrei Kastsiuk andreikastsiuk

🤓
Student forever
View GitHub Profile
@andreikastsiuk
andreikastsiuk / Manifest.xml
Created February 17, 2019 20:37
Simple sample of shaking process
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
@andreikastsiuk
andreikastsiuk / Ripple
Created February 11, 2019 23:02
Ripple effect
<style name="AppTheme.MyRipple">
<item name="colorControlHighlight">@color/your_color</item>
<item name="android:background">?selectableItemBackgroundBorderless</item>
</style>
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
cardView.setBackgroundResource(outValue.resourceId);
@andreikastsiuk
andreikastsiuk / MainActivity.java
Created February 11, 2019 21:50 — forked from granoeste/MainActivity.java
[Android] Button background change for state. (selector)
package com.example.sellectorsample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
@andreikastsiuk
andreikastsiuk / ViewModelProviderFactory.java
Created January 19, 2019 10:44
A provider factory that persists ViewModels. Used if the view model has a parameterized constructor.
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
/**
* A provider factory that persists ViewModels {@link ViewModel}.
* Used if the view model has a parameterized constructor.
*/
public class ViewModelProviderFactory<V> implements ViewModelProvider.Factory {
private V viewModel;
@andreikastsiuk
andreikastsiuk / DisplayUtil.java
Created January 13, 2019 23:48
Some useful display methods
public static int getScreenWidth(@NonNull Context context) {
Point size = new Point();
((Activity) context).getWindowManager().getDefaultDisplay().getSize(size);
return size.x;
}
public static int getScreenHeight(@NonNull Context context) {
Point size = new Point();
((Activity) context).getWindowManager().getDefaultDisplay().getSize(size);
return size.y;
/* Module level build.gradle */
android {
defaultConfig {
...
flavorDimensions "default"
}
signingConfigs {
/*
* Automates generation of Release APK
* ./gradlew assembleRelease
* */
Properties props = new Properties()
def userHome = Paths.get(System.getProperty('user.home'));
def propFile = file(userHome.resolve('PATH_TO_SIGNING_CONFIG_PROPERTIES_FILE.properties'))
if (propFile.canRead()) {
props.load(new FileInputStream(propFile))
if (props != null && props.containsKey('STORE_FILE') && props.containsKey('KEY_STORE_PASSWORD')
@andreikastsiuk
andreikastsiuk / filterFlavor.gradle
Created October 27, 2018 21:23
variantFilter (filter)
variantFilter { variant ->
// 'dev' flavor is only available for debug build
if (!variant.buildType.name.equals('debug') && variant.getFlavors().get(0).name.equals("dev")) {
variant.setIgnore(true)
}
// 'prod' flavor is only available for release build
if (!(variant.buildType.name.equals('release') || variant.buildType.name.equals('alpha')) &&
variant.getFlavors().get(0).name.equals("prod")) {
variant.setIgnore(true)
}
@andreikastsiuk
andreikastsiuk / ArchLifecycleApp.kt
Created October 8, 2018 14:16
Getting an event when app is in background/foreground
class ArchLifecycleApp : Application(), LifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onAppBackgrounded() {
Log.d("TAG", "App in background")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
((ViewGroup) findViewById(R.id.llRoot)).getLayoutTransition()
.enableTransitionType(LayoutTransition.CHANGING);
}