:bella: A lightweight top alert, fully customizable with action and auto dismiss.
Add below codes to your root build.gradle
file (not your module build.gradle file).
allprojects {
repositories {
jcenter()
}
}
And add a dependency code to your module's build.gradle
file.
dependencies {
implementation "com.github.elbehiry:bella:0.2.0"
}
Here is a basic example of implementing Bella alert text using Bella.Builder
class.
Bella bella = new Bella.Builder(context)
.setText("you can add any content...")
.setTextColor(ContextCompat.getColor(context, R.color.white))
.setTextSize(15f)
.setTextIsHtml(false)
.setButtonText("retry")
.setOnBellaButtonClickListener(view -> {
//action here
})
.setPadding(6f)
.build();
This is how to create Bella
instance using kotlin dsl.
val bella = createBella(context){
setText("you can add any content...")
setTextColor(ContextCompat.getColor(context, R.color.white))
setTextSize(15f)
setTextIsHtml(false)
setButtonText("retry")
setOnBellaButtonClickListener{
//action here
}
setPadding(6)
}
This is how to show bella alert view and dismiss.
bella.showAlignedWithParent(parent: ViewGroup) // shows the bella using [ViewGroup].
We can dismiss popup simply using bella.dismiss()
method.
bella.dismiss()
We can dismiss automatically some milliseconds later when the alert is shown using
setAutoDismissDuration
method on Bella.Builder
.
Bella.Builder(context)
// dismisses automatically 1000 milliseconds later when the popup is shown.
.setAutoDismissDuration(1000L)
...
easy way to create bella view.
Bella.make(showAlert, "something went wrong", Duration.LENGTH_LONG).show()
We can customize the text on the bella alert.
.setText("You can edit your profile now!")
.setTextSize(15f)
.setTextTypeface(Typeface.BOLD)
.setTextColor(ContextCompat.getColor(context, R.color.white_87))
If your text has HTML in it, you can enable HTML rendering by adding this:
.setTextIsHtml(true)
This will parse the text using Html.fromHtml(text)
.
TextFrom is an attribute class that has some attributes about TextView for customizing popup text.
TextForm textForm = new TextForm.Builder(context)
.setText("This is a TextForm")
.setTextColorResource(R.color.colorPrimary)
.setTextSize(14f)
.setTextTypeface(Typeface.BOLD)
.build();
builder.setTextForm(textForm);
This is how to create TextForm
using kotlin dsl.
val form = textForm(context) {
text = "This is a TextForm"
textColor = ContextCompat.getColor(context, R.color.white)
textSize = 14f
textTypeface = Typeface.BOLD
}
We can listen to the bella alert button is clicked, alert is dismissed using listeners.
.setOnBellaClickListener { //action }
.setOnBellaDismissListener { //action }
.setOnBellaShownListener { //action }
.setOnBellaButtonClickListener { //action }
Just use setLifecycleOwner
method. Then dismiss
method will be called automatically before activity or fragment would be destroyed.
.setLifecycleOwner(lifecycleOwner)
We should create a class which extends Bella.Factory
.
An implementation class of the factory must have a default(non-argument) constructor.
HomeAlertFactory.kt
class HomeAlertFactory : Bella.Factory() {
override fun create(context: Context, lifecycleOwner: LifecycleOwner): Bella {
return Bella.Builder(context)
.setButtonVisible(true)
.setButtonTextColorResource(R.color.action_button_color)
.setButtonTextSize(12f)
.setButtonText(context.getString(R.string.retry))
.setTextResource(R.string.alert_content)
.setTextSize(12f)
.setLifecycleOwner(lifecycleOwner)
.setAutoDismissDuration(BellaViewDuration)
.setBackgroundColorResource(R.color.error_red)
.build()
}
}
Copyright 2020 elbehiry
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.