I have some issue about WebView - java

I can't use WebView correctly. When I run my app there is "What to use" window and buttons "Browser, Chrome, Sorting" On Clicking "Отмена"/"Cancel" there is nothing happen. There are no errors in my code and I really don't know how to use WebView correctly. Please help me. "What to use" window
But I add WebView and I want to show everything in it. There is my code:
Main_Activity.java
package com.admin.zabroshki;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#SuppressLint("ResourceType")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// получим идентификатор выбранного пункта меню
int id = item.getItemId();
String urlVK = new String("https://vk.com/zabroshkiborika");
String urlInstagram = new
String("https://instagram.com/zabroshkiborika");
WebView view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setLoadWithOverviewMode(true);
view.getSettings().setUseWideViewPort(true);
switch (id)
{
case R.id.urlVK:
view.loadUrl(urlVK);
break;
case R.id.Instagram:
view.loadUrl(urlInstagram);
break;
}
return false;
}
}
activity_main.xml
<?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"
tools:context="com.admin.zabroshki.MainActivity">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

You try this.
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://vk.com/zabroshkiborika");

Related

Autosuggestion is not enable on webview using websetting

I have found it impossible to correctly set the autocompletion of fields in html forms in an android studio webview:
activity_main.xml:
<?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"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="#+id/progressbar_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:usesCleartextTraffic="true" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.example.webview;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
private ProgressBar progressBar;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.webview);
progressBar = findViewById(R.id.progressbar_id);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setGeolocationEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSaveFormData(true);
WebViewClient webViewClient = new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
myWebView.setVisibility(View.GONE);
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
myWebView.setVisibility(View.VISIBLE);
}
};
myWebView.setWebViewClient(webViewClient);
myWebView.setSoundEffectsEnabled(true);
myWebView.loadUrl("https://tcg-wallet.ga/home");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
i have try using:
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
but nothing happens there are no suggestions, I expected something like:
I don't know what I need to add to enable those suggestions!

How to Set Linear Layout with WebView for onClickListener

I have a linear layout carrying webview. I have set the id of the LinearLayout to 'lay001'.
My intention is to set the onClickListener of the layout so that onclick, the intent will send data to webview and then to the url i specified.
All my efforts to make it work failed me. Please help me set the java code. Thanks
activity_main.xml
<LinearLayout
android:id="#+id/lay001"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_margin="4dp"
android:orientation="vertical"
android:background="#drawable/layout_bg1">
<WebView
android:id="#+id/webview001"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:accessibilityPaneTitle="The Liturgy"
android:layout_marginTop="2dp">
</WebView>
<TextView
android:id="#+id/textView001"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:text="Anglican Hymns Tunes"
android:textSize="10sp"
android:textStyle="italic|bold"
android:textAlignment="center"
android:textColor="#DD1A1A"/>
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private WebView webview;
private LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
webview = (WebView001) findViewById(R.id.webview001);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://www.google.com");
}
}
});
}
}
You are missing findViewById() of linearlayout.
Try this out :
private LinearLayout myLayout;
myLayout= (LinearLayout) findViewById(R.id.lay001);
Also change this :
webview = (WebView) findViewById(R.id.webview001);
To load url :
myLayout.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
mWebview.loadUrl("your url");
}
});

toolbar not showing in webview?

