Android html.form html - java

I'd like to read html tags to a TextView, so I have done this:
titolo = (TextView) this.findViewById(R.articolo.titolo);
testo = (TextView) this.findViewById(R.articolo.testo);
titolo.setText(db.getTitolo());
testo.setText(db.getTesto());
testo.setText(Html.fromHtml(testo));
But i have an errore here: testo.setText(Html.fromHtml(testo)); Why?
This application retrieves data from a database so I hope that if I write into the database, for example hello this is formatted as bold using html.fromhtml

public static Spanned fromHtml (String source)
Returns displayable styled text from the provided HTML string. Any tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.
This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
More info #
http://developer.android.com/reference/android/text/Html.html
This
testo = (TextView) this.findViewById(R.articolo.testo); // textview initialized
This
testo.setText(Html.fromHtml(testo)); // wrong
fromHtml takes a string as a argument
It should be
testo.setText(Html.fromHtml("myhtmlString"));
Example :
String s ="<b>"+"Hello"+"</b>";
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(Html.fromHtml(s));

In your example you're sending TextView to fromHtml and you should deliver String variable. That String could contain HTML tags.
TextView testo = (TextView) findViewById(R.articolo.testo);
String formattedText = "This is <b>bold</b>";
testo.setText(Html.fromHtml(formattedText));
Of course you could get String from DB. I don't know how works your getTesto() method, but if it returns String you could write:
TextView testo = (TextView) findViewById(R.articolo.testo);
String formattedText = db.getTesto();
testo.setText(Html.fromHtml(formattedText));

Related

how to set value in setBackgroundResource

I want to change a picture from another activity and display it as a picture or button, but it makes me a problem when I want to run the program, I'd love to help
imageView10 = (ImageView) findViewById(R.id.imageView10);
String s;
SharedPreferences mySharedPreferences2 = enter.this.getSharedPreferences("two", Context.MODE_PRIVATE);
s = mySharedPreferences2.getString("USERNAME2", "");
s = "R.drawable.add1";
imageView10.setBackgroundResource(s);
the s value is red...

How to overwrite a text in TextView using a button?

I'm working on an app that generates passwords randomly using the array. The password is in TextView. Everything is good unless I want to generate a new password second time. How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
Here are the variables that I'm using:
EditText dlugosc;
String haslo = "";
String pustak = "";
TextView haslo0;
And this is a code that I use to generate a password:
(znaki is the name of array)
dlugosc = findViewById(R.id.password_len);
haslo0 = findViewById(R.id.password);
String yui = dlugosc.getText().toString();
int x = Integer.parseInt(yui);
for(int i = 0; i < x; i++){
int Index = generator.nextInt(znaki.length);
haslo = znaki[Index] + haslo;
}
I have already tried doing an if structure:
if (haslo0 != null){
haslo0.setText(pustak);
haslo0.setText(haslo);
}
else
haslo0.setText(haslo);
But it doesn't help :(
When I want to have 7 chars in the password and click the button first time, the result is correct e.g. PKAjzQL. But when I click the button second time, the result is nBzcRjQPKAjzQL instead of nBzcRjQ.
Why are you appending the old string haslo behind the newly generated one in haslo = znaki[Index] + haslo;
Probably that's why you are getting output like that. Can you please try just setting newly generated password into the textview like
haslo = znaki[Index];
And then try to set text in the text view using haslo0.setText(haslo);
How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
The problem is not to "delete" the old text, the problem is that you have to clear the list for example, every time user clicks on the Button you clear the list doing : znaki.clear(), then it will only show the new password generated.
If you see your output :
First output :
PKAjzQL --> This is correct
Second output :
nBzcRjQPKAjzQL --> this is the new output + the old one
Can you give the code of the OnClickButton? And why are you setting the same TextView with diferents Strings when you click?
haslo0.setText(pustak);
haslo0.setText(haslo);
?

Two extra lines added to edit text when retrieving html String

I save my formatted edit text in sqlite database in html then after retrieving it two extra lines are added to the edit text
So I started like so:-
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
String text = Html.toHtml(e);//convert to String
Then after I inserted text into the database .I retrieved it back to edt this way:-
Spanned sp = Html.fromHtml(text);//convert text to spanned
edt.setText(sp);//setting to the edittext
It was retrieved successfully however 2 extra lines where added to edt at the end of text each time why you think that occurs?.
try this
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
/* String text = Html.toHtml(e);//convert to String
Spanned sp = Html.fromHtml(text); //convert text to spanned */
edt.setText(e);//setting to the edittext
I finally found a solution using code from an answer in this question Remove extra line breaks after Html.fromHtml()
.Its all about removing extra html whitspaces but using it for spannable
String like so :-
Spanned sp = Html.fromHtml(subnote);
int h = sp.length();
// loop back to the first non-whitespace character
while(--h >= 0 && Character.isWhitespace(sp.charAt(h))) {
}
sp= (Spanned) sp.subSequence(0, h+1);
edt.setText(sp);

Android - Set font in other language

How can I set custom font to editText in Android? I use this code
etPlace = (EditText)findViewById(R.id.editTextPlace);
etPlace.setTypeface(tf);
It can work with English language. But if I type in Thai language, it will become to HEX. I tried to remove setTypeface code but it can't solve. Anyone can suggest me?
This is example picture of my app
You must use some Unicode font that support your wanted language in the setTypeface method. Like Arial Unicode MS.
You should create a class that return EditText Object with font you want to support.
EditText public SetLanguage(EditText tv,string type)
{
EditText newtv = tv;
Typeface tf;
switch(type)
{
case "en":
tf = Typeface.createFromAsset(this.getAssets(),"en.ttf");
break;
case "thai":
tf = Typeface.createFromAsset(this.getAssets(),"thai.ttf");
break;
// up so on
}
newtv.setTypeface(tf);
return newtv;
}
// and call it any where..
EditText etPlace = (EditText ) findViewById(R.id.textView2);
etPlace = classobj.SetLanguage(textView1,"thai");
//assign string of text to it

how to make textView clickable and collect information about clicked data?

i had data set which are retrieve from sqlite database and the data will display by using textView.
this is code
ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());
Cursor allcontact= onbOfContactDatabase.showData();
TextView t=(TextView)findViewById(R.id.textView);
String dbString="";
allcontact.moveToFirst();
do {
dbString+=allcontact.getString(allcontact.getColumnIndex("name"));
dbString+="\n";
t.setText(dbString);
} while(allcontact.moveToNext());
The output of this code like
new text
data1
data2
data3
data4
data5
i want all data make clickable and how to know who data is clicked?
plese help this is argent.
Change the TextView to a ListView then set onClickListener for the ListView, then with that set
String output = listview.getselecteditem(position);

Categories