Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: is there a way to apply a right-margin span within an edittext?

I'm looking for something similar to LeadingMarginSpan, but capable of applying both left- and right- margins to the text, as opposed to just a left margin.

What I'm trying to do is have the option of making some paragraphs narrower than others within the same edittext, so it might looks something like this:

blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah
blah blah

     blah blah blah blah blah blah
     blah blah blah blah blah blah
     blah blah blah blah blah blah
     blah

blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah
blah blah blah blah blah

As far as I know there are no pre-defined spans that do this, and I don't know how to go about creating a new span from scratch that could do it. Any help would be greatly appreciated!

like image 502
Andy Avatar asked Mar 22 '12 19:03

Andy


People also ask

What is margin in Android with example?

In order to style and positioned user interface elements, we use standard attributes in android known as Margin and Padding. In this article, all the confusion about Margin is explained with examples. Margin specifies an extra space outside that View on which we applied Margin. In simple words, Margin means to push outside.

What is a span type in Android text?

Android provides over 20 span types in the android.text.style package. Android categorizes spans in two primary ways: How the span affects text: a span can affect either text appearance or text metrics. Span scope: some spans can be applied to individual characters, while others must be applied to an entire paragraph. Figure 4.

What is edittext in Android?

In Android, EditText is a standard entry widget in android apps. It is an overlay over TextView that configures itself to be editable. EditText is a subclass of TextView with text editing operations. We often use EditText in our applications in order to provide an input or text field, especially in forms.

How to set the size of text of a edit text?

7. textSize: textSize attribute is used to set the size of text of a edit text. We can set the text size in sp (scale independent pixel) or dp (density pixel). Below is the example code in which we set the 25sp size for the text of a edit text.


2 Answers

You can't set right padding/margin with span. But you can cheat with MetricAffectingSpan. It has two methods: updateMeasureState (called when measuring text) and updateDrawState (called when drawing text).

So, if you increase textScaleX at measuring, android will make shorter lines. If you don't increase textScaleX when drawing, android will not scale lines when drawing. As a result, you'll get shorter lines. That will look like right padding.

It's not perfect: you can't set right padding in pixels, but you will have some padding on right.

Here is 5% right padding example.

public class RoughtRightPaddingSpan extends MetricAffectingSpan {

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTextScaleX(1.05f);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
    }
}
like image 191
babay Avatar answered Sep 26 '22 16:09

babay


As far as I know there are no pre-defined spans that do this

AFAIK, you are correct.

and I don't know how to go about creating a new span from scratch that could do it.

Well, support for LeadingMarginSpan is baked into Layout and StaticLayout, so simply creating a TrailingMarginSpan will be insufficient. You would have to create your own custom subclasses of Layout and StaticLayout, overriding and cloning their very complicated draw() methods, and pour in your TrailingMarginSupport.

In short, this will be painful.

If you happen to be making your own firmware, of course, this becomes much simpler... :-)

like image 32
CommonsWare Avatar answered Sep 22 '22 16:09

CommonsWare