A tool to limit the amount of boilerplate with Android's View Binding.
Developed by Patches 07/2021 - present
Supports Android SDK levels 18-30
Compiled with Java8
Make sure you have the appropriate repository accessible:
For stable releases and pre-releases:
repositories {
mavenCentral()
}
For Snapshots:
maven("https://oss.sonatype.org/content/repositories/snapshots")
and then add it as a dependency. Please see the release page for the latest version.
For Gradle:
implementation 'com.isupatches.android:viewglu:<LATEST_VERSION>'
For Maven:
<dependency>
<groupId>com.isupatches.android</groupId>
<artifactId>viewglu</artifactId>
<version>LATEST_VERSION</version>
<type>pom</type>
</dependency>
You may also download the @aar from the release page and import it into your project manually.
There is only one prerequisite for this library and that is that viewBinding
must be enabled:
buildFeatures {
viewBinding = true
}
The Binding
class for an activity can be attached in a simple one-liner that leverages the layout inflater from the activity:
internal class MainActivity : BaseActivity() {
override val binding: ActivityMainBinding by paste(ActivityMainBinding::inflate)
}
The activity is then responsible for setting the content view with the bindings root:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
See the MainActivity for a full example.
Fragments are slightly different so that memory constraints and leaks are prevented.
Option 1: Via binding an already inflated view
internal class FragmentBound : BaseFragmentWithLayout(R.layout.fragment_with_text) {
override val binding: FragmentWithTextBinding by paste(FragmentWithTextBinding::bind)
}
See the FragmentBound for a full example.
Option 2: Via manual inflation
internal class FragmentInflated : BaseFragment() {
private var binding: FragmentWithTextBinding by paste()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentWithTextBinding.inflate(inflater, container, false)
return binding.root
}
}
See the FragmentInflated for a full example.
In both cases, ViewGlu will automagically detach the binding during the Fragment's
onDestroy
Please checkout the CHANGELOG and the release notes for details on what's been updated and improved.
To provide the libraries functionality:
- androidx.appcompat:appcompat
- androidx.lifecycle:lifecycle-runtime
- androidx.lifecycle:lifecycle-compiler
- org.jetbrains.kotlin:kotlin-stdlib
For static analysis:
For code coverage:
For publishing:
For documentation
For the sample app:
Copyright 2021 Patches Klinefelter
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 distributed 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 and limitations under the License.