Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  162] [ 1]  / answers: 1 / hits: 93119  / 14 Years ago, sun, october 31, 2010, 12:00:00

I'm not highly familiar with javascript but I think this is the best way to accomplish my purpose. If not, please correct me.



I have a licence text 2 buttons at the end. All of this is written in HTML in a WebView because there are some links in the licence. Now, I want that when the user clicks the ok button in the WebView, this triggers some javascript or listener that I can grab in Java to fire an Intent to go forward in the application. (The cancel button would do the opposite, but if I know how to do one, I can do the other. ;) )



Does this rings any bell to someone?
Any explanation or sample code is welcome.


More From » android

 Answers
55

I finally got it on my own after some reading. Kind of hard when you know nothing about javascript and when the doc is quite thin on the subject.

Here is my solution, hope this will help others :



With an HTML page containing 2 buttons at the end like that :



<div>
<button type=button id=ok style=font-weight: 700; margin-right: 20px; onclick=validClick();>J'accepte</button>
<button type=button id=no onclick=refuseClick();>Je refuse</button>
</div>


I send the event of the click to the javascript at the top of my HTML page :



<script language=javascript>

function validClick()
{
valid.performClick();
document.getElementById(ok).value = J'accepte;
}
function refuseClick()
{
refuse.performClick();
document.getElementById(no).value = Je refuse;
}

</script>


valid and refuse are 2 java objects that I passed through the javascript interface to use their methods. So in java, I created 2 buttons (not really displayed in the Activity, only here for their methods and are sort of shadows of the HTML buttons :



valid = new Button(ctx);
valid.setOnClickListener(this);
refuse = new Button(ctx);
refuse.setOnClickListener(this);


Then I added javascript to my WebView :



// Enablejavascript
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
// Add the interface to record javascript events
wv.addJavascriptInterface(valid, valid);
wv.addJavascriptInterface(refuse, refuse);


And finally, handle the click events in the onClick method :



@Override
public void onClick(View v) {
if (v.equals(valid)) {
//do Something
} else if (v.equals(refuse)) {
//do Something else }
}


Hope this will help some people


[#95124] Thursday, October 28, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;