Sunil
Sunil

Reputation: 864

How to insert text at either end of the android textview?

Is it possible to have a textview which contains some text at the leftmost side and some text at the rightmost side of the textview.
For Example: I want to have a textview which should appear like this
/////////////////
text         a
////////////////

Upvotes: 2

Views: 2003

Answers (3)

Simon
Simon

Reputation: 14472

textView.setText("text" + spaces(10) + "a");

String spaces(int numberOfSpaces){

    if (numberOfSpaces < 1){return "";}

    StringBuilder sb = new StringBuilder(" ");

    for (int i=1;i<numberOfSpaces;i++){
       sb.append(" ");
    }

    return sb.toString();

}

Upvotes: 0

Antrromet
Antrromet

Reputation: 15414

It is possible using a single TextView. Just put the text in HTML format like

  <string name="my_string">
<![CDATA[
    <!DOCTYPE html>
    <html>
    <body>

    <h1><span>Sunil bn</span><span style="float:right">Your Text</span></h1>
    </body>
    </html>
]]>
    </string>

in the strings file. Suppose the name of the string is "my_string". Then in your code set the text on the TextView using

myTextView.setText(Html.fromHtml(getResources()
                    .getString(R.string.my_string))); 

Upvotes: 2

MAC
MAC

Reputation: 15847

No its not possible with single TextView.

You can try textview.setText("test _____ "+"a"); but it will not work perfectly.

But you can take two TextView then you can achieve this.

Upvotes: 1

Related Questions