Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
EmmanuelGuther / Logout.kt
Created June 6, 2018 11:13
A way to close all activities except one. Android Kotlin
fun closeActivitiesGoLogin(context: Context?) {
val intent = Intent(context, LoginActivity::class.java)
val cn = intent.component
val mainIntent = Intent.makeRestartActivityTask(cn)
context?.startActivity(mainIntent)
}
@EmmanuelGuther
EmmanuelGuther / regexStrongPassword.kt
Created June 6, 2018 10:17
REGEX STRONG PASSWORD
Description of this regular expression is as below:
Passwords will contain at least 1 upper case letter
Passwords will contain at least 1 lower case letter
Passwords will contain at least 1 number or special character
Passwords will contain at least 8 characters in length
Password maximum length should not be arbitrarily limited
(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
@EmmanuelGuther
EmmanuelGuther / Snackbar.kt
Created June 1, 2018 10:58
Kotlin extension function to easy change snackbar text color
fun Snackbar.setTextColor(color: Int): Snackbar {
val tv = view.findViewById(android.support.design.R.id.snackbar_text) as TextView
tv.setTextColor(color)
return this
}
@EmmanuelGuther
EmmanuelGuther / HideKeyboard.kt
Created May 16, 2018 16:30
Kotlin extension function to hide keyboard if it is being shown
fun AppCompatActivity.hideKeyBoard(){
val view = this.currentFocus
if (view != null) {
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
@EmmanuelGuther
EmmanuelGuther / HideShowKeyboard.kt
Created May 16, 2018 15:37
Extension function to hide or show keyboard
fun AppCompatActivity.toggleHideShowKeyboard() {
val inputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
when {
inputMethodManager.isActive -> inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
else -> inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0) // hide
}
}
@EmmanuelGuther
EmmanuelGuther / CircularImageView.kt
Created April 15, 2018 09:36
Kotlin circular imageview
class CircularImageView : ImageView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}
override fun onDraw(canvas: Canvas) {
@EmmanuelGuther
EmmanuelGuther / roundedBitmap.kt
Created April 15, 2018 08:38
Kotlin function to transform bitmap into roundedBitmap
fun roundedBitmap(bitmap: Bitmap): RoundedBitmapDrawable {
val roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(Resources.getSystem(), bitmap)
roundedBitmapDrawable.isCircular = true
roundedBitmapDrawable.cornerRadius = Math.max(bitmap.width, bitmap.height) / 2.0f
return roundedBitmapDrawable
}
@EmmanuelGuther
EmmanuelGuther / RetrofitGetAPIWithAuth.kt
Last active October 11, 2024 10:11
Get rest api with Auth - token and using okhttp Authenticator to manage the refresh token
object RetrofitGetAPIWithAuth {
private val BASE_URL = BuildConfig.API_BASE
private var retrofit: Retrofit? = null
private val tokenLoginModel: TokenLoginModel? = obtainTokenLoginModel()
private val accessToken = tokenLoginModel?.access_token
class HeaderInterceptor : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
@EmmanuelGuther
EmmanuelGuther / ImagePicker.java
Created February 19, 2018 18:55 — forked from Mariovc/ ImagePicker.java
Utility for picking an image from Gallery/Camera with Android Intents
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
@EmmanuelGuther
EmmanuelGuther / AccountAuthenticator.java
Created February 19, 2018 12:58 — forked from burgalon/AccountAuthenticator.java
Implementing OAuth2 with AccountManager, Retrofit and Dagger
public class AccountAuthenticator extends AbstractAccountAuthenticator {
private final Context context;
@Inject @ClientId String clientId;
@Inject @ClientSecret String clientSecret;
@Inject ApiService apiService;
public AccountAuthenticator(Context context) {
super(context);