I'm writing a simple caesar-encryption-activity. Two EditTexts on screen, one clear-text, one crypted. Here's an example for the crypted EditText - the cleartext one is similar.
<EditText
android:layout_below="#id/Caesar_Label_CryptText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/Caesar_Text_CryptText"
android:hint="Enter crypted text"
android:maxLines="2"
android:lines="2"
android:minLines="2"
android:inputType="text|textMultiLine|textVisiblePassword"
android:scrollbars="vertical"
android:gravity="top" />
Now when entering cleartext I have an TextChangedListener running that programatically crypts and fills that crypto-EditText. So far, so good.
When the cleartext entered gets really long, the cleartext-EditText scrolls with my imput, but the crypto-EditText stays at the top of the text. I'd really like the crypto-EditText to scroll so that it always shows the bottom line of its content.
How can that be done, preferably from the onTextChanged()-Method of the TextWatcher?
Ok, found it. It was the cursor (called Selection on EditText and TextViews).
This is how I got it to work:
ivClear // assigned the EditText that has the input
ivCrypt // assigned the target EditText, that I want to scroll
aText // the input from ivClear, crypted
Then use:
ivCrypt.setText(aText); // assign the Text
ivCrypt.setSelection(ivClear.getSelectionStart()); // scroll
Phew, finally :) Always underestimated the power of the Spannable ;)
The base class android.view.View has methods getScrollX(), getScrollY() and scrollTo() that may be helpful, though I haven't tried it.
http://developer.android.com/reference/android/view/View.html#scrollTo(int, int)
Related
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
I'm trying to implement something like a text editor.
I want to have a possibility to edit a long string without word-wrapping.
The edited string should be scrolled horizontally.
New line should be entered only when I press Enter.
So, my editor should be able to edit as long string as user desires (with horizontal scrolling) and be simultaneously multi-lined.
<EditText
android:id="#+id/editor"
android:background="#android:color/transparent"
android:scrollHorizontally="true"
android:lineSpacingExtra = "3sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:typeface="monospace"
android:textSize="16sp"
android:inputType="textMultiLine"
android:scrollbars="vertical|horizontal"
android:gravity="left|top"
android:text="Code editor"
tools:ignore="HardcodedText"/>
In this case long strings are word-wrapped.
And
android:inputType="text"
gives me a possibility to edit long strings in one line... but how to enter a new line?
Pressing [Enter] doesn't enter a new string, just the next focusable element is selected.
I tried all combinations that could imagine, but nothing worked properly.
Either the text is multi-lined with word-wrapping or it's presented in one wide line.
I wondering if it's possible at all, to get such desired behaviour.
Now I'm thinking about subclassing EditText and manually handling Enter pressing.
But I don't like this approach.
How to get horizontally scrollable EditText with 'multiline' feature?
Any ideas?
You can combine the MUTILINE input type and the ability to scroll horizontally:
editText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setHorizontallyScrolling(true);
or in xml :
<EditText
android:inputType="text|textMultiLine"
android:scrollHorizontally="true"
.../>
I have a HorizontalScrollView, in which I have an EditText. If I don't set any input type, it works just as expected. However, if I use TYPE_CLASS_NUMBER, it scrolls to the right end when I select it. Why? How to fix this problem?
Text input has a left gravity/justification and number input has a right gravity/justification.
You can override this either in your layout xml or programmatically.
<EditText
android:id="#+id/etNumbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:inputType="number"
android:hint="#string/et_number_hint"/>
or
EditText etNnumber = (EditText) findViewById(R.id.etNumbers);
etNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
etNumber.setGravity(Gravity.LEFT);
Post your existing implementation and I can edit this with an actual code solution.
I put a multiline EditText in my application. The problem is that if the user presses enter, the EditText often collides with the icons in my application. I want to avoid that.
I wanted to know how to make it so that the user can only press enter 5 times and no more?
To restrict the height of an EditText, you can use the maxLines attribute
<EditText
...
android:maxLines="5"
...
/>
<EditText
android:id="#+id/textbox"
android:singleLine="true"
android:scrollbars="horizontal"
android:scrollHorizontally="true" />
((EditText)findViewById(R.id.display)).setKeyListener(null);
the EditText serves as some kind of log box, text is appended to it at some events.
I need the EditText to be scrolled to the right at all times, I mean the scrollbar should be positioned to the maximum right and so the most recent text is visible.
I cant seem to get it done, not sure how to read the current position of the scrollbar and adjust it accordingly.
I've tried playing with EditText.setScrollX() though even setScrollX(10000) didnt have any effect.
try with
editText.setSelection(editText.getText().length()-1);