I am developing an android news app and implemented toolbar as a back arrow when the user clicks should go to the back
I am developing an android news app and implemented toolbar as a back arrow when the user clicks should go to the back
below my DetailActivity where I have implemented toolbar
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import butterknife.BindView;
import butterknife.ButterKnife;
import edgar.yodgorbek.sportnews.R;
public class DetailActivity extends AppCompatActivity {
#BindView(R.id.toolbar)
Toolbar toolbar;
#BindView(R.id.imageView)
ImageView imageView;
#BindView(R.id.webView)
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
String imageUrl = getIntent().getExtras().getString("imageKey");
Picasso.get().load(imageUrl).into(imageView);
webView.getSettings().setJavaScriptEnabled(true);
String url = getIntent().getExtras().getString("urlKey");
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
below my activity_detail.xml
<android.support.design.widget.CoordinatorLayout 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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:layout_marginTop="32dp"
tools:ignore="ContentDescription" />
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView" />
</android.support.design.widget.CoordinatorLayout>

Error message: java.lang.IllegalArgumentException

I'm new at coding so I might be not very good at explaining. I'm trying to build chat app with Firebase.
Here is my code for Main activity:
Mpackage com.example.otto.myfirstapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.text.format.DateFormat;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
private static int SIGN_IN_REQUEST_CODE = 1;
private FirebaseListAdapter<ChatClass> adapter;
RelativeLayout activity_main;
FloatingActionButton fab;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_sign_out)
{
AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Snackbar.make(activity_main,"You have been signed out.", Snackbar.LENGTH_SHORT).show();
finish();
}
});
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SIGN_IN_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
Snackbar.make(activity_main,"Successfully sighned in.Welcome!", Snackbar.LENGTH_SHORT).show();
displayChatMessage();
}
else{
Snackbar.make(activity_main,"Error 1", Snackbar.LENGTH_SHORT).show();
finish();
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText input = (EditText)findViewById(R.id.input);
FirebaseDatabase.getInstance().getReference().push().setValue(new ChatClass(input.getText().toString(),FirebaseAuth.getInstance().getCurrentUser().getEmail()));
input.setText("");
}
});
if(FirebaseAuth.getInstance().getCurrentUser() == null)
{
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(),SIGN_IN_REQUEST_CODE);
}
else
{
Snackbar.make(activity_main,"Welcome " +FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
}
displayChatMessage();
}
private void displayChatMessage() {
ListView listOfMessage = (ListView)findViewById(R.id.list_of_message);
adapter = new FirebaseListAdapter<ChatClass>(this,ChatClass.class,R.layout.list_item,FirebaseDatabase.getInstance().getReference()) {
#Override
protected void populateView(View v, ChatClass model, int position) {
TextView messageText,messageUser,messageTime;
messageText = (TextView)findViewById(R.id.message_text);
messageUser = (TextView)findViewById(R.id.message_user);
messageTime = (TextView)findViewById(R.id.message_time);
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());
messageTime.setText(DateFormat.format("dd-mm-yy (HH:mm:ss)",model.getMessageTime()));
}
};
listOfMessage.setAdapter(adapter);
}
}
And code for acitivty_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#drawable/c_send"
android:id="#+id/fab"
android:tint="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
app:fabSize="mini"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/fab"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message..."
android:id="#+id/input"
/>
</android.support.design.widget.TextInputLayout>
<ListView
android:id="#+id/list_of_message"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/fab"
android:dividerHeight="16dp"
android:divider="#android:color/transparent"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
Error code from the logcat while app says that it keeps stopping tells me that problem is in the snackbar:
Caused by: java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
at android.support.design.widget.Snackbar.make(Snackbar.java:181)
at com.example.otto.myfirstapplication.MainActivity.onCreate(MainActivity.java:97)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
I updated all dependencies to the latest versions. I was thinking maybe I have to downgrade to solve a problem but I'm not sure.
Anyway I'm super stuck and hope you can help me. Thanks in advance.
Change at all places,
Snackbar.make(activity_main,...
to
Snackbar.make(findViewById(android.R.id.content),...
Cause
You did not initialize activity_main. So it is null when you use it. findViewById(android.R.id.content) gives you root view of current Activity.
First of All you have to create coordinator layout to appear your snackbar. so change your main relativelayout to coordinator layout .
<android.support.design.widget.CoordinatorLayout 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:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
//YOUR CURRENT CODE
</android.support.design.widget.CoordinatorLayout>
after that in your activity .
coordinatorLayout = (CoordinatorLayout) findViewById(R.id
.coordinatorLayout);
and make your Snackbar layout like this
Snackbar snackbar = Snackbar.make(coordinatorLayout, "Welcome to AndroidHive", Snackbar.LENGTH_LONG);
snackbar.show();
As in your code,
RelativeLayout activity_main;
you haven't initialized the activity_main, use findviewbyid in onCreate of activity
In xml change the below code, add the id in xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/container"
tools:context=".MainActivity">
and in the code fetch the id in onCreate method of Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main=findViewById(R.id.container);
}
and now use this view(activity_main) in snack bar.

No view found for id 0x7f070053

was testing som things in Android Studios and ran in to this error:
FATAL EXCEPTION: main
Process: com.meisolsson.app, PID: 25610
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meisolsson.app/com.meisolsson.app.LoginActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070053 (com.meisolsson.app:id/container) for fragment PlaceholderFragment{4284e750 #0 id=0x7f070053}
Here is my LoginActivity.java
package com.meisolsson.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.view.Window;
import android.widget.Button;
public class LoginActivity extends ActionBarActivity {
private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
this.setContentView(R.layout.fragment_login);
this.loginButton = (Button)this.findViewById(R.id.button);
this.loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
}
}
And my fragment_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".LoginActivity$PlaceholderFragment"
android:focusable="true"
android:background="#fcff00">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="#string/user"
android:padding="10dp"
android:background="#ffffff"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:layout_alignRight="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="#string/pass"
android:background="#ffffff"
android:padding="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login"
android:id="#+id/button"
style="#style/AppTheme"
android:background="#ff5900"
android:shadowColor="#000000"
android:layout_below="#+id/editText2"
android:layout_alignLeft="#+id/editText"
android:layout_alignRight="#+id/editText2"
android:clickable="true"/>
</RelativeLayout>
The problem is the way you're doing this. For the Activity, you don't set the Fragment's view, you set a separate view that has a spot to put a Fragment. Depending on how you did your PlaceHolderFragment, the following ought to get a working app.
First add this file:
activity_main.xml
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then change this stuff in your Activity:
LoginActivity.java
/* Same stuff*/
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//This is the important change right here!
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//The stuff that you had handling buttons should go in your
//PlaceHolderFragment class's onCreateView() method
}
I hope this helps. Good Luck!

Categories

Resources