Reputation: 211
So I've gone through the basic tutorial and I've read up on activities and UI listeners.
However I'm still unsure on how to open an HTML page when I select a button.
What I think I have to do is, make an HTML file and store it in the res/layout folder, create an activity which is connected to the HTML file. Then I'm not sure what to do. Where do I call the OnClickListener
function and when do I handle the event of the button being clicked?
Upvotes: 1
Views: 1014
Reputation: 1007474
You really should consider starting with something simpler, as an apparent newcomer to Android.
Be that as it may...
First, you need to decide where the "HTML page" is: on the phone, or on the Internet. For right now, the correct answer is "on the Internet". You will need the URL for it (e.g., http://www.ohai.com/kthxbye.html
).
Then, you need to create a layout file, in the res/layout/
folder, containing your Button
. This is not an HTML file -- it is an Android layout file. Assuming you are going to run on Android 1.6 and newer devices, you can add an attribute to the <Button>
element, named android:onClick
, that will name a method in your soon-to-be-written Activity
class. If you are just working with a new project, you might as well just modify the res/layout/main.xml
file that is supplied to you to have this Button
.
Then, you need to create an Activity
class. Your project hopefully already has an Activity
class set up for you; if not, you will need to create one yourself. In onCreate()
, use setContentView(R.layout.main)
(or whatever) to load up the layout file.
Add a method, of the name you specified in android:onClick
, to your Activity
, returning void
and taking a View
as a parameter. In there, add in a line like:
startActivity(new Intent(this, Uri.parse("...")));
where ...
is replaced with the URL to your HTML file.
Now, it is possible to have the HTML baked into your application, but then you will have a fair bit of difficulty getting the browser application to view it, since that application has no right to your application's files. There are a few ways to deal with that, all of which are relatively complicated.
Upvotes: 2