My webview doesn't start after creating progress bar - java

I created my new webapp. It worked well. But I wanted to add a progress bar. The progress bar works, but my webview doesn't show anynore. I don't understand why. Can someone may help, please?
I tried all the codes I found online. They are ok for the others, but not for me.
Here my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:fullBackupOnly="true"
android:icon="#drawable/donorionepng"
android:label="PCDO GENOVA"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.PCDOPCDOGENOVA"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:exported="true"/>
<activity
android:name=".SplashActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here my MainActivity.java
package it.donorionegenova.pcdopcdogenova;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.pm.ActivityInfo;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ClientCertRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.core.content.res.ResourcesCompat;
import com.onesignal.OneSignal;
import android.annotation.SuppressLint;
public class MainActivity extends AppCompatActivity {
private static final String ONESIGNAL_APP_ID = "........ de2";
private SwipeRefreshLayout swipeRefreshLayout;
private WebView webView;
ProgressBar progressBar;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressBar progressBar = findViewById(R.id.prBar);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.donorione-genova.it/home-app");
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
if (newProgress == 100) {
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
} else {
webView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
}
});
TextView textView = findViewById(R.id.textview);
textView.postDelayed(() -> textView.setText("loading..."), 500); // delay of 2 seconds before setting a text to textView
textView.postDelayed(() -> textView.setVisibility(View.GONE), 2000);
// Enable verbose OneSignal logging to debug issues if needed.
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
// OneSignal Initialization
OneSignal.initWithContext(this);
OneSignal.setAppId(ONESIGNAL_APP_ID);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDisplayZoomControls(false);
settings.supportZoom();
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
// settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setDomStorageEnabled(true);
settings.setLoadsImagesAutomatically(true);
webView.clearHistory();
webView.clearCache(true);
registerForContextMenu(webView);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.donorione-genova.it/home-app");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView View, WebResourceRequest request) {
final Uri uri = request.getUrl();
if (uri.toString().startsWith("mailto:")) {
//Handle mail Urls
startActivity(new Intent(Intent.ACTION_SENDTO, uri));
} else if (uri.toString().startsWith("tel:")) {
//Handle telephony Urls
startActivity(new Intent(Intent.ACTION_DIAL, uri));
} else {
//Handle Web Urls
webView.loadUrl(uri.toString());
}
return true;
}
});
//per fare il refresh
swipeRefreshLayout = findViewById(R.id.swipe);
swipeRefreshLayout.setOnRefreshListener(() -> {
swipeRefreshLayout.setRefreshing(true);
new Handler().postDelayed(() -> {
swipeRefreshLayout.setRefreshing(false);
webView.reload();
}, 2000);
});
swipeRefreshLayout.setColorSchemeColors(
ResourcesCompat.getColor(
getResources(), R.color.holo_blue_dark, null),
ResourcesCompat.getColor(
getResources(), R.color.holo_orange_dark, null),
ResourcesCompat.getColor(
getResources(), R.color.holo_green_dark, null),
ResourcesCompat.getColor(
getResources(), R.color.holo_red_dark, null));
}
public void gohome(View v)
{
webView.loadUrl("https://donorione-genova.it/home-app");
}
public void clickexit(View v)
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
}
else
super.onBackPressed();
}
}
And here my ActivityMain:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/prBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
<WebView
android:id="#+id/webView"
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"
android:visibility="visible" >
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<Button
android:id="#+id/button"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:background="#0063B1"
android:onClick="clickexit"
android:text="#string/Chiudi"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="#+id/button2">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:background="#0063B1"
android:onClick="gohome"
android:text="#string/Home"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent">
</Button>
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#0000ff"
android:textSize="40sp"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>

I didn't use SwipeRefreshLayout, but the documentation says you need to include another layout inside it. As you added a second view inside, you are now using it as a default ViewGroup, which is probably not working as expected. Put a LinearLayout or whatever inside the SwipeRefreshLayout and your components inside your LinearLayout.

Related

Android WebView Progress bar + Swipe Refresh

I am new on Android JAVA programming, trying to make this code to work. I tried all the combinations from StackOverflow, and this is the best I got. I manage to get webView and SwipeRefresh to work, but the horizontal progress bar doesn't work, and I don't know why. It just doesn't show when I refresh. Here is the code
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">
<ProgressBar
android:id="#+id/progressbar"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="3dp" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefreshlayout"
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:layout_centerInParent="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
MainActivity.java
package com.example.test;
import android.graphics.Bitmap;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
SwipeRefreshLayout swipeRefreshLayout;
ProgressBar progressBar;
WebView webView;
String url = "https://www.google.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
progressBar = findViewById(R.id.progressbar);
webView = findViewById(R.id.webView);
//setting webviewclient
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//To open hyperlink in existing WebView
view.loadUrl(request.getUrl().toString());
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
});
//setting webchromeclient
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView webView, int newProgress) {
progressBar.setProgress(newProgress);
}
});
//setting other settings
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl(url);
//setting swiperefreshlistener
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
}
public void onBackPressed (){
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks
Your progressbar is behind the webview
<?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">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefreshlayout"
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:layout_centerInParent="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ProgressBar
android:id="#+id/progressbar"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="3dp"
android:visibility="gone"
tools:visibility="visible" />
</RelativeLayout>

App working on Nougat+ versions but crashing on Marshmallow and Lollipop

This is the error report whenever I launch the app it crashes and says
Unfortunately, {app-name} has stopped
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2413)
at android.app.ActivityThread.access$800 (ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1317)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:135)
at android.app.ActivityThread.main (ActivityThread.java:5343)
at java.lang.reflect.Method.invoke (Native Method)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:700)
Caused by: android.view.InflateException:
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate (LayoutInflater.java:806)
at android.view.LayoutInflater.inflate (LayoutInflater.java:504)
at android.view.LayoutInflater.inflate (LayoutInflater.java:414)
at android.view.LayoutInflater.inflate (LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView (AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView (AppCompatActivity.java:139)
at com.khokhar.yousaf.cipher.Splash.onCreate (Splash.java:15)
at android.app.Activity.performCreate (Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2292)
Caused by: android.content.res.Resources$NotFoundException:
at android.content.res.Resources.loadDrawableForCookie (Resources.java:2444)
at android.content.res.Resources.loadDrawable (Resources.java:2384)
at android.content.res.TypedArray.getDrawable (TypedArray.java:749)
at android.view.View.<init> (View.java:3742)
at android.widget.ImageView.<init> (ImageView.java:149)
at android.widget.ImageView.<init> (ImageView.java:145)
at android.support.v7.widget.AppCompatImageView.<init> (AppCompatImageView.java:71)
at android.support.v7.widget.AppCompatImageView.<init> (AppCompatImageView.java:67)
at android.support.v7.app.AppCompatViewInflater.createImageView (AppCompatViewInflater.java:181)
at android.support.v7.app.AppCompatViewInflater.createView (AppCompatViewInflater.java:105)
at android.support.v7.app.AppCompatDelegateImplV9.createView (AppCompatDelegateImplV9.java:1035)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView (AppCompatDelegateImplV9.java:1092)
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:725)
This is my Splash Screen coding
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_splash);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//thread for splash screen running
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(2500);
} catch (InterruptedException e) {
Log.d("Exception", "Exception" + e);
} finally {
startActivity(new Intent(Splash.this, MainActivity.class));
}
finish();
}
};
logoTimer.start();
}
}
And this XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/splashScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:orientation="vertical"
tools:context="com.khokhar.yousaf.cipher.Splash">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher_foreground"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/mediumload" />
</RelativeLayout>
The drawable image I used is 600x1024 PNG
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.khokhar.yousaf.cipher">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:hardwareAccelerated="true"
android:icon="#drawable/cee"
android:label="#string/app_name"
android:roundIcon="#drawable/cee"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "java-lang-programming://android-app-google-plus-demo"-->
<data
android:host="cipherengineers"
android:scheme="android-app-cipher-engineers" />
</intent-filter>
</activity>
<activity android:name=".Splash"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
My MainActivity is basically webview
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
WebView webView;
String url="https://website/";
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
webView = (WebView) findViewById(R.id.webView);
textView = (TextView) findViewById(R.id.textView1212);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
textView.setText(progress + "");
}
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(MainActivity.this);
newWebView.getSettings().setJavaScriptEnabled(true);
newWebView.getSettings().setSupportZoom(true);
newWebView.getSettings().setBuiltInZoomControls(true);
newWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
newWebView.getSettings().setSupportMultipleWindows(true);
view.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
return true;
}
});
webView.setWebViewClient(new MyWebViewClient());
WebSettings browserSetting = webView.getSettings();
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
browserSetting.setJavaScriptCanOpenWindowsAutomatically(true);
browserSetting.setJavaScriptEnabled(true);
browserSetting.setLoadWithOverviewMode(true);
browserSetting.setSupportMultipleWindows(true);
browserSetting.setDatabaseEnabled(true);
browserSetting.setAppCacheEnabled(true);
browserSetting.setDomStorageEnabled(true);
browserSetting.setDomStorageEnabled(true);
browserSetting.setGeolocationEnabled(true);
browserSetting.setSaveFormData(false);
browserSetting.setUseWideViewPort(true);
browserSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
browserSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
browserSetting.setDomStorageEnabled(true);
if (android.os.Build.VERSION.SDK_INT >= 21) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
}else {
CookieManager.getInstance().setAcceptCookie(true);
}
webView.loadUrl(url);
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.INVISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
#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) && webView.canGoBack()) {
webView.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);
}
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Exit");
// set dialog message
alertDialogBuilder
.setMessage("Are You Sure?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.GRAY));
}
}
I removed background from Splash Screen XML and nothing worked
MainActivity XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/loading_screen"
android:orientation="vertical"
tools:context="com.khokhar.yousaf.cipher.MainActivity">
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="218dp"
android:progressTint="?android:attr/fastScrollTextColor"
android:visibility="invisible" />
<TextView
android:id="#+id/textView1212"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:textColor="#android:color/holo_red_dark"
android:textSize="13sp"
android:textStyle="bold|italic" />
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:animationCache="true"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="41dp" />
</RelativeLayout>
I can't figure it out.
Removing the background from ImageView
The reason is lower versions are not able to find ic_launcher_foreground in the drawable directory which actually exists in drawable-v24.
Remove the background image and write your ImageView like this
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/mediumload" />
you cant start a new Activity on Background Thread.. since you run new Thread and inside you start a new Activity it will crash..
2 solutions : declare Handler outside the Thread
Handler handler = new Handler();
inside the thread run
handler.post(new Runnable() { public void run() { startActivity(..) } });
or use function :
RunOnUiThread(new Runnable() { public void run() { startActivity(...) } );

Cannot get android studio app to start login screen after initial splash screen

I am new to java coding and Android Studio, so please bear with me. However, I am trying to get a login screen to start after the splash screen and the app crashes after the splash screen. The splash screen works no problem. Anyways, here is the first set of code and this is the splash screen code in Main activity.
package com.example.xxxx.safetyxxxxxxx;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
tv = (TextView) findViewById(R.id.tv);
iv = (ImageView) findViewById(R.id.imageView);
Animation mine = AnimationUtils.loadAnimation(this, R.anim.transition);
final Intent go = new Intent(MainActivity.this, LoginPageActivity.class);
//set duration ... 1 second ... :p
mine.setDuration(1000);
tv.startAnimation(mine);
iv.startAnimation(mine);
//make a thread to go to second activity...
Thread t = new Thread() {
#Override
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
MainActivity.this.startActivity(go);
finish();
}
}
};
t.start();
}
}
this is the Login Page activity called "LoginPageActivity.java" that I would like to have the app go to after the splash screen.
package com.example.xxxx.safetyxxxxxxx;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import static com.example.xxxx.safetyxxxxxxx.R.layout.activity_login_page;
public class LoginPageActivity extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_login_page);
findViewById(R.id.textViewSignUp).setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.textViewSignUp:
startActivity(new Intent(this, SignUpActivity.class));
break;
}
}
}
This is the androidmanifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxx.safetyxxxxxxx">
<application
android:name=".Database"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginPageActivity"
android:label="#string/app_name">
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".SignUpActivity" />
</application>
</manifest>
Here is the logcat error
1-28 12:55:33.490 7647-7670/com.example.xxxx.safetyxxxxxxx E/AndroidRuntime: FATAL EXCEPTION: Thread-5
Process: com.example.xxxx.safetyxxxxxxx, PID: 7647
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.xxxx.safetyxxxxxxx/com.example.xxxx.safetyxxxxxxx.LoginPageActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1932)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1615)
at android.app.Activity.startActivityForResult(Activity.java:4472)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4430)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4791)
at android.app.Activity.startActivity(Activity.java:4759)
at com.example.mike.safetychecker.MainActivity$1.run(MainActivity.java:39)
Also if needed the file activity_login_page.xml code is below
<?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=".LoginPageActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Hi, Welcome to Safety xxxxxx Please Login or Signup"
android:textAlignment="center"
android:textColor="#android:color/holo_green_dark"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.995"
tools:layout_editor_absoluteX="0dp"
tools:ignore="MissingConstraints" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="398dp"
tools:ignore="MissingConstraints" />
<EditText
android:id="#+id/editTextEmail"
android:layout_width="346dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="email"
android:inputType="textEmailAddress"
android:text=" email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="270dp"
tools:ignore="MissingConstraints" />
<EditText
android:id="#+id/Password"
android:layout_width="346dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:inputType="textVisiblePassword"
android:text=" Password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="336dp"
tools:ignore="MissingConstraints" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="219dp"
android:layout_height="229dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/editTextEmail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/safetyxxxx" />
<TextView
android:id="#+id/textViewSignUp"
android:layout_width="351dp"
android:layout_height="34dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Do Not Have An Account? Click Here"
android:textAlignment="center"
android:textColor="#android:color/holo_green_dark"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.529"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="459dp"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
Also there is this transition.xml file contained in a anim folder that might help
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
/>
you have to try to open your LoginPageActivity like this way
Paste this code
openActivity();
instead of this
Thread t = new Thread() {
#Override
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
MainActivity.this.startActivity(go);
finish();
}
}
};
t.start();
and put this method
public void openActivity()
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent go = new Intent(MainActivity.this,LoginPageActivity.class);
startActivity(go);
finish();
}
}, 5000);
}
import this packages
import android.os.Bundle;
import android.os.Handler;
and also mention in your manifest like this
<activity android:name=".LoginPageActivity"
android:label="#string/app_name"/>
You have to declare your Activity in your AndroidManifest.xml. Almost all of your system-related classes have to be declared in it, this includes Activities, Services, BroadCast Receivers. You can read up on how Manifest works over here
Back to your problem, you can fix it by add the following line inside the application tag in your Android Manifest. Remove any intent filters you have applied to it
<activity android:name=".LoginPageActivity" />
Remove :
<intent-filter>
<action android:name="com.example.xxxx.safetyxxxxxxx.LoginPageActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Because there is always remains only one LAUNCHER activity in application. And also intent filter is wrong there.
change LAUNCHER TO DEFAULT in your mainfest, Two launcher is not possible at same time (Assuming MainActivity as LAUNCHER)
</activity android:name=".LoginPageActivity>
<intent-filter>
<action android:name="com.example.xxxx.safetyxxxxxxx.LoginPageActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
use this
<activity android:name=".LoginPageActivity"
android:label="#string/app_name"/>
instead of
<activity android:name=".LoginPageActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.xxxx.safetyxxxxxxx.LoginPageActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this,com.example.xxxx.safetyxxxxxxx.LoginPageActivity.class);
startActivity(intent);
finish();
}
}, 5000);
//where 5000 is the delayed time

Android - Store variable and use in webview link

I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.
Here's what I have so far;
LoginActivity.java
package com.example.app;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class LoginActivity extends Activity {
EditText subdomain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subdomain = (EditText) findViewById(R.id.subdomain);
Button btn=(Button)findViewById(R.id.sign_in_button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(LoginActivity.this,
MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
public void saveInfo (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", YourSchool.getText().toString());
editor.commit();
}
}
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a
browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");
// Stop local links and redirects from opening in browser instead
of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void displayData (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
String client_subdomain = sharedPref.getString("name", "");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_login.xml
<LinearLayout
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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.app.LoginActivity">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/YourSchool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your school"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="webView"
android:text="SIGN IN"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
activity_main.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"
tools:context=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="#string/app_name"></activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain
I think it's probably something small that I've missed out but any help would be greatly appreciated.
Many thanks,
Sam
Store the variable using sharedPrefeence as below,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("your_string_key", yourStringValue);
editor.commit();
Then retrieve the value as,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
String myStringValue = sp.getInt("your_string_key", -1);
Use the variable in the loadUrl as
mWebView.loadUrl("https://"+myStringValue+".mydomain.co.uk");

Android: StoredPreferences + WebView Link

I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.
Here's what I have so far;
LoginActivity.java
package com.example.app;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class LoginActivity extends Activity {
EditText subdomain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subdomain = (EditText) findViewById(R.id.subdomain);
Button btn=(Button)findViewById(R.id.sign_in_button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(LoginActivity.this,
MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
public void saveInfo (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", YourSchool.getText().toString());
editor.commit();
}
}
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a
browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");
// Stop local links and redirects from opening in browser instead
of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void displayData (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
String client_subdomain = sharedPref.getString("name", "");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_login.xml
<LinearLayout
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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.app.LoginActivity">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/YourSchool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your school"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="webView"
android:text="SIGN IN"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
activity_main.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"
tools:context=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="#string/app_name"></activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain
I think it's probably something small that I've missed out but any help would be greatly appreciated.
Many thanks,
Sam
Its because haven't declared "client_subdomain" as a variable within scope of onCreate
why don't you try changing displayData to
public String displayData () {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
return sharedPref.getString("name", "");
}
and in onCreate of the Main Activity
...
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+displayData()+".domain.co.uk/texts");
...
Also, I don't see you ever calling "saveInfo" anywhere in your Login Activity. As well, you should probably change the function too. You aren't grabbing the text from the EditText correctly (whatever YourSchool) is
public void saveInfo() {
SharedPreferences sharedPref = getSharedPreferences("spfile", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", subdomain.getText().toString());
editor.commit();
}
And this will be what your onCreate should have,
protected void onCreate(Bundle savedInstanceState) {
...
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
saveInfo();
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
...
}

Categories