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];
Related
I want to compare a xml string with a string from an edittext oder button.
first I set the text of the button:
button1.setText(getString(R.string.okey));
and now I want to check if the text from the button is the same as R.string.okey from the xml file. Like this I can leave out a new variable.
Is it possible to check if the strings are the same with something like this?
if (button1.getText().toString().equals(getString(R.string.okey))){
}
But that doesn't work for me.
Thank you in advance.
this must work, its just to simple. you must change somehow text on button or maybe getString returns different text (Locale changed?). use logging or debugger to check what is button1.getText().toString() and getString(R.string.okey) at the moment of comparison (equals call)
boolean areEqual = button1.getText().toString().equals(getString(R.string.okey));
Log.i("justChecking", "getString:" + getString(R.string.okey) +
", button1.getText:" + button1.getText().toString() +
", are equal:" + areEqual);
if (areEqual){
}
How to Write and View Logs with Logcat
Store them in variables
String a = button1.getText()+"";
String b = getString(R.string.str)+"";
if(a.equals(b)){ }
I want to display the text in vertical text view (i.e. characters should be placed one below another as below)
a
b
c
d
I wish to do this in a single text view.
We can turn the entire text by using android:toDegrees="-90" or "90" but i want to display as shown above which is not achieved with android:toDegress tag.
Appreciate your help!
UPDATE
I will get the String from another app through AIDL which is subjected to change as per the sender, in addition, he will send me a flag to display the text in a vertical or horizontal direction. Based on the flag I should display accordingly. I have no problem with a horizontal view as it is the default one but to display it in vertical(as shown above) I need help.
If String is Dynamic then you can add \n after each character.
private String build(String str){
if(!TextUtils.isEmpty(str)) {
StringBuilder builder = new StringBuilder();
builder.append(str.charAt(0));
for (int i = 1; i < str.length(); i++) {
builder.append("\n"+str.charAt(i));
}
return builder.toString();
}
return "";
}
textView.setText(build("abcd"));
It will show vertically. This way text can go out of Bound so you should make TextView scrollable in this case.
You can add line break to your string with either \n or <br>
With \n:
<string name="sample_string">a\nb\nc\nd</string>
textView.setText(R.string.sample_string)
With <br>:
<string name="sample_string"><![CDATA[a<br />b<br />c<br />d]]></string>
textView.setText(Html.fromHtml(context.getString(R.string.sample_string))
I have a code rite now that just generates a random drink combination from an array, what I need to do is have a different image assigned to each choice and have it display that image.
Here is my code for a random drink:
if(Vodka.equals(true)){
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(array_city.Vodka.length);
text2.setText(array_city.Vodka[randomIndex]);
}
Say this code spits out "Smirnof" then i display a picture of the bottle, it spits out "Sky" then changes to a picture of that bottle. How would i do this without making an if statement for each option, my arrays are very long and that would be alot of if statements i was just Hoping that there is an easier way to do it?
Thanks anybody for your help! it is very much appreciated I have been stuck on this for a while.
===============================================================
#Joan
Here is what i am trying to put together using your code:
//Run option Vodka
if(Vodka.equals(true)){
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(array_city.Vodka.length);
text2.setText(array_city.Vodka[randomIndex]);
final ImageView image = (ImageView) findViewById(R.id.imageView1);
int Cimage = getResources().getIdentifier(array_city.Vodka[randomIndex], null, "com.famousmods.what.should.i.drink");
image.setImageResource(Cimage);
}
Here is what my array looks like (a smaller example):
public static final String[] Vodka = {"Absolut Vodka","Finlandia","Ketel One","Polmos Krakow","Skyy","smirnoff vodka",
"Stolichnaya","Fleischmann's","Gilbey's","Gordon's","Wolfschmitt","Five-O-Clock"};
I have put the file "smirnoff_vodka.png" into my res/drawables as an example but it doesnt work?
You can use getResources().getIdentifier("image_name", null, "your_application_package"); on your context to retrieve the image id. Then you can use this id as you would use R.id.image_name.
EDIT: It needs to be "drawable" instead of null. See below.
I am on the project RichTextEditor and completed almost all functionality. I can insert image and can save the file with image and also getting the image and all styles while opening the file again.I am stuck at one point ie. when copying all the content of the Edittext, while pasting except Image all things got paste, but in image area i got like this
any idea or workaround to copy and paste the image.
Thanks.
I have the same problem. After get the editText field's string, I find the "obj" character, and then replace it with image's link. I created a ArrayList to store the images' links. And moreover, I think I need to catch the delete action. If an image is deleted, I deleted its link in the image list. Below is the code I use to replace the "obj" character.
private String replaceSpecialCharactorFromNote(){
String noteString = edt_note.getText().toString();
char[] noteCharacters = noteString.toCharArray();
for(int i=0; i<noteCharacters.length; i++){
if((int)noteCharacters[i] <1 || (int)noteCharacters[i]>254 ){//compare the ascii code
Log.i("the first abnormal charactor is ", "" + noteCharacters[i]);
if(imageIndex < imgsList.size()){
Log.i("replace triggered", "special char index is "+i);
Log.i("replace triggered", "replaced image index is "+imageIndex);
Log.i("replace triggered", "image is "+imgsList.get(imageIndex));
String beforeString = noteString.substring(0, i);
String afterString = noteString.substring(i+1);
noteString = beforeString + imgsList.get(imageIndex) + afterString;
Log.i("replace triggered", "note is "+noteString);
}
imageIndex++;
}
}
return noteString;
}
Overall, I do not think the way I did is the best way to solve the problem. The best way probably will be to create a custom field to handle it.
Did you check the content on the clipboard? How is the image handled in the clipboard? You will have to make your RichTextView handle the paste operation (is the image copied as a bimap / are you referencing a path to the bitmap) from the clipboard.
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()