I have a situation where I am creating an HTML button using JavaScript and injecting it through Java. The goal is for this button to change activity when clicked. Below is the code snippet:
public class WebsiteActivity extends AppCompatActivity implements View.OnClickListener {
Button btnActTwo;
//...
private class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void clickBtnActTwo() {
btnActTwo = (Button) findViewById(R.id.btnActTwo);
btnActTwo.setOnClickListener(WebsiteActivity.this);
Log.e("TEST","TEST");
}
//...
public void onPageFinished(WebView view, String url) {
String javascript = "javascript:"
+ "function onClickBtnActTwo() {Android.clickBtnActTwo();}"
+ "var btnacttwo = document.createElement('div');"
+ "btnacttwo.innerHTML = 'BtnAcTwo text';"
+ "btnacttwo.onclick = onClickBtnActTwo;"
+ "element = document.getElementsByClassName('contentMenu')[0];"
+ "element.appendChild(btnacttwo);"
view.loadUrl(javascript);
//...
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnActTwo:
Log.e("TEST2","TEST2");
Intent btnActTwoIntent = new Intent(this, BtnActTwoActivity.class);
startActivity(btnActTwoIntent);
break;
//...
When testing, the console displays the TEST tag when pushing the dynamically created div button, but not TEST2. It seems like there might be an issue with implementing the functionality from injected JavaScript. Any suggestions on how to make this work seamlessly?