Android UI Utils: Enhance your app with easy-to-use utilities for managing UI elements. This library streamlines status bar customization, permission handling, activity results, media retrieval, and RecyclerView adapter creation.
To use the TUIUtils library, add the following dependency to your build.gradle
file:
// ...
dependencies {
implementation "io.github.tans5:tuiutils:1.0.1"
}
If you’re using annotations to customize the system bar, include the following code when your app launches:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
AutoApplySystemBarAnnotation.init(this)
}
}
Add the @SystemBarStyle
annotation to your Activity class for default transparent status and navigation bars.
Alternatively, use the following code when your activity is created:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Customize system bar color
systemBarColor()
// Make status bar and navigation bar light.
systemBarThemeStyle(
statusBarThemeStyle = SystemBarThemeStyle.Light,
navigationThemeStyle = SystemBarThemeStyle.Light
)
}
Add the @FullScreenStyle annotation to your Activity class.
Or use the following code when your activity is created:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fit_system_window)
// make activity full screen.
this.fullScreenStyle()
}
permissionsRequest(
permissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
error = {
},
callback = { granted: Set<String>, denied: Set<String> ->
})
permissionsRequestSimplify(
permissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
error = {
},
callback = { isAllGranted ->
}
)
launch {
runCatching {
permissionsRequestSuspend(Manifest.permission.ACCESS_FINE_LOCATION)
}.onSuccess { (granted, denied) ->
// TODO:
}.onFailure {
// TODO:
}
runCatching {
permissionsRequestSimplifySuspend(Manifest.permission.ACCESS_FINE_LOCATION)
}.onSuccess { isAllGranted ->
// TODO:
}.onFailure {
// TODO:
}
}
permissionsRequestRx3(Manifest.permission.ACCESS_FINE_LOCATION)
.subscribe({ (granted, denied) ->
// TODO:
}, { t: Throwable ->
// TODO:
}
)
permissionsRequestSimplifyRx3(Manifest.permission.ACCESS_FINE_LOCATION)
.subscribe({ isGrantAll ->
// TODO
}, { t: Throwable ->
// TODO:
})
startActivityResult(
targetActivityIntent = Intent(this, MainActivity::class.java),
error = {
// TODO:
},
callback = { resultCode, resultData ->
// TODO:
}
)
launch {
runCatching {
startActivityResultSuspend(Intent(this@MainActivity, MainActivity::class.java))
}.onSuccess { (resultCode, resultData) ->
// TODO:
}.onFailure {
// TODO:
}
}
startActivityResultRx3(Intent(this@MainActivity, MainActivity::class.java))
.subscribe({ (resultCode, resultData) ->
// TODO:
}, { e: Throwable ->
// TODO:
})
After Android 13
need permissions READ_MEDIA_AUDIO
, READ_MEDIA_VIDEO
and READ_MEDIA_IMAGE
, Before Android 13
, need permission READ_EXTERNAL_STORAGE
.
val audios = queryAudioFromMediaStore()
val images = queryImageFromMediaStore()
val videos = queryVideoFromMediaStore()
takeAPhoto(
outputFileUri = outputUri,
error = { error ->
// TODO:
},
callback = { isSuccess ->
// TODO:
})
runCatching {
takeAPhotoSuspend(outputUri)
}.onSuccess { isSuccess ->
// TODO:
}.onFailure { error ->
// TODO:
}
takeAPhotoRx3(outputUri)
.subscribe(
{ isSuccess ->
// TODO:
},
{ error ->
// TODO:
})
pickImage(
error = { error ->
// TODO:
},
callback = { imageUri ->
//TODO:
})
launch {
runCatching { pickImageSuspend() }
.onSuccess { imageUri ->
// TODO:
}
.onFailure { error ->
// TODO:
}
}
pickImageRx3()
.subscribe(
{ uri ->
// TODO:
},
{error ->
// TODO:
}
)
Base on Kotlin Coroutines.
viewBinding.fragmentActBt.clicks(
coroutineScope = this,
minInterval = 300L,
clickWorkOn = Dispatchers.IO) {
// TODO
}
viewBinding.swipeRefresh.refreshes(
coroutineScope = this,
refreshWorkOn = Dispatchers.IO) {
// TODO
}
val adapterBuilder = SimpleAdapterBuilderImpl<MediaStoreImage>(
itemViewCreator = SingleItemViewCreatorImpl(R.layout.image_item_layout),
dataSource = FlowDataSourceImpl(stateFlow.map { it.images }),
dataBinder = DataBinderImpl { data, view, _ ->
val itemViewBinding = ImageItemLayoutBinding.bind(view)
Glide.with(view)
.load(data.uri)
.into(itemViewBinding.imageIv)
}
)
viewBinding.imagesRv.adapter = adapterBuilder.build()
// Header
val headerAdapterBuilder = SimpleAdapterBuilderImpl<Unit>(
itemViewCreator = SingleItemViewCreatorImpl(R.layout.header_footer_item_layout),
dataSource = FlowDataSourceImpl(headerFooterDataFlow),
dataBinder = DataBinderImpl { _, view, _ ->
HeaderFooterItemLayoutBinding.bind(view).let { binding ->
binding.msgTv.text = "Header"
}
}
)
// Audios Content
val audiosAdapterBuilder = SimpleAdapterBuilderImpl<MediaStoreAudio>(
itemViewCreator = SingleItemViewCreatorImpl(R.layout.audio_item_layout),
dataSource = FlowDataSourceImpl(stateFlow.map { it.audios }),
dataBinder = DataBinderImpl { data, view, _ ->
val itemViewBinding = AudioItemLayoutBinding.bind(view)
itemViewBinding.musicTitleTv.text = data.title
itemViewBinding.artistAlbumTv.text = "${data.artist}-${data.album}"
view.clicks(this) {
Toast.makeText(this@AudiosFragment.requireContext(), data.title, Toast.LENGTH_SHORT).show()
}
}
)
// Footer
val footerAdapterBuilder = SimpleAdapterBuilderImpl<Unit>(
itemViewCreator = SingleItemViewCreatorImpl(R.layout.header_footer_item_layout),
dataSource = FlowDataSourceImpl(headerFooterDataFlow),
dataBinder = DataBinderImpl { _, view, _ ->
HeaderFooterItemLayoutBinding.bind(view).let { binding ->
binding.msgTv.text = "Footer"
}
}
)
// Empty
val emptyAdapterBuilder = SimpleAdapterBuilderImpl<Unit>(
itemViewCreator = SingleItemViewCreatorImpl(R.layout.empty_content_layout),
dataSource = FlowDataSourceImpl(stateFlow.map { if (it.audios.isEmpty() && it.hasLoadFirstData) listOf(Unit) else emptyList() }),
dataBinder = DataBinderImpl { _, view, _ ->
EmptyContentLayoutBinding.bind(view).let { binding ->
binding.msgTv.text = "No Audio."
}
}
)
viewBinding.audiosRv.adapter = (headerAdapterBuilder + audiosAdapterBuilder + footerAdapterBuilder + emptyAdapterBuilder).build()