Android EditText does not show all text - java

I have a code which retrieves HTML source code of a page. I can retrieve and display the full source code on a TextView without any problem. Then, I want to pass all content of TextView to EditText in order to enable Editing the code. But when I pass source code to EditText, EditText does not show all of the code. The code is broken from random locations. It shows some part of the code. I am totally tilted.
Some details:
TextView is in another activity and EditText is in another activity, I am passing TextView content to EditText with Intents.
Do you have any idea?
Thanks in advance.
BR,
Ali

Try this:
EditText text = new EditText(this);
text.setText(Html.fromHtml(html));
text.setMovementMethod(LinkMovementMethod.getInstance());

All the time, I have counted the number of characters that I am passing to the edittext, it is revealed out that whatever website source code I am previewing the count is 10000. And the solution is so simple:
android:maxLength="100000"
That is it! Now I can view up to 100000 characters.

Related

How to set a SpannableString to an EditText in an Android Material TextInputLayout?

I can't find a way to set the text of an EditText in a Material TextInputLayout to a SpannableString and have the styling actually show up on screen. I've tried every combination of EditText inside a TextInputLayout, e.g.:
AppCompatEditText
EditText
TextInputEditText
with every way to create a SpannableString, e.g.:
HtmlCompat.fromHtml(string, HtmlCompat.FROM_HTML_MODE_COMPACT)
SpannableString(string).setSpan(...)
TextUtils.stringOrSpannedString(string)
with every way to set the text of an EditText, e.g.:
edittext.setText(spannedString)
edittext.setText(spannedString, TextView.BufferType.SPANNABLE)
edittext.append(spannedString)
The crazy thing is they all work when the EditText is not inside a Material TextInputLayout, so it must be a problem with that. More information: the SpannableString is respected when you set the hint or placeholder text on the TextInputLayout, just not the text of the EditText inside it. I looked through the TextInputLayout source code, but didn't see anything wrong. Thanks to anyone who can help.
I figured it out right when I was going to give up. You can get the Editable object from the EditText and then apply the Span to it. Here's how:
mEditText.text.setSpan(...)
Surprisingly easy when you know where to look.

Android Studio Development. EditText

Im looking to make a simple app that involves the user to input an integer and depending on that integer, the same number of EditText will appear. For example, if the user inputs 5, then 5 EditTexts will materialize right below. I dont need help with the layout, just the java code. Im new to programming and im not quite sure how to approach this. thanks.
You can go for creating EditTexts programmatically inside a loop. Use
EditText editText = new EditText(this); // declare the editText
linearLayout.addView(editText); // add the editText to the parent view, wchich is a linear layout here. inside a loop with the number entered in editText as limit.
For more reference and example about creating views dynamically, refer this link.

how to get phone number from my Contacts on android programing

i have EditText and button on my form.
how to do this:
when i press the button i want to open all my Contacts,
and when i pick one - i what that his phone number will be in the EditText
thanks in advance
use this links May be it's useful for you.
http://www.java2s.com/Open-Source/Android/Contact/spelldial/org/ravelin/android/ContactAccessor.java.htm
http://developer.android.com/resources/samples/ContactManager/index.html
http://code.google.com/p/android-business-card/source/browse/trunk/android-business-card/BusinessCard/src/com/example/android/?r=3#android%2Fbusinesscard
In this post you can read contact list.
How to call Android contacts list?
there are some methods...
Then set EditText with contact list content
void setText(CharSequence text, TextView.BufferType type)
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.
:-)

Android/java - trouble creating newlines with TextView

I just want to append a TextView element to my linearlayout in order to add a newline to the screen the users see on the phone. I tried:
final TextView newline = new TextView(this);
newline.setText("\n");
main_layout.addView(newline);
... but to no avail. In fact this code made my phone vibrate angrily for some reason.
Help much appreciated!
EDIT - typo removed, and main_layout is my linearlayout element to which I'm appending content (strings).
I'm a newbie myself, so maybe I'm missing something obvious here, but shouldn't your code read:
newline.setText("\n");
Or you could just do:
textView.append("\n");
to append a /n to an existing TextView element
First you need to add the TextView to your layout with the add() function. Better practice is to create a layout xml file for your view. It is much easier to work with.
And how is newLine getting '\n' text if you setting it on title_response?
And angry buzzing usually indicates that your application crashed. Welcome to the club :)
Try passing to the ctor of the TextView an AttributeSet as second parameter, setting the "lines" attribute. Just in order to make sure that the TextView is not created as "signle-line",

Dynamically Displaying Text with TextView (Android)

Basically what I want to do in my Android app is use TextView to display two different pieces of text at once. So in code, I want to be able to do something like this:
LinearLayout ll = new LinearLayout(this);
TextView text = new TextView(this);
text.setTextColor(0xFF000000);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setTextSize(20f);
text.setText("Text1");
text.setTextSize(14f);
text.setColor(0xFF0000FF);
text.setText("\nText2");
ll.addView(text);
To clarify, I am trying to display a black "Text1" and a blue "Text2" at once using only a single TextView. Obviously this doesn't work out using the code above. I've considered using a second TextView but that seems like a waste of effort and memory to me. I'm sure the brilliant minds here can come up with the best solution to this.
Thank you very much in advance for your time and your assistance.
There are two options for you.
One is
Spannable
and other is
fromHtml (String source)
So that you can get your desired output.
I think with the current version of the code, you can see only the latest text (Text2).
If you want to have multiple look and feel for two texts, I would suggest use 2 separate TextViews. It would add more flexibility.
If you are not going to change this UI code later, then you can consider Html.toHtml() in setText() call.
It seems the problem is with:
LinearLayout.addView(text);
You are trying to add a view to a LinearLayout, but the layout doesn't exist (in the current activity). You need to add the TextView to a Layout defined in the .xml you are using. Suppose you have a LinearLayout with id "linearlayout01" in the xml file "activity1.xml", you would do something like:
setContentView(R.layout.activity1);
// Create and adjust TextView text
...
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout01);
layout.addView(text);
Once a View is added to a ViewGroup of which LinearLayout is a descendant you do not need to readd it to update its display. If you preform any changes on a view that requires it to change its display it will handle all the proper notifications about required redraws or relayouts by calling View#invalidate and View#requestLayout where appropriate.
In addition, because all UI changes are handled on the same thread you do not need to worry about calling multiple methods that will update the UI. This is because of two reasons, first, the execution of the redraws will not occur until your code is finished, second, android has optimizations built in that combines multiple invalidate calls into one.
So, the only thing you need to worry about is getting a proper reference to your TextView instance and then you can call all the methods on it that you need to make it display what you wish.
Since you are creating your Views manually and not from xml you need to add your root ViewGroup to the Activity by calling Activity#setContentView.
Edit:
Then you're going to need to learn about SpannableString and SpannableStringBuilder. There is some very brief documentation here: Selecting, Highlighting, or Styling Portions of Text
when do you plan to update the textview ? If it is on click of a button then get a reference to the textview and in the onClickListener() update the text, color, etc whatever you want to do.
After seeing your other comments, I think SpannableString is what you are looking for

Categories