Coder Social home page Coder Social logo

bella's Introduction

Bella (not maintained anymore...)


:bella: A lightweight top alert, fully customizable with action and auto dismiss.


License API


Including in your project

Download

Gradle

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"
}

Usage

Basic Example for Java

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();

Create using kotlin dsl

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)
        }

Show and dismiss

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)
   ...

Example Two

easy way to create bella view.

Bella.make(showAlert, "something went wrong", Duration.LENGTH_LONG).show()

Text Composition

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).

TextForm

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
}

OnBellaClickListener, OnBellaDismissListener, OnBellaButtonClickListener

We can listen to the bella alert button is clicked, alert is dismissed using listeners.

.setOnBellaClickListener { //action }
.setOnBellaDismissListener { //action }
.setOnBellaShownListener { //action }
.setOnBellaButtonClickListener { //action }

Avoid Memory leak

Just use setLifecycleOwner method. Then dismiss method will be called automatically before activity or fragment would be destroyed.

.setLifecycleOwner(lifecycleOwner)

Lazy initialization

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()
    }
}

License

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.

bella's People

Contributors

elbehiry avatar melbehiry avatar

Stargazers

Asmaa Mohamed avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.