Setting part of summary with different colour while using preferences - android

I have a set of listpreferences which I am displaying in a PreferenceActivity screen. I would like to display a part of the text in the summary field with some formatting, say italics, or different colour.
What would be a good way to go about this?
I am using the following code to set the summary of a listpreference:
lp.setSummary(myText);
//E.g myText = "My name is SoAndSo";
//Required output : "My name is *SoAndSo* (in italics)
// or
// "My name is *SoAndSo* **(in green)**

You can format the text output in html, using html tags.
Here's one way:
mBox = new TextView(context);
mBox.setText(Html.fromHtml("My name is <i>SoAndSo</i>" + "<br />" +
"<small>" + description + "</small>" + "<br />" +
"<small>" + DateAdded + "</small>"));
For an unofficial list of tags supported by this method, refer to this link

Related

How to get bullet points when parsing text with Jsoup?

I am using Jsoup to get the text from an html doc and display it in my android app.
The text cotains a list (<ul><li>).
If I do it like this I get only the text:
val doc = Jsoup.parse(someHtml)
return doc.text()
I tried using wholeText:
val doc = Jsoup.parse(removeImages)
return doc.wholeText()
In this way it keeps some formatting, but still it ignores the bullet points. Is there any way to get the bullet points in the text?
The bullets are rendered by the browser, so they are not a part of the text.
You'll have to add it by yourself, like in this example:
String html = "<html>" +
"<head>" +
"<title>List</title>" +
"</head>" +
"<body>" +
"<ul>" +
"<li>Item 1</li>" +
"<li>Item 2</li>" +
"<li>Item 3</li>" +
"</ul> " +
"</body>" +
"</html>";
Document doc = Jsoup.parse(html);
Element list = doc.select("ul").first();
Elements item = list.children();
for (Element e : item) {
System.out.println("\u2022" + e.text());
}
The output is:
•Item 1
•Item 2
•Item 3
You can replace the bullet with any other character that you like, by replacing the \u2022 code with any other valid code/character.

Applying dimensions from dimens.xml to text formatted with Html.fromHtml

I have this code for setting the text of a TextView:
TextView txt = new TextView(this);
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />" + "<small>" + m.getText() + "</small>" + "<br />");
The <small> mark is working, but I'd like to set the text size according to my dimensions defined in the dimens.xml file, which I use for all other text in my application. Adding the TextView through an xml layout is not an option since I don't know how many TextViews I'll be adding.
Dimensions in the dimens.xml file are set up like <dimen name="text_size_320dp_small">16sp</dimen>.
How can I apply these dimensions to my text formatted with Html.fromHtml?
Thanks a lot.
I have tested following code myself. You can do it like this.
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />"
+ "<font textsize="
+ getResources().getDimension(R.dimen.text_size_320dp_small) + ">" + m.getText()
+ "</font>" + "<br />"));
[Updated]:
Just came up with some references and updates :
You can store this in strings.xml
<string name="mystring"><font size = "%s"></string>
In code you can write as:
int sptopx = getResources().getDimensionPixelSize(R.dimen.text_size_320dp_small);
Spanned modified = Html.fromHtml( context.getString(R.string.mystring, sptopx) );
myTextView.setText(spanned);
TextView txt = new TextView(this);
txt.setText(
Html.fromHtml(
"<b>" + m.getTitle() + "</b>" +
"<br />" +
modified +
">" + m.getText() + "</font>" +
"<br />"
)
);
for details about html tags support in TextViews you can check this link.
You can't directly, the small tag creates a RelativeSizeSpan with a proportion of .8f, which is hardcoded into the implementation of Html.fromHtml.
Leaves two options that I can see, set the text size to 20sp (which would make small work out to 16sp). Probably not ideal.
The other option is to use a custom tag <mySmall> by replacing all occurrences of <small> and </small> with <mySmall>& </mySmall>. And then call fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) with a TagHandler that integrates a AbsoluteSizeSpan into the output Editable.
Why don't you use txt.setSizeText(yoursize)? However you can retrieve your dimensions using this:
float yourDimen = getResources().getDimension(R.dimen.your_dimen_name);

Html Checkbox in android webview

I want to know:-
In my project i am using html content and displaying them in android webview. and i am using eclipse ide. this is tiny code.
"<form name =\"frm\">"+
"<input type=\"checkbox\" name =\"First\" value =\"xyz\">xyz<br>"+
"<input type=\"checkbox\" name =\"First\" value =\"abc\">abc<br>"+
"</form>"
my question is how can i get check box state .its checked or unchecked.
or how can i catch the state in my java code.
UPD:-
public String html = "<form name =\"frm\">"+
"<input type=\"checkbox\" name =\"First\" value =\"xyz\">as<br>"+
"<input type=\"checkbox\" name =\"Second\" value =\"zyx\">as<br>"+
"<input type =\"button\" onclick =\"callDoSomething()\"><br>"+
"</form>" +
"<script type=\"text/javascript\">"+
"function callDoSomething() {"+
" var theName = document.frm.First.value;"+
"alert('theName ')"+
"}"+
"</script>";
First, both of your checkboxes are named "First", you should probably name second one "Second". If you want to search checkboxes by value - just add a simple js for loop.
Assuming you want to get the results from your Android code (as opposed to JS event like clicking a button), here's how you get Java boolean value for you checkbox by name:
// assuming your activity is MyActivity, target checkbox name
// is in the targetCheckboxName var and webView has the document
// loaded already
Object jsi = new Object() {
#JavascriptInterface
public String reportCheckboxState(String name, boolean isChecked) {
new AlertDialog.Builder(MyActivity.this).setMessage(name + " is " +
isChecked).create().show();
}
};
webView.addJavascriptInterface(jsi, "injection");
webView.loadUrl(
"javascript:injection.reportCheckboxState(frm." + targetCheckboxName +
".name, frm." + targetCheckboxName + ".checked);"
);
But really, it's a very simple trick. Judging by comments to the question, you should probably read up on JavaScript and WebView.addJavaScriptInterface()

Not display bold content in webview android

I have set custom font in Webview using
String html = "<html><head><title></title> "
+ "<style type=\"text/css\"> "
+ "#font-face { font-family:'GEORGIA'; src: url('file:///android_asset/GEORGIA.TTF'); }"
+ "body { font-family: 'GEORGIA'; text-align: justify;}"
+ "</style> "
+ "</head><body >"
+ "<div>"
+ "<p>"
+ data + "</p></div></body></html>";
webview.loadDataWithBaseURL("",html,"text/html", "utf-8", "");
It is set font but problem is that in data i have text like heading contents when i run it then heading is not display bold.
Maybe your font does not contain bold characters.
True Type Fonts (TTF) can contain a subset of all available characters.
Did you copy the ttf-file from your systems fontpath?
If not, you can open the font with a tool like font-explorer,

Using size HTML attribute in TextView

I have the following:
textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));
The string 'Hello' does turn red but the size does not change.
It is as if the size attribute is just ignored, does anyone know why this is? Am I doing something wrong?
Size attribute seems not working.
You can use <small> or <big> (multiple times to increase the effect)
You can also use <h1> to <h6> (Header only, i.e. add a new line)
Its old-fashion, but it works well !
Yes, size attribute just ignored. Only "color" and "face" attributes are taken into account.
From Html class sources:
private void handleStartTag(String tag, Attributes attributes) {
if (tag.equalsIgnoreCase("br")) {
// We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
// so we can safely emite the linebreaks when we handle the close tag.
}
...
else if (tag.equalsIgnoreCase("font")) {
startFont(mSpannableStringBuilder, attributes);
}
...
}
private static void startFont(SpannableStringBuilder text,
Attributes attributes) {
String color = attributes.getValue("", "color");
String face = attributes.getValue("", "face");
int len = text.length();
text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}
Try this one,Its working for me,use small,big key words
TextView mBox = (TextView) findViewById(R.id.txt);
mBox.setText(Html.fromHtml("<font color=#cc0029>" + "<b>"
+ "Hiiiiiiiiii" + "</b>" + "<br />" + "<small>" + "description"
+ "</small>" + "<br />" + "<small>" + "DateAdded" + "</small>"));
Sergey Gotov is right. The only way to change text size it to use h1 - h6 tags.
EDIT: You can also implement TagHandler and use your own tags.
Look at Formatting and Styling on the android developpers site:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Or, on this old post of StackOverflow :
Highlighting Text Color using Html.fromHtml() in Android?

Categories

Resources