У меня есть WebView, работающий с включенным JavaScript, и я хочу вызвать функцию через JavaScript внутри WebView, которая делает элемент LinearLayout видимым, а другую функцию скрыть его
Это моя рабочая функция, которая делает егоотображается при загрузке новой страницы в WebView
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
findViewById(R.id.cargando).setVisibility(View.VISIBLE);
}
Это мой макет (довольно стандартно)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
<LinearLayout
android:id="@+id/cargando"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CC3333"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp"
tools:ignore="MissingConstraints">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Я хочу что-то вроде этого, что я могу вызвать через Javascript:
WebAppInterface.java
public class WebAppInterface extends MainActivity{
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void mostrar_cargar() {
findViewById(R.id.cargando).setVisibility(View.VISIBLE);
}
}
Но я получаю сообщение об ошибке
Cannot resolve method 'findViewById(int)'
Я не знаю, нужно ли мне добавить что-то еще к функции, которая будет вызыватьсячерез Javascript.
EDIT1:
Теперь я не получаю сообщение об ошибке после расширения своей основной деятельности, но у меня ничего не получается после вызова функции mostrar_cargar () с JavaScript.
* Когда я добавляю final LinearLayout cargando = (LinearLayout) findViewById(R.id.cargando);
ниже Context mContext;
, мое приложение не может работать.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("http://x.x.x.x");
webView.addJavascriptInterface(new WebAppInterface(this),"Android");
}
}
Спасибо за любые рекомендации или решения.