Skip to content

BindingDelegate is a simplify usage of Android View Binding built by Jonathan Lee utilising Kotlin Property Delegate. Initialise your view binding using just one line of code.

License

Notifications You must be signed in to change notification settings

jonathanlee06/BindingDelegate

Repository files navigation

BindingDelegate 📌

License API Jitpack

BindingDelegate is a simplify usage of Android View Binding built by Jonathan Lee utilising Kotlin Property Delegate. Initialise your view binding using just one line of code.

Made with ❤️ in Malaysia

✏️ Getting Started

1. Add JitPack repository

Add the repository in your root build.gradle file under repositories section:

  • Kotlin DSL
allprojects {
    repositories {
        ...
        maven(url = "https://jitpack.io")
    }
}
  • Groovy
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

2. Add the library dependency

  • Kotlin DSL
dependencies {
    implementation("com.github.jonathanlee06:BindingDelegate:1.0.3")
}
  • Groovy
dependencies {
    implementation 'com.github.jonathanlee06:BindingDelegate:1.0.3'
}

3. Enable ViewBinding

https://developer.android.com/topic/libraries/view-binding#setup

🔎 Usage

1. Activity (with reflection) See example

class MainActivity : AppCompatActivity() {
    private val binding: ActivityMainBinding by viewBinding()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.button.setOnClickListener {
            val intent = Intent(this, FragmentActivity::class.java).also {
                it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            }
            startActivity(intent)
        }
    }
}

2. Activity (without reflection): Pass ::inflate method reference See example

class FragmentActivity : AppCompatActivity() {
    private val binding by viewBinding(ActivityFragmentBinding::inflate)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setSupportActionBar(binding.toolbar)
    }
}

3. Fragment (with reflection) See example

class FirstFragment : Fragment() {

    private val binding: FragmentFirstBinding by viewBinding()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = inflater.inflate(R.layout.fragment_first, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.buttonFirst.setOnClickListener {
            findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }
    }
}

4. Fragment (without reflection): Pass ::bind method reference See example

class SecondFragment : Fragment(R.layout.fragment_second) {

    private val binding by viewBinding(FragmentSecondBinding::bind)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.buttonSecond.setOnClickListener {
            findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
        }
    }
}

📓 Note

Proguard

If there is any problem with Proguard, add below to your app/proguard-rules.pro:

# BindingDelegate uses Reflection.
-keepclassmembers class ** implements androidx.viewbinding.ViewBinding {
    public static ** bind(android.view.View);
    public static ** inflate(android.view.LayoutInflater, android.view.ViewGroup, boolean);
    public static ** inflate(android.view.LayoutInflater, android.view.ViewGroup);
}

❤️ See anything you like?

Support it by joining stargazers for this repository. ⭐

:octocat: Author

Jonathan Lee

📑 License

This project is licensed under the Apache License, Version 2.0 . See the LICENSE file for more info.

Designed and developed by 2022 Jonathan Lee

    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, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

About

BindingDelegate is a simplify usage of Android View Binding built by Jonathan Lee utilising Kotlin Property Delegate. Initialise your view binding using just one line of code.

Topics

Resources

License

Stars

Watchers

Forks

Languages