Skip to content

Instantly share code, notes, and snippets.

View bapspatil's full-sized avatar
🪄

Bapusaheb Patil bapspatil

🪄
View GitHub Profile
@bapspatil
bapspatil / build.gradle for app
Last active September 14, 2022 12:40
app module's build.gradle file for Android App Bundle
// 'app' module should have the application plugin.
apply plugin: 'com.android.application'
android {
defaultConfig {
...
// Specify the version code, only once for each new version of your app, for your App Bundle
// No need to have different version codes for different APKs generated.
// All split APKs will share the same version code once installed via Google Play.
versionCode 1
@bapspatil
bapspatil / RetrofitCached.java
Created December 23, 2017 21:51
Make Retrofit work offline with caching
package bapspatil.pantheon.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.io.IOException;
import bapspatil.pantheon.network.RetrofitAPI;
import okhttp3.Cache;
apply plugin: 'com.android.application'
android {
...
...
defaultConfig {
...
versionCode 2000
...
@bapspatil
bapspatil / RetrofitAPI.kt
Created September 7, 2018 06:59
Final code snippet for cache-enabled Retrofit
val cacheSize = (5 x 1024 x 1024).toLong()
val myCache = Cache(context.cacheDir, cacheSize)
val okHttpClient = OkHttpClient.Builder()
.cache(myCache)
.addInterceptor { chain ->
var request = chain.request()
request = if (hasNetwork(context)!!)
request.newBuilder().header("Cache-Control", "public, max-age=" + 5).build()
else
@bapspatil
bapspatil / AA Transition App theme between light and dark themes
Created December 13, 2017 14:43 — forked from alphamu/AA Transition App theme between light and dark themes
Example of how to change themes at runtime with a smooth animation. In this example, we just switch between a light and a dark theme. The activity animation is defined in the theme and as such will be default on all activities with the set theme. [Demo Video here](http://youtu.be/Ps0phswbHls).
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class TransitionThemeActivity extends AppCompatActivity implements View.OnClickListener {
@bapspatil
bapspatil / MyOkHttpClient.kt
Last active January 4, 2021 19:39
OkHttpClient (SHORT) for caching
val okHttpClient = OkHttpClient.Builder()
// Specify the cache we created earlier.
.cache(myCache)
// Add an Interceptor to the OkHttpClient.
.addInterceptor { chain ->
// Get the request from the chain.
var request = chain.request()
/*
@bapspatil
bapspatil / EndlessScrollListener.java
Created November 12, 2017 01:53
An endless scroll listener for RecyclerView.
/*
** Created by bapspatil
*/
public abstract class EndlessScrollListener extends RecyclerView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
@bapspatil
bapspatil / ExoPlayerDemoFragment.java
Last active May 10, 2020 09:32
Generic code to set up ExoPlayer in a Fragment
/*
** Created by bapspatil
*/
public class ExoPlayerDemoFragment extends Fragment {
@BindView(R.id.video_exoplayer_view) SimpleExoPlayerView mPlayerView;
private SimpleExoPlayer mPlayer;
private Unbinder unbinder;
public ExoPlayerDemoFragment() {
@bapspatil
bapspatil / ExpandableTextView.java
Created December 5, 2017 14:10 — forked from gilbertwat/ExpandableTextView.java
A TextView that only click to expand to show full text and click to show shorten text, The ellipsis can be configured too.
package com.mpayme.znap.core.widget;
import java.lang.reflect.Field;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
@bapspatil
bapspatil / CounterView.kt
Last active September 17, 2019 06:39
CounterView
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.inc_dec_view.view.*
/**
* Created by Bapusaheb Patil.
*/
class IncDecView : LinearLayout, View.OnClickListener {