Skip to content

Instantly share code, notes, and snippets.

View Zhuinden's full-sized avatar
🤔
Fighting Proguard

Gabor Varadi Zhuinden

🤔
Fighting Proguard
View GitHub Profile
@Zhuinden
Zhuinden / BasicTextFieldWithCursorAtEnd.kt
Last active May 7, 2024 20:37
A Compose TextField that initializes the cursor position at the end of the text, rather than the start, when focused without tapping the TextField manually.
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@Zhuinden
Zhuinden / DynamicFragmentPagerAdapter.java
Last active April 26, 2024 09:32
Dynamic FragmentPagerAdapter
public class DynamicFragmentPagerAdapter extends PagerAdapter {
private static final String TAG = "DynamicFragmentPagerAdapter";
private final FragmentManager fragmentManager;
public static abstract class FragmentIdentifier implements Parcelable { //should be concrete children with @Parcelize if possible, don't forget CREATOR field
private final String fragmentTag;
private final Bundle args;
public FragmentIdentifier(@NonNull String fragmentTag, @Nullable Bundle args) {
@Zhuinden
Zhuinden / FragmentViewBindingDelegate.kt
Last active February 24, 2024 20:13
Fragment view binding delegate
// https://github.com/Zhuinden/fragmentviewbindingdelegate-kt
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
@Zhuinden
Zhuinden / MainActivity.kt
Last active December 30, 2022 09:40
Fragment multiple tabs without remove or replace
class MainActivity : AppCompatActivity() {
private lateinit var swipeFragment: SwipeFragment
private lateinit var favoritesFragment: FavoritesFragment
private lateinit var newsFragment: NewsFragment
private val fragments: Array<out Fragment> get() = arrayOf(swipeFragment, favoritesFragment, newsFragment)
private fun selectFragment(selectedFragment: Fragment) {
var transaction = supportFragmentManager.beginTransaction()
fragments.forEachIndexed { index, fragment ->
@Zhuinden
Zhuinden / CustomFragmentContainerView.java
Last active November 28, 2022 08:48
CustomFragmentContainerView
import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.widget.FrameLayout;
@Zhuinden
Zhuinden / MyFragment.java
Last active September 10, 2022 11:02
Nested child fragments for bottom navigation view in Java
public class MyFragment
extends Fragment {
public MyFragment() {
super(R.layout.my_fragment);
}
private int selectedIndex = 0;
private FirstFragment firstFragment;
@Zhuinden
Zhuinden / NavigationDispatcher.kt
Created June 19, 2020 00:48
Hilt NavigationDispatcher
@ActivityRetainedScoped
class NavigationDispatcher @Inject constructor() {
private val navigationEmitter: EventEmitter<NavigationCommand> = EventEmitter()
val navigationCommands: EventSource<NavigationCommand> = navigationEmitter
fun emit(navigationCommand: NavigationCommand) {
navigationEmitter.emit(navigationCommand)
}
}
@Zhuinden
Zhuinden / ByteArrayConverterFactory.java
Created January 22, 2022 16:56
ByteArrayConverterFactory
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.annotation.Nullable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.zhuinden.testingapp"
minSdkVersion 14
targetSdkVersion 24
@Zhuinden
Zhuinden / ApplicationComponent.kt
Last active September 11, 2021 06:25
Dagger + ViewModel + SavedStateHandle
@Singleton
@Component(modules = [AssistedInjectionModule::class])
interface ApplicationComponent {
fun mySavedStateViewModelFactory(): MySavedStateViewModel.Factory
}