I want to customize text (font, size, color...) which is displayed on a picture. I decided to use WebView as I'm not sure how to use (and is it possible at all???) HTML/CSS inside a TextView. The problem is that a WebView has its own background(white) though which I can't see my picture. setBackgroundDrawable(null); did not help.
Use an imageview and textview inside of an absolutelayout or relativelayout. Then align the two views to different sides of the layout and use margins and gravity to space them correctly. Using a webview for images and text when there is no need seems pointless if they are not on a webpage.
http://developer.android.com/resources/articles/layout-tricks-merge.html
To add a font to your project, create a new folder called "assets" in your root. inside there creates a folder "fonts" and in there, place your .ttf file. Here is a link:
How to add external fonts to android application
You might be able to use yourWebView.setBackgroundResource(R.drawable.yourImage); But I am not 100% sure that WebViews support that method. Another option would be to put the Image in the html that you are using though. That might be the easiest way to go about making it look exactly how you want.
You can try this out..
public class Hello extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.hello);
mWebView = (WebView) findViewById(R.id.webviewactive);
String text =
mWebView.loadData(text, "text/html", "utf-8");
mWebView.setBackgroundColor(0);
OK, so I found the answer. If you want to make the background of WebView transparent, just write in your .xml file android:alpha="0". To retrieve HTML-tags in Android, implement a TagHandler interface. There is an example here.
Related
Can I assign the same id name to two different views which are in two different xml files?
It won't let me change it using "Edit ID.." button, but when I change it manually, it doesn't say anything and works fine. But might it become a problem and why?
If possible, please add a link or an general explanation on android visual tree build, I would really like to learn on the subject.
Thanks.
It's fine to have the same ID in multiple XML files. The only time this might be a problem is if you had a nested layout containing an ID which is also present in the parent.
Having the same ID can actually be useful in some cases- for example if you load a different XML file in different circumstances (such as portrait and landscape) you can give views the same ID, so in code you just call findViewById once and it will work.
You can't use the same ids in same layout .. but if their behavior is different then it is possible.
public class MainActivity extends Activity {
Button btn;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn1);
tv=(TextView)findViewById(R.id.btn1);
}
You can use the same ids in different layouts but it is recommended that you use different ids for different layouts because when you will deal with lots of layouts then it will create a problem to recognize which id is for which layout ...
I want to load a text file in Android with custom size and color just as shown in the website (download the 1.html file). This is a HTML file and I have used WebView to show the file but I want to show using TextView. If I simply copy this in Text file it deletes the custom color and the size. Is there any easy way using Java code like loop or something to make specific lines in Red color and the other in black to make it look like it's shown in HTML File?
https://sites.google.com/site/gurbaniujagar/page1
Thanks in advance
Take a look at this site here, it shows you all you need to know.
http://javatechig.com/2013/04/07/how-to-display-html-in-android-view/
The purpose of the XML layout is confusing for me, having only worked with UI in Visual Studio with the drag-drop xml layout and C# code-behind. Should I be creating a separate xml layout for every screen (load, main, data) in my application? How do views associate with layouts? How do I reference the xml and use it in my code? I only know of the setContentView(xml layout) call.
For example, for my loading screen I want to display a background image and a loading bar. I'm thinking there's a way for the layout to contain the picture and the widget so the user sees the loading screen immediately, and in the actual code I'm initializing all my necessary variables.
If you are confused on how layout works in Android, better try reading this topic on XML Layouts and also read this tutorial about user interface in Android for a head start.
Here's also a tutorial on how to set background image to progress bar.
You should load the layout resource(main.xml) from your application code, in onCreate method of your Activity.
setContentView(R.layout.main);
Yes, you need an XML file for every page of your application.
Write a XML file for that particular page mentioning the fields required and in your .java file use this command
setContentView(R.layout.main);
I need to load a page into a webview and then apply some custom css to it. This page is not under my control, so I can't simply add a CSS include or inline CSS within the page itself.
Right now I'm just doing an HTTP get request, putting the contents into a String, prepending the string with my custom css (in style tags), and finally doing a loadDataWithBaseURL() on the string.
Is there a more elegant/efficient solution?
Thanks!
Well using what is available in the Webview, you have probably done the best that could be done.
However, if you really want to do it elegantly, get the source codes of WebView and all the classes it needs. Alter them to ultimately add the function WebView.setCSS() (for example)... It is doable but it needs some time.
you can just try WebView.reload() just like :
if(first_loading){
w.loadUrl(DisplayActiviyParam.sd_html);
first_loading = false;
}else{
w.reload();
}
,what you need do is tu change your css file contents. Good lucky!
I currently have a layout that has scrollviewer as main layout, and a relative one (with a lot of views) as my app layout, but now I need to make tabs, the android tutorial instructs to make the actual layout of each tab programatically, how do I convert my current layout (xml) to a class? Is there any guideline to follow? how do I set the relative layout inside the scrollviewer and how do I specify the "android:layout_below="SOMETHING"" and all that properties?
I din't understand your question very well but, when creating the TabLayout programaticaly you define the activities that are used for each of the tabs. Each of them should have setContentView( 'activity layout')
In order to access XML elements in Android you need to do something like the following:
Button submitButton = (Button) myActivity.getViewById(R.id.btnSubmit);
Then you can call things like submitButton.setVisibility(View.HIDDEN) to hide it, or whatever else you need to do with it.
This should work for any layout elements.
Make sure you do this in your onActivityStart method, or else you will throw runtime exceptions.
There isn't any way to automatically "convert" an XML layout file to a class, unless you're using an XML parsing algorithm of some sort, which sounds like way more than you're trying to do.
Hope this helped!