Skip to content

Instantly share code, notes, and snippets.

Avatar

Saurabh saurabharora90

View GitHub Profile
View Placeholder.java
public static @DrawableRes int getPlaceHolder(Context context, @DrawableRes int actualPlaceholder) {
if(isLowRamDevice(context))
return R.drawable.low_ram_placeholder;
else
return actualPlaceholder;
}
public static boolean isLowRamDevice(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
return activityManager != null && ActivityManagerCompat.isLowRamDevice(activityManager);
View Description.java
public static Description getDescriptionsFromJson(JsonElement jsonElement){
Description description = new Description();
for (Map.Entry<String, JsonElement> entry :jsonElement.getAsJsonObject().entrySet()){
if(entry.getKey().equalsIgnoreCase("en") || entry.getKey().equalsIgnoreCase(getDefaultLangCode()))
description.descriptionMap.put(entry.getKey(), entry.getValue().getAsString());
}
return description;
}
public static String getDefaultCode(Context context){
@saurabharora90
saurabharora90 / styles_themes.xml
Last active Feb 17, 2018
Primer on Styles and Themes
View styles_themes.xml
<!-- A theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/randomRed</item>
<item name="alertDialogTheme">@style/MyDialog</item>
</style>
<style name="MyDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="colorAccent">@color/secondary</item>
@saurabharora90
saurabharora90 / Manfiest.xml
Last active Feb 17, 2018
Primer on Styles and Themes - Seekbar theme
View Manfiest.xml
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
View ServiceConnection.java
@Override
protected void onDestroy() {
super.onDestroy();
customTabActivityHelper.setConnectionCallback(null);
}
@Override
protected void onStart() {
super.onStart();
customTabActivityHelper.bindCustomTabsService(this);
View ServiceConnection.kt
private val customTabActivityHelper: CustomTabActivityHelper =
CustomTabActivityHelper(this, lifecycle, this)
override fun onClick(view: View) {
val viewId = view.id
val uri = Uri.parse(urlEditText.text.toString())
when (viewId) {
R.id.button_may_launch_url -> customTabActivityHelper.mayLaunchUrl(uri)
R.id.start_custom_tab -> {
val customTabsIntent = CustomTabsIntent.Builder(customTabActivityHelper.session)
@saurabharora90
saurabharora90 / layout.xml
Created Sep 30, 2018
Motion Layout Example - Building the example shown at Google IO: https://youtu.be/ytZteMo4ETk?t=31m59s
View layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
app:layoutDescription="@xml/scene_05"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivDusk"
android:layout_width="0dp"
@saurabharora90
saurabharora90 / BGLocation.kt
Last active Jul 10, 2019
Request access to location while app is in background,
View BGLocation.kt
<manifest>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
</manifest>
//Request for the permission like any other permission request:
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION),
your-permission-request-code)
@saurabharora90
saurabharora90 / MoveFab.kt
Created Jul 10, 2019
Move FAB above so that it doesn't conflict with transparent floating navigation bar
View MoveFab.kt
root.setOnApplyWindowInsetsListener { _, insets ->
val fabLp = fab.layoutParams as CoordinatorLayout.LayoutParams
fabLp.bottomMargin = fabOriginalBottomMargin + insets.systemWindowInsetBottom
fab.layoutParams = fabLp
insets.consumeSystemWindowInsets()
}
View lint-module-build.gradle
apply plugin: 'java-library'
apply plugin: 'kotlin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
compileOnly 'com.android.tools.lint:lint-api:26.4.1'
compileOnly 'com.android.tools.lint:lint-checks:26.4.1'
}