Webview app turns off after Splash Screen Android Studio - java

I am creating a Webview application and I need to add Splash Screen at load time.
I tried different ways, with this it "starts", but after Splash Screen it just turns off. What can I do to make the application show Splash Screen and open correctly?
SplashScreen.java:
package com.example.evrika;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashScreen.this,MainActivity.class));
finish();
}
},1000);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.evrika;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
WebView webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.loadUrl("https://evrikaspace.ru/");
WebViewClient webViewClient = new WebViewClient() {
#SuppressWarnings("deprecation") #Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N) #Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
};
webView.setWebViewClient(webViewClient);
}
}

Probably this line is making noise getSupportActionBar().hide();

Related

WebView will not load when in another layout, but works fine in main

I am working on an android project that reqires a home screen that I just have image buttons on for the time being, but would like to access different layouts, it access the different layouts just fine, but when using the same code for webview, webview refuses to load and just displays a blank screen, the same code worked fine on the main activity, but is giving me trouble now and im not sure where to go from here, I only have an image on the second activity as a sanity check that my buttons were going to the right places
Main Activity
package com.example.bsc_group;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
ImageButton foodButton = (ImageButton) findViewById(R.id.imageButtonFood);
foodButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
ImageButton techButton = (ImageButton) findViewById(R.id.imageButtonTech);
techButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
ImageButton calendarButton = (ImageButton) findViewById(R.id.imageButtonCalendar);
calendarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.bcs_calendar_widget);
}
});
}
}
Second activity
package com.example.bsc_group;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class BcsCalendarWidget extends AppCompatActivity {
public WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
webView = findViewById(R.id.webviewWidget);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://google.com");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
setContentView(R.layout.bcs_calendar_widget);
{
//Spinner spinnerBcsListings = findViewById(R.id.spinner_bcs_group);
//ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.bcs_group_listings, android.R.layout.simple_spinner_item);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
//spinnerBcsListings.setAdapter(adapter);
}
}
#Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
}else {
setContentView(R.layout.activity_main);}
}
}
XML file for second activity
<?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">
<WebView
android:id="#+id/webviewWidget"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher_foreground" />
</RelativeLayout>
The issue is that you are not opening the activity. Only giving a context view without executing the code. This bad. Whenever you want to go to another activity, use this code:
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
Also in the onBackpressed I see that you are just doing setContentView. Don't do that. Use this instead:
finish();

Was trying to show the ambient temparature inside the textView, but onSensorChanged method is not working, its not setting the text of textview

