Skip to content

Instantly share code, notes, and snippets.

View saurabharora90's full-sized avatar

Saurabh saurabharora90

View GitHub Profile
@saurabharora90
saurabharora90 / Placeholder.java
Created January 10, 2018 08:33
Performance at Viki
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);
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 February 17, 2018 12:35
Primer on Styles and Themes
<!-- 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 February 17, 2018 17:19
Primer on Styles and Themes - Seekbar theme
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
@Override
protected void onDestroy() {
super.onDestroy();
customTabActivityHelper.setConnectionCallback(null);
}
@Override
protected void onStart() {
super.onStart();
customTabActivityHelper.bindCustomTabsService(this);
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 September 30, 2018 07:56
Motion Layout Example - Building the example shown at Google IO: https://youtu.be/ytZteMo4ETk?t=31m59s
<?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 / FullScreenPendingIntent.kt
Created July 9, 2019 16:53
Beginning with Android Q, Background Activity Starts should be done by notification triggered activities.
val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
....
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(fullScreenPendingIntent, true)
@saurabharora90
saurabharora90 / BGLocation.kt
Last active July 10, 2019 16:01
Request access to location while app is in background,
<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 July 10, 2019 17:16
Move FAB above so that it doesn't conflict with transparent floating navigation bar
root.setOnApplyWindowInsetsListener { _, insets ->
val fabLp = fab.layoutParams as CoordinatorLayout.LayoutParams
fabLp.bottomMargin = fabOriginalBottomMargin + insets.systemWindowInsetBottom
fab.layoutParams = fabLp
insets.consumeSystemWindowInsets()
}