A simpler way to style your TextViews

Overview

BabushkaText

BabushkaText is a custom TextView which lets you customize the styling of parts of your text via Spannables, but without the hassle of having to deal directly with Spannable themselves.

  • Create complex style structures for your text without having to add multiple TextViews to your view hierarchy.
  • Eliminate setSpan(Object what, int start, int end, int flags) calls by using more convenient methods such as .style(Typeface.BOLD), .superscript() and others.

What can I do with BabushkaText?

Examples of different styles. Each one is a single TextView.

Logo

Example

Generate the following style:

NewY York

With the code below:

        BabushkaText babushka = (BabushkaText)findViewById(R.id.babushka_text);

        // Add the first piece "Central Park"
        babushka.addPiece(new BabushkaText.Piece.Builder("Central Park, NY\n")
                .textColor(Color.parseColor("#414141"))
                .build());

        // Add the second piece "1.2 mi"
        babushka.addPiece(new BabushkaText.Piece.Builder("1.2 mi ")
                .textColor(Color.parseColor("#0081E2"))
                .textSizeRelative(0.9f)
                .build());

        // Add the third piece "from here"
        babushka.addPiece(new BabushkaText.Piece.Builder("from here")
                .textColor(Color.parseColor("#969696"))
                .textSizeRelative(0.9f)
                .build());

        // Display the final, styled text
        babushka.display();

What kind of styles can I add?

Right now you can customize the style of your text by using the following methods, which internally get converted to the corresponding Spannables

Babushka Method Internal Span
textSize AbsoluteSizeSpan
textColor ForegroundColorSpan
textSizeRelative RelativeSizeSpan
backgroundColor BackgroundColorSpan
style StyleSpan
underline UnderlineSpan
strike StrikethroughSpan
superscript SuperscriptSpan
subscript SubscriptSpan

What else can I do?

Once you've created and displayed your BabushkaText, you can modify the text contents of each Piece individually. For example, if we want to change the 1.2 mi from the above example into 1.9 km, we could do the following:

// grab the Piece at position 1
Piece piece = babushka.getPiece(1);

// modify it's text
piece.setText("1.9 km ");

// you must always call display after you alter a Piece's text
babushka.display();

Download

BabushkaText is a single Java file, so you can simply download it and reference it in your project.

Download BabushkaText.java here: BabushkaText

Or if you prefer, you can simply download the JAR

Contact Me

If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request :)).

License

Copyright 2014 Henrique Boregio

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.
Comments
  • How to use it in xml eclypse file ?

    How to use it in xml eclypse file ?

    Hi, I was looking for your amazing Babushka Text, I implemented it on my java class file, all ok, but I need to link it was Resource (you said : BabushkaText babushka = (BabushkaText)findViewById(R.id.babushka_text);), and when I do it (for me : BabushkaText text = (BabushkaText)findViewById(R.id.text_preview);) where text_preview is a TextView, there's an error (normal :D) ! So, how could I implement the Babushka on XML file ? Thank you, and, have a good day ! PS : My text_preview on XML is :+1:

    opened by OwnHno 3
  • set text color has no effect

    set text color has no effect

    For example:

    bt.addPiece(new BabushkaText.Piece.Builder("please\n")
                .style(Typeface.BOLD)
                .textSize(35)
                .build());
    bt.addPiece(new BabushkaText.Piece.Builder("change textcolor")
                .textSize(20)
                .build());
    bt.display();
    

    I get textview with black text colour (default color) Then I call

    bt.setTextColor(getResources().getColor(R.color.white));
    

    And textColor remains black. I understand what there is no built-in ability to change spannable text colour on the fly, but it's would be very helpful function.

    enhancement 
    opened by IlyaEremin 1
  • List Adapter problem

    List Adapter problem

    Hi Thank for your effort. I have a question about this class.

    When i use this in the list view..

    AirPortName.addPiece(new BabushkaText.Piece.Builder(listData.get(position).getAirPortName()) .backgroundColor(Color.parseColor("#DFF1FE")) .textColor(Color.parseColor("#073680")) .style(Typeface.BOLD) .build()); AirPortName.display();

    this is my code.

    But problem is ..duplicated the text when i scroll up and down.

    so..I try this code

    AirPortName = (BabushkaText)row.findViewById(R.id.title); AirPortName.reset(); AirPortName.addPiece(new BabushkaText.Piece.Builder(listData.get(position).getAirPortName()) .backgroundColor(Color.parseColor("#DFF1FE")) .textColor(Color.parseColor("#073680")) .style(Typeface.BOLD) .build()); AirPortName.display();

    Use this... AirPortName.reset();

    is it right?

    Thanks a lot.

    opened by sangrime 1
  • i have a problem..

    i have a problem..

    i want to use babushktext, so i copy .java code. but i have a problem with my code... I'd really appreciate it if you help me ------------solve it kkk.... how do i delete this post-------- 2017-04-01_13-01-13

    opened by hyekie 1
  • 修改了 DEFAULT_ABSOLUTE_TEXT_SIZE 静态变态对别的textview的影响

    修改了 DEFAULT_ABSOLUTE_TEXT_SIZE 静态变态对别的textview的影响

    if (aPiece.textSize==0)

    •    {
      
    •        aPiece.textSize = DEFAULT_ABSOLUTE_TEXT_SIZE;
      
    •    }
      
    •    finalString.setSpan(new AbsoluteSizeSpan(aPiece.textSize), start, end, Spannable
      
    •            .SPAN_EXCLUSIVE_EXCLUSIVE);
      
    opened by Aarthas 0
Owner
Henrique Boregio
CTO @ Primephonic
Henrique Boregio
A tool that facilitates working with Spans on TextViews or any extension of them (EditTexts, Buttons...).

AwesomeText Working with Spans is ugly and difficult to manage, it's like working with a goblin of Moria. Don't understand the Spans? With AwesomeText

José Manuel Pereira García 674 Nov 27, 2022
Gmail style MultiAutoCompleteTextView for Android

Version 3.0 The 3.0.1 version is now available! This should resolve a number of text handling issues and lay the groundwork for better support of mixe

Splitwise 1.3k Nov 14, 2022
A material style input for codes

Material Code input A material style input for put codes Based on Code input field concept by SAMUEL KANTALA How to use Minimal SDK Version 11 Usage w

Adrián Lomas 962 Nov 26, 2022
A little animation framework which could help you to show message in a nice looking way

TextSurface A little animation framework which could help you to show message in a nice looking way. Usage Create TextSurface instance or add it in yo

Eugene Levenetc 2.3k Dec 30, 2022
Simple way to create linked text, such as @username or #hashtag, in Android TextView and EditText

Simple Linkable Text Simple way to create link text, such as @username or #hashtag, in Android TextView and EditText Installation Gradle Add dependenc

Aditya Pradana Sugiarto 76 Nov 29, 2022
Dealing with Android Text by simple way to get high performance.

Gapo Android RichText RichText supports Hashtag, Mention, Url, Phone Number, Email, Markdown, Custom Span, SeeMore/SeeLess by limited line or length,

GapoWork 8 Oct 20, 2021
Dealing with Android Text by simple way to get high performance.

Gapo Android RichText RichText supports Hashtag, Mention, Url, Phone Number, Email, Markdown, Custom Span, SeeMore/SeeLess by limited line or length,

GapoWork 8 Oct 20, 2021
An extension of Android's TextView, EditText and Button that let's you use the font of your choice

AnyTextView (deprecated) Note: AnyTextView is no longer being maintained. I recommend replacing AnyTextView with the Calligraphy library instead. Frus

Hans Petter Eide 165 Nov 11, 2022
💰 A library to dynamically format your EditTexts to take currency inputs

CurrencyEditText A library to dynamically format your EditTexts to take currency inputs. Gradle Dependency Add the dependency to your app's build.grad

Cotta & Cush Limited 115 Dec 28, 2022
An easy approach on how to create your country code picker for the edit text.

Country-Code-Picker Original Article on Dev.to Click below ?? App's Overview In this article, I am going to demonstrate how to create a simple country

Siddharth Singh 5 Mar 10, 2022
A simpler way to style your TextViews

BabushkaText BabushkaText is a custom TextView which lets you customize the styling of parts of your text via Spannables, but without the hassle of ha

Henrique Boregio 752 Dec 29, 2022
A simpler way to style your TextViews

BabushkaText BabushkaText is a custom TextView which lets you customize the styling of parts of your text via Spannables, but without the hassle of ha

Henrique Boregio 753 Jun 7, 2022
Android Library that provide simpler way to achieve modularity

ToyBricks 中文文档 ToyBricks简介以及原理分析 ToyBricks用户手册 Introduction Android Library that provide simpler way to achieve modularity. Compile System requirement

null 33 Nov 14, 2022
Binding your extras more easier, more simpler for your Android project

Ktan Ktan make your intent / arguments more easier and readable. And most important, will help you bind all extras for your activity / fragment. And a

Ade Fruandta 3 Jan 7, 2023
Custom font library for android | Library to change/add font of Entire Android Application at once without wasting your time - TextViews, EditText, Buttons, Views etc.,

AppFontChanger In a Single shot change font of Entire Android Application - TextViews, EditText, Buttons, Views etc., Kindly use the following links t

Prabhakar Thota 48 Aug 10, 2022
[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).

Simple Stack Why do I want this? To make navigation to another screen as simple as backstack.goTo(SomeScreen()), and going back as simple as backstack

Gabor Varadi 1.3k Jan 2, 2023
A more lightweight and simpler to use MVVM architecture for Android.

A more lightweight and simpler to use MVVM architecture for Android, Data and views are two-way bound.

fangbing chen 7 Jul 5, 2022
****. Use the native and support library variants instead - https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html. An android library that makes it easy to add custom fonts to edittexts and textviews

Add to your project Add this line to your dependencies in build.gradle compile 'in.workarounds.typography:typography:0.0.8' Using the views There are

Workarounds 43 Nov 6, 2021
A tool that facilitates working with Spans on TextViews or any extension of them (EditTexts, Buttons...).

AwesomeText Working with Spans is ugly and difficult to manage, it's like working with a goblin of Moria. Don't understand the Spans? With AwesomeText

José Manuel Pereira García 674 Nov 27, 2022
🪄 This is an animation library with an increasing number of TextViews

CountNumberEvent ?? This is an animation library with an increasing number of TextViews Demo Setup Add it in your root build.gradle at the end of repo

박상선 5 Feb 17, 2022