I am trying to show results in text view but it's not showing temperature in text view. My device has a sensor,when I set text through the if statement inside oncreate it works. It's just onSensorchanged that is not working, even tried to make toast inside the onSensorChanged but it's not working.
package com.example.lab4sensor;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private TextView txtview;
private SensorManager sensormanager;
private Sensor sensOR;
private boolean sensorAvailable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtview = findViewById(R.id.txtTemp);
sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
if(sensormanager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE)!=null){
sensOR = sensormanager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
//txtview.setText("set temp");
sensorAvailable = true;
}else{
txtview.setText("Temparature Sensor is not available");
sensorAvailable = false;
}
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
txtview.setText(sensorEvent.values[0]+" °C");
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
protected void onResume() {
super.onResume();
if(sensorAvailable = true){
sensormanager.registerListener(this,sensOR,SensorManager.SENSOR_DELAY_NORMAL);
}else{
Toast.makeText(this,"Not available",Toast.LENGTH_LONG).show();
}
}
#Override
protected void onPause() {
super.onPause();
if(sensorAvailable =true){
sensormanager.unregisterListener(this);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:id="#+id/txtTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

No view found for id for fragment in Preference setting screen

In my application I use DraverLayout as a main activity, which contains fragments as a parts of navigationView.
But for settings I use SettingActivity which contains SettingFragment. Problem appears when I try to open subscreen from settings screen:
"java.lang.IllegalArgumentException: No view found for id 0x7f09014e (com.example.tipcollector:id/settings_container) for fragment NotificationMenuFragment{1fe50ef} (618cc8dc-6287-4830-b9a1-0e9b2d1d1827) id=0x7f09014e}". What was done incorrectly and how can i fix it? I will appreciate any help or information!
SettingActivity.java
package com.example.tipcollector;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import android.os.Bundle;
import com.example.tipcollector.Notification.NotificationMenuFragment;
public class SettingsActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
toolbar = findViewById(R.id.mainToolbar);
setSupportActionBar(toolbar);
if(findViewById(R.id.settings_container)!=null){
if(savedInstanceState!=null)
return;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, new SettingsFragment()).commit();
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
final Bundle args = pref.getExtras();
final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
getClassLoader(),
pref.getFragment());
fragment.setArguments(args);
fragment.setTargetFragment(caller,0);
getSupportFragmentManager().beginTransaction()
.replace(R.id.settings_container, fragment)
.addToBackStack(null)
.commit();
return true;
}
}
Here is my SettingsFragment
package com.example.tipcollector;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import com.example.tipcollector.Notification.NotificationMenuFragment;
import database.DataBaseHelper;
public class SettingsFragment extends PreferenceFragmentCompat implements PreferenceManager.OnPreferenceTreeClickListener {
public static final String PREF_HOURLY_RATE = "hourly_rate_key";
public static final String PREF_CURRENCY = "currencies";
public static final String PREF_NOTIFICATION = "notifications";
private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onPreferenceTreeClick(Preference preference) {
String key = preference.getKey();
if ("clear_all_key".equals(key)) {}
if(PREF_NOTIFICATION.equals(key)){
getChildFragmentManager().beginTransaction().replace(R.id.settings_container,new NotificationMenuFragment()).commit();
}
return true;
}
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferencesscreen,rootKey);
preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(PREF_HOURLY_RATE)){
EditTextPreference hourlyRatePref = findPreference(key);
hourlyRatePref.setSummary(sharedPreferences.getString(key,""));
}else if(key.equals(PREF_CURRENCY)){
ListPreference currencyPref = findPreference(key);
currencyPref.setSummary(sharedPreferences.getString(key,""));
}
}
};
}
And xml files for both. Here is preference screen (layout for SettingFragment)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:title="#string/setting_general"
/>
<ListPreference
android:entryValues="#array/currencies"
android:entries="#array/currencies"
app:title="#string/select_currency"
app:key="currencies"
android:icon="#drawable/ic_money_black"
android:defaultValue="PLN"/>
<EditTextPreference
android:summary="#string/settings_general_hourly_rate"
android:title="#string/your_hourly_rate"
app:key="hourly_rate_key"
android:icon="#drawable/ic_business_center_black_24dp"
/>
<PreferenceCategory
android:title="#string/data_settings"/>
<Preference
android:title="#string/clear_all_database"
app:key="clear_all_key"
android:summary="#string/clear_all_data_message"
android:icon="#drawable/ic_delete_forever_black_24dp"/>
<PreferenceCategory
android:title="Notification"/>
<Preference
android:title="Notification"
app:key="notification"
android:icon="#drawable/ic_notifications_black_24dp"/>
</PreferenceScreen>
And here is SettingActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".SettingsActivity"
android:orientation="vertical">
<include
layout="#layout/main_toolbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/settings_container"/>
</LinearLayout>
Changes to be done in your preferencesscreen.xml
<Preference
android:title="Notification"
app:key="notification"
android:icon="#drawable/ic_notifications_black_24dp"
app:fragment="com.example.tipcollector.Notification.NotificationMenuFragment" />
Also, remove this method from your SettingsFragment
#Override
public boolean onPreferenceTreeClick(Preference preference) {
String key = preference.getKey();
if ("clear_all_key".equals(key)) {}
if(PREF_NOTIFICATION.equals(key)){
getChildFragmentManager().beginTransaction().replace(R.id.settings_container,new NotificationMenuFragment()).commit();
}
return true;
}
You don't need this since preferenceClick will be handled by this tag in xml
app:fragment="com.example.tipcollector.Notification.NotificationMenuFragment"

Admob ads not visible with app using webview and progressbar

I have tried making an app for my website with a webview and progressbar, the visibility of the webview remains hidden till the page loads. Now I have tried adding google admob banner ad at the botton and it isn't showing up, I am using a relativelayout and the ad shows in preview of xml, but not in app.
The activity_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">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone" />
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="#000000"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3467064285652442/5673227109">
</com.google.android.gms.ads.AdView>
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:layout_constraintStart_toStartOf="#+id/activity_main_webview"
app:layout_constraintTop_toTopOf="#+id/activity_main_webview" />
</RelativeLayout>
The MainActivity.java:
package com.dhruv.spadebee;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
import android.util.Log;
import android.view.MenuItem;
import android.view.Window;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import android.view.View;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
String ShowOrHideWebViewInitialUse = "show";
private WebView mWebView;
private ProgressBar spinner;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#080808")));
final ImageView img = (ImageView) findViewById(R.id.toolbar_top);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, img);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
mWebView.loadUrl("https://spadebee.com/");
break;
case R.id.two:
mWebView.loadUrl("https://spadebee.com/category/general/");
break;
case R.id.three:
mWebView.loadUrl("https://spadebee.com/category/programming/");
break;
case R.id.four:
mWebView.loadUrl("https://spadebee.com/category/gaming/");
break;
case R.id.five:
mWebView.loadUrl("https://spadebee.com/category/android/");
break;
case R.id.six:
mWebView.loadUrl("https://spadebee.com/about");
break;
}
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Stop local links and redirects from opening in browser instead of WebView
spinner = (ProgressBar)findViewById(R.id.progressBar1);
mWebView.setWebViewClient(new MyAppWebViewClient() {
#Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
if (ShowOrHideWebViewInitialUse.equals("show")) {
webview.setVisibility(webview.INVISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url)
{
mWebView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByClassName('custom-header')[0].style.display='none'; " +
"})()");
mWebView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByClassName('main-navigation')[0].style.display='none'; " +
"})()");
spinner.setVisibility(View.GONE);
mWebView.setVisibility(WebView.VISIBLE);
super.onPageFinished(view, url);
}
});
mWebView.loadUrl("https://spadebee.com/");
}
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
#Override
public void notificationOpened(OSNotificationOpenResult result) {
Log.i("OSNotificationPayload", "result.notification.payload.toJSONObject().toString(): " + result.notification.payload.toJSONObject().toString());
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
String appURL = result.notification.payload.launchURL;
Log.i("appURL", appURL);
mWebView.loadUrl("https://spadebee.com");
mWebView.setWebViewClient(new MyAppWebViewClient());
}
}
public class jparser {
public void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url = args[0];
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href].more-link");
Elements media = doc.select("img");
Elements summary = doc.select("div.entry-summary");
}
}
}
The preview:
Android Studio preview
Any help as to why it isn't working will be appreciated, I have tried going through all the posts here, adding progresbar and webview in a linear layout, setting adviews visibility to visible but nothing works.
Locking at your log output the last line
I/Ads: Ad failed to load : 3
states that there went something wrong while loading the ad, so there probably is nothing to show?
As stated in this post failed to load ad : 3 regarding error "3":
If you are getting this error, then your code is correct. The issue is that AdMob does not always have an ad to return for every request.
So maybe this is just a temporary problem, have you tried your app on a different device or after a little break?
Also you could try to use the android sample ad ids
https://developers.google.com/admob/android/test-ads#sample_ad_units
they usually work

show splash screen while Webview is loading in background

I'm using below code to populate my Main activity with Webview, but as it takes a certain time to load the page and it appears white blank. So I will like to show splash screen for my Webview before page load finishes. I'm not using any webview from Layout activity.
package com.faraksoch.sagar.facebook;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebview = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview.loadUrl("http://www.google.com//");
setContentView(mWebview);
}
}
I know that this code should work but I when I combine them..it won't work
WebView wv = (WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
...
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView1).setVisibility(View.VISIBLE);
}
});
wv.loadUrl("http://yoururlhere.com");
Any help will be appreciated. Thanks in advance.
Make sure webview is visibility is not GONE before it is loading.
Please refer below code.
public class WebViewActivity extends AppCompatActivity {
private WebView mWebView;
private ImageView mSplashView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
mWebView = (WebView) findViewById(R.id.webview);
mSplashView = (ImageView) findViewById(R.id.splash_view);
mWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mSplashView.setVisibility(View.GONE);
mWebView.setVisibility(View.VISIBLE);
Toast.makeText(getBaseContext(), "Page Loaded.", Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl("http://yoururlhere.com");
mWebView.setVisibility(View.GONE);
mSplashView.setVisibility(View.VISIBLE);
}
}
Layout:
<?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">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
/>
<ImageView
android:id="#+id/splash_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#mipmap/ic_launcher"
android:visibility="gone"
/>
</android.support.constraint.ConstraintLayout>

Categories