I am using WebView
to justify text. I want to know-
Is it possible to set the text size in
layout
file? As I want different text size for different screen sizes.And also one problem is first background appears then after 2 second text display. Is it possible to display text immediately?
Size of text is 7-8 lines.
Code-
public class Main extends Activity {
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
String text = "<html><body>"+"<p align=\"justify\">"+getString(R.string.itext)+"</p>"+"</body></html>";
mWebView .loadData(text, "text/html", "utf-8");
mWebView .setBackgroundColor(Color.TRANSPARENT);
}
}
Xml-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/light">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"/>
</LinearLayout>
1 - I'm using this code that gives me a good result on all my devices.
If web is tyour WebView,
OK, this is hardcoded
Now, a more dynamic solution (as per your question):
If you put your desired size in an integers file (several, one for each screen you are supporting), you could do so to retrieve its value:
Assuming that your res/values/integers.xml file(s) is similar to this one:
So change this line in the above code
to
2 - To load things faster, I use the following code
Note that I load more than 7-8 lines of text: it's a full HTML page with JavaScript to expand / collapse sections. And it takes, say, half a second to load. So, I guess, 7-8 lines of text will be loaded in a flash.
You can use
Html.fromHtml()
and write your text inHTML
format.either setTextSize or
This one works too:
And Display text immediately:
There is three way to set
FontSize
onWebView
.1) WebView.setDefaultFontSize(int)
2) WebView.getSettings().setTextSize(WebSettings.TextSize.SMALLEST)
or
or
or
or
3) Add font-size in html formate.
For setting text size from layout-
For Immediate text display-
In values folder-
Hope it works.
You can always use CSS to change the font size:
To display fater, as Digvesh Patel said before: