I am trying to take a string (which is formatted HTML) and split it at every double enter. I know that before and after a double enter will be a p block tag, and I'm using a for loop to split it in this way. Here is my code
for(String s : rawHTML.split("\\n\\n")){
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
comm.setText(Html.fromHtml(s) );
Expected behavior from this is every "Currently is" in the log should be blank. If there are two or more p blocks, though, when I setText, it overwrites the previous text.
For example, I have a string
<p>Test</p>\n\n<p>Hello</p>
I should be seeing two TextViews like so
[Test]
[Hello]
Instead, I see this
[Hello]
[]
I am really stumped and can't figure out this strange issue.
Thank you!
The following code snippet shoud work for you.
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
for(String s : rawHTML.split("\\n\\n")){
comm.append(Html.fromHtml(s) );
}
Related
I am trying to put a link on a textview, but when I click this link the application breaks!! This is my code:
final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
msg.setText(Html.fromHtml(dbStructure.getMsg()));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());
Where dbStructure.getMsg() returns a String. This String could be something like:
< a href="/reference/android/widget/RelativeLayout.html">RelativeLayout< /a>
lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.
It seems nice, but the app stops when I press it.
EDIT
The error thrown ActivityNotFoundException.
the link you are trying to open is broken
/reference/android/widget/RelativeLayout.html
there is nothing corresponding to the above link.
replace it with the proper url like this
http://developer.android.com/reference/android/widget/RelativeLayout.html
Thank you very much for every one... the problem is (as #Antonio #danidee #TheRedFox and #Arslan say) the format of the url... it doesn´t start with http.
With permission from you all, I am going to answer my own question:
final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
String msg_text = dbStructure.getMsg();
if(msg_text.contains("href=\"")) {
String[] msg_aux = msg_text.split("<a href=\"");
if (!msg_aux[1].toLowerCase().startsWith("http"))
msg_aux[1] = "href=\"http://" + msg_aux[1];
else
msg_aux[1] = "href=\"" + msg_aux[1];
msg_text = msg_aux[0] + msg_aux[1];
}
msg.setText(Html.fromHtml(msg_text));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());
Thank you.
EDIT on the code, these lines:
else
msg_aux[1] = "href=\"" + msg_aux[1];
I know to get a string of a specific TextView in a ListView, I can do this:
ReviewUser = ((TextView) rowView.findViewById(R.id.labelUser))
.getText().toString();
What if I want to get the TextView itself?
The TextView is an integer and I simply want to get the TextView and add 1 to it.
So you already specified that you know how to get the specific text from your ListView. Since you want to modify that same TextView, the rest is simple. This code is lengthier just to show the steps.
TextView textView = (TextView) rowView.findViewById(R.id.labelUser)
String text = textView.getText().toString();
int num = Integer.valueOf (text).intValue() + 1;
textView.setText (""+num);
Also, if you are working with a String ArrayAdapter and you know the index of the row you want to modify, what you can do is (assuming arrayAdapter is initialized and index is your index variable):
String text = arrayAdapter.get(index);
arrayAdapter.remove (text);
arrayAdapter.insert ((Integer.valueOf (text).intValue() + 1 ) + "", index);
I create a TextView dynamically and want to set the text as linkable. Text value is "Google". I referred to internet & blogs like this, which shows the same way, but I couldn't produce the expected results.
I tried different ways, but the output I see is the whole text with text only. The code I have tried with is :
TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
// Make Linkable
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv1.setText(Html.fromHtml(l.getLeftString()));
/*SpannableString s = new SpannableString(l.getLeftString());
Linkify.addLinks(s, Linkify.WEB_URLS);
tv1.setText(s);
tv1.setMovementMethod(LinkMovementMethod.getInstance());
*/
dialogLayout.addView(tv1);
In my output I see "Google" and no link. I also tried Clean project & building it again, but no success.
I am looking to see only "Google" as underlined with blue color (as default) and on clicking Google, the browser open with http://google.com.
What is lacking in my code to get the output ?
BTW For REF : I use 64bit Win 7, Java, Eclipse, Android API 8-2.2
Any help is highly appreciated.
I finally got it working using the following code :
TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
tv1.setText(Html.fromHtml("" + l.getLeftString() + ""));
tv1.setClickable(true);
tv1.setMovementMethod (LinkMovementMethod.getInstance());
dialogLayout.addView(tv1);
l.getRightString() - contains a url like http:\www.google.com
l.getLeftString() - contains text for the url like "Go to Google"
RESULTS :
Text "Go to Google" on my dialog with blue color and underlined, and on clicking it the browser opens up and shwows the respective page. On returning/Exiting the browser it again comes to the app from the state where it had left.
Hope this helps.
Save your html in a string
<string name="link"><a href="http://www.google.com">Google</a></string>
Set textview ID to to
textViewLinkable
In main activity use following code:
((TextView) findViewById(R.id.textViewLinkable)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textViewLinkable)).setText(Html.fromHtml(getResources().getString(R.string.link)));
I was also facing the same issue I resolved using following
String str_text = "<a href=http://www.google.com >Google</a>";
TextView link;
link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
link.setText(Html.fromHtml(str_text));
for changing the link color to blue use
link.setLinkTextColor(Color.BLUE);
Here is my simple implementation tested up to Android N.
String termsText = "By registering, you are agree to our";
String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
String allText = termsText + termsLink + privacyLink;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
}
else {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
}
txtview.setMovementMethod(LinkMovementMethod.getInstance());
Pass this statement to your textview, and in string.xml set an string as
<string name="txtCredits"> </string>
Now pass this string name " android:text="#string/txtCredits" to your xml class where the txtview is there .
use this code autolink-java On GitHub
like this
private String getLink(String string){
LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
.build();
Iterable<Span> spans = linkExtractor.extractSpans(string);
StringBuilder sb = new StringBuilder();
for (Span span : spans) {
String text = string.substring(span.getBeginIndex(), span.getEndIndex());
if (span instanceof LinkSpan) {
// span is a URL
sb.append("<a href=\"");
sb.append(text);
sb.append("\">");
sb.append(text);
sb.append("</a>");
} else {
// span is plain text before/after link
sb.append(text);
}
}
return sb.toString(); // "wow http://test.com such linked"
}
I have in my layout.xml a TextView with "id = txtLog".
Where do the test results from my application using:
Log.i("Result:", "Value of x = " + x);
for show result in LogCat.
It is possible to show these results "Log.i" within the TextView?
Note: I left a space at the bottom of my application to show the TextView.
Like a console.
I would like to display these messages on TextView.
If possible create a scroll bar and display every time I use Log.i
I am a beginner, do not know if it is possible. Yet thanks.
I would think
myTextView.setText(myTextView.getText() + "Value of x = " + x + "\n");
would work.
EDIT:
Also, to make the TextView scrollable, you need to set a movement method like so:
myTextView.setMovementMethod(new ScrollingMovementMethod());
EDIT 2:
If you want the information to go to both Log.i and a TextView, then you need a method that holds a reference to the TextView you want to update.
public static void LogToView(TextView myTextView, String title, String message) {
Log.i(title, message);
myTextView.setText(myTextView.getText() + title + ": Value of x = " + x + "\n");
}
Put that in whatever class or in your Activity class. Use it instead of Log.i and the message will be passed to both.
Here's my issue:
I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:
Episode 1
Episode 2
Episode 3
etc.
I'm using my own adapter class that extends SimpleCursorAdapter to do this...
Since I had formatting errors I am using Android.R.layout.simple_list_item_1 and Android.R.id.text1
Basically the only reason I have a custom adapter is so I can do something like this:
textView.setText("Episode " + cursor.getString("column_for_episode_number");
The problem is, I get a list that looks like this:
Episode
1
Episode
2
Episode
3
When I try something like this(which worked in a different portion of my code):
String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");
I get the exact same list output :(
Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/
text.replaceAll(System.getProperty("line.separator"), "");
There is a mistake in your code. Use "\n" instead of "\\n"
String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);
Check if there is new line at the beginning before you replace and do the same test again:
for(int i=0; cursor.getString("blah").length()-1; i++)
{
if(cursor.getString("blah").charAt(i)=='\\n') <-- use the constant for the line separator
{
Log.i("NEW LINE?", "YES, WE HAVE");
}
}
Or use the .contains("\n"); method:
Check the xml for the width of the textview as well.
Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.
This could help you:
response = response.replaceAll("\\s+","");
It sounds like you are hitting wrapping issues rather than newline issues. Change this:
String text = "Episode " + cursor.getString("blah");
To this:
String text = "Episode" + cursor.getString("blah");
And see if that changes the output. Post your layout xml please?
this worked for my (on android 4.4):
(where body is a string with a newline entered from an EditText view on handset)
for (int i=0; i<body.length(); i++) {
if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
body = body.substring(0, i) + " " + body.substring(i+1, body.length());
}
}
have you tried
cursor.getString("blah").trim()