Difference between setText() and append()
Asked Answered
B

5

11

I'm curious about the difference setText() and append() are creating. I'm writing a very basic editor with line numbers. I have a TextView to hold line numbers on the left, paired with an EditText on the right to hold the data. Here's the XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="top">
    <TextView
        android:id="@+id/line_numbers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="0dip"
        android:gravity="top"
        android:textSize="14sp"
        android:textColor="#000000"
        android:typeface="monospace"
        android:paddingLeft="0dp"/>
    <EditText
        android:id="@+id/editor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text|textMultiLine|textNoSuggestions"
        android:imeOptions="actionNone"
        android:gravity="top"
        android:textSize="14sp"
        android:textColor="#000000"
        android:typeface="monospace"/>
</LinearLayout>

Ignoring some of the other things I'm doing, the most curious thing I came across was the extra spacing that showed up when I used append() (assuming things have been initialized and all that).

This below, in combination with the XML, sets a flush border between the TextView and EditText.

theEditor = (EditText) findViewById(R.id.editor);
lineNumbers = (TextView) findViewById(R.id.line_numbers);
theLineCount = theEditor.getLineCount();
lineNumbers.setText(String.valueOf(theLineCount)+"\n");

Change the last line to this, though, and suddenly each line in the TextView has padding on the right before the EditText.

lineNumbers.append(String.valueOf(theLineCount)+"\n");

It's not the end of the world. but I was curious what was causing this behavior. Since I'm new to the language, the only thing I could think of was maybe, when append throws the Editable on there, it adds the padding. If I can get an answer, I get to replace all of these nasty lines with simpler appends:

lineNumbers.setText(lineNumbers.getText().toString()+String.valueOf(newLineCount)+"\n");
Bessiebessy answered 14/10, 2013 at 5:30 Comment(0)
H
2

I think changing BufferType to EDITABLE by append method caused the unexpected padding. If you want to use append method instead of setText method and remove that padding,

you can try to remove it by using

textView.setincludeFontPadding(false)

or adding this line to your textview in your xml file

android:includeFontPadding="false"

Hope this helps.

Honorable answered 14/10, 2013 at 9:51 Comment(0)
P
15
lineNumbers.setText("It is test,");

//Here lineNumbers have It is test

lineNumbers will have "It is test,". After that, if you use setText again, text will completely change

lineNumbers.setText("It is second test,");

//Here you'll lose first text and lineNumbers text will be "It is second test,"

After that, if you use append, lets see what will happen..

lineNumbers.append("It is third test,");

// Here you will not lose lineNumbers text.. It will be like this "It is second test,It is third test"

Pellagra answered 14/10, 2013 at 6:16 Comment(0)
T
6

setText(): Destroys the buffer content by filling the text to be set. append(): Adds a text to a buffer and then prints the result.

Example: example.setText("Hello"); would print Hello on the output screen. If you then execute example.append("World"); you would get HelloWorld as the output.

Trinetta answered 14/10, 2013 at 6:9 Comment(1)
Not addressing the content of the question in any way (maybe the title, but not the description of the exact problem)Donall
W
4

setText will replace the existing text with new text.

From Android doc:
Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.

append will keep the old text and add the new one more like concatenating.

From Android Doc
Convenience method: Append the specified text to the TextView's display buffer, upgrading it to BufferType.EDITABLE if it was not already editable.

Whiff answered 14/10, 2013 at 6:7 Comment(0)
H
2

I think changing BufferType to EDITABLE by append method caused the unexpected padding. If you want to use append method instead of setText method and remove that padding,

you can try to remove it by using

textView.setincludeFontPadding(false)

or adding this line to your textview in your xml file

android:includeFontPadding="false"

Hope this helps.

Honorable answered 14/10, 2013 at 9:51 Comment(0)
E
1

The basic difference is that setText() replaces all the text from the existing one and append() adds your new value to existing one. Hope i helped.

Eskilstuna answered 30/3, 2018 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.