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");
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"
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.
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.
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.
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.
Related
My textfield not expanding after line of words. I can't see my first line after write second line for text box. I want to expand automatically text field. My text field not creating new line after writing words. I can send my message but can't see another lines before send. My language is kotlin.
just use:
<TextView
android:id="#+id/textView"
android:layout_width="150dp"
android:layout_height="wrap_content"/>
Define input type in text field element .
android:inputType="textMultiLine" and layout height android:layout_height="wrap_content"
Still not solved then please attach your layout XML file.
If you want your textview to become flexible, you shouldn't give to it fixed size of
height.
Make its height "wrap_content" so textview's size will change according
to what you write inside it.
Just add:
android:layout_height="wrap_content"
I'd like to do textual back-and-forth interaction in an Android control. The idea is to have something like this:
This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning of the last line). The user is then free to enter text (using whatever Android input method, keyboard, etc.) but isn't allowed to change any of the output so far. Ideally, the user's input would be styled differently.
Then, as soon as newline is entered, I want the program to be notified and editing to stopped:
This is some text output by the program.
What is your name? Foo Bar
Hello, Foo Bar!
Note that this needs to be a proper control, i.e. one I can compose with other controls to make it just one part of the app's main layout.
Make a TextView and the EditText next to each other then your problem is solved and add the following line of code in EditText.
android:singleLine= 'true';
It allow only one line to be entered to the EditText. let me know whether this is what your expecting.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="What is your Name?"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView" />
</RelativeLayout>
This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning
of the last line). The user is then free to enter text (using whatever
Android input method, keyboard, etc.) but isn't allowed to change any
of the output so far. Ideally, the user's input would be styled
differently.
I would strongly recommend to rethink about your design as the same thing can be done with the help of LinearLayout,Editext,TextView with very simple and more manageable way.
I would suggest you to create a new LinearLayout(TextView + EditText) and assign the background of layout like EditText and edittext's no background.
Upon editText done, you could show a new TextView in the bottom
You need a ListView at top, to show your conversation & then below it, needs a horizontal view with a TextView (to show question) and EditText(with background transparent - to ask user to fill an answer).
I have a TextView in Android with a Text that can vary quite a bit in length. It could be a single word, it could also be more than ten full sentences.
My TextView has android:layout_width="match_parent" and android:layout_height="wrap_content". I've looked around and found a lot of ways to make it wrap over multiple lines (some requiring multiple of these):
android:scrollHorizontally="false"
android:singleLine="false"
android:ellipsize="none"
android:maxLines="10"
etc.
So my question is: Which should I use to wrap my TextView-Text in Android version 4.1+?
PS: I haven't tested any of these yet, but since I've found so many different answers on SO-questions about Text-Wrapping, I was wondering what the "best" method is (for my Android version).
I ended up using:
<TextView
...
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:scrollHorizontally="false"
android:ellipsize="end"
android:maxLines="10" />
When I looked at the singleLine in Eclipse it gave the following java-doc:
Constrains the text to a single horizontally scrolling line instead of
letting it wrap onto multiple lines, and advances focus instead of
inserting a newline when you press the enter key. * Deprecated: This
attribute is deprecated. Use "maxLines" instead to change the layout
of a static text, and use the "textMultiLine" flag in the inputType
attribute instead for editable text views (if both singleLine and
inputType are supplied, the inputType flags will override the value
of singleLine). [boolean]
So I now use maxLines="10", since I don't want an entire life-story to be inside the TextView, 10 lines should be a good maximum. I've added the ellipsize="end" to have three dots (...) at the end of the text when it has more than 10 lines. And the scrollHorizontally="false" does the trick of allowing multiple lines without a horizontal scroll-bar.
try setting this attributes:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:singleLine="false"
here
to elipsize, a neologism, means to shorten text using an ellipsis, i.e. three dots ... or more commonly ligature …, to stand in for the omitted bits.
Say original value pf text view is aaabbbccc and its fitting inside the view
start's output will be : ...bccc
end's output will be : aaab...
middle's output will be : aa...cc
marquee's out put will be : aaabbbccc auto sliding from right to left
My question may sound a bit weird but I'll try to explain it better here.
I ahve a TextView in android, at the inferior part of my activity. I want to have it limited to 2 lines, which is easily reachable by adding the following line in the TextView xml element:
android:maxLines="2"
Okay, now we've got it limited to 2 lines.
Then, in my Activity, I make:
termsandconditions = (TextView) findViewById(R.id.textView1);
termsandconditions.setText(terms);
Okay, now I've got a big string with the terms and conditions, but limited to 2 lines due to the xml attribute.
Now my question is, how can I cut it after having it limited to 2 lines, and concatenate a string with "Read more"? I don't need it to be in the same textView or whatever, I only want that it looks like:
Terms: blablablalblalbla blal blablalblalblalblalbla lalblalblalblalblalblalb lalblalblalb lalblalblalblalblalb lalblalblalb lalblalblalblalb lalb bla View More.
Thanks and I hope you can understand my problem.
You can use setEllipsize (TextUtils.TruncateAt where) method of TextView or the android:ellipsize XML attribute.
public void setEllipsize (TextUtils.TruncateAt where) Added in API
level 1
Causes words in the text that are longer than the view is wide to be
ellipsized instead of broken in the middle. You may also want to
setSingleLine() or setHorizontallyScrolling(boolean) to constrain the
text to a single line. Use null to turn off ellipsizing. If
setMaxLines(int) has been used to set two or more lines, END and
MARQUEE* are only supported (other ellipsizing types will not do
anything).
Related XML Attributes
android:ellipsize
Might be a bit hacky, but here's my suggestion:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="your_text_view"
android:text="long long long text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="link"
android:text="View More"
android:onClick="addMoreLinesAndHideThisTextView"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Some additional tweaking may be needed, but I think you got the idea.
How can I allow user to edit a TextView? Of course, I can use EditText instead, but I don't know how to customize it and also I've read in Android documentation that TextView can be editable. So I tried this:
<TextView android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80sp"
android:text="MyText"
android:editable="true"
android:singleLine="true"
android:inputType="text"
android:focusable="true"
android:clickable="true"
android:cursorVisible="true"/>
But it still looks like common TextView. Does anyone know what I have missed? Or, may be, how to customize EditText for it look like TextView: without borders and background?
I know you don't want to use an EditText but it's really easy to make it look like a TextView.
<EditText
android:id="#+id/Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</EditText>
You can also use android:background="#null".
Edit:
The TextView's editable param does make it editable (with some restrictions).
If you set android:editable="true" you can access the TextView via the D-pad, or you could add android:focusableInTouchMode="true" to be able to gain focus on touch.
The problem is you cannot modify the existing text, and you cannot move the cursor.
The text you write just gets added before the existing text.
You can make your TextView editable by adding these lines
tv.setFocusable(true);
tv.setEnabled(true);
tv.setClickable(true);
tv.setFocusableInTouchMode(true);
You can fake a editable Textview. You just have to hide the textview when you touch it (make it "clickable"), replace it with an EditText, and display it again when the edit is over.
TextView defines all capabilities found on EditText, but doesn't have built-in support to them. Some main differences on EditText:
a) Method getDefaultEditable() returns true. This is only a mark that defines this subclass as editable.
b) A movement method. Is an object that control the cursor behavior (position, backward/forward moves - that may change in some languages, etc). In opposition, TextView just returns null, because is not cursor anyway.
c) Method CharSequence getText(). TextView returns a single String for that. EditText uses a specific char sequence implementation (Editable) that represents a mutable text buffer.
Because that, we can't think about TextView like a restrained EditText. TextView sketch the editoring interface, but not implement itself.
If you need a text component that you can switch off editing sometimes, you are looking for the EditText component.
tv.setCursorVisible(true);
tv.setFocusableInTouchMode(true);
tv.requestFocus();
tv.setEnabled(true);
I finally found the solution to your problem by creating the TextView programmatically
TextView textView = new TextView(context, null, android.R.attr.editTextStyle) {
#Override
public boolean getDefaultEditable() {
return true;
}
};
OR IN XML
<TextView
android:id="#+id/textView_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:style="#android:attr/editTextStyle"
android:editable="true"/>
Enjoy!