We have a button in HTML. When the user clicks it, the value of my specific EditText
should change, but it doesn't.
Code:
class ActivityTest extends Activity {
TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_res/raw/htmlimages.html");
webView.addJavascriptInterface(new MyTest(), "Scripts");
webView.getSettings().setJavaScriptEnabled(true);
textView1 = (TextView) findViewById(R.id.textView1);
}
public class MyTest {
void setText(String string)
{
textView1.setText(string);
}
}
}
EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.
This example demonstrates how do I set only numeric value for editText in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Assuming that your html part is correct one thing that you need to do is make ui calls on the main thread.
so your code:
void setText(String string)
{
textView1.setText(string);
}
should be
@JavascriptInterface
public void setText() {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("Testing text");
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With