0

I'm new to Android Studios and I've been trying to develop an app based on a WordPress website. I've managed to come up with enough coding to make it work, but I'd like to know how I can improve the code. Specifically, I'd like to know how to set configurations (so the app doesn't reset on orientation change), create an error display for when there is no internet connection, and send notifications for when articles on the website get uploaded. If anyone can help that would be appreciated.

Here is my Main Activity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.res.Configuration;
import android.net.Uri;
import android.content.Intent;
import android.content.Context;
import android.net.ConnectivityManager;
import android.widget.Toast;
import android.net.NetworkInfo;
import android.view.Gravity;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    private WebView mywebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings= mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebView.loadUrl("https://71Republic.com/");
        mywebView.setWebViewClient(new WebViewClient());
    }
    public void onBackPressed() {
        if(mywebView.canGoBack())
        {
            mywebView.goBack();
        }

        else{
            super.onBackPressed();
        }
    }

}

Here is my Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.a71republic.a71republicappandroid">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <application
        android:configChanges="keyboardHidden|orientation|screenSize"
        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>
    </application>

</manifest>

Here is my Activity Main

<?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">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

Any help would be appreciated

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • Hi, welcome to stack overflow. Please refer the [ask] link for more details on how to ask a question and update your question accordingly. – Jeroen Heier Aug 02 '18 at 17:05

1 Answers1

0

Configuration changes:
You have to mention the screen-orientation attribute to portrait(or landscape as your requirement) for each activity in the manifest file.

Error display on no internet connection:

    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        //your error message
    }


For detailed explanation refer to this link: Stackoverflow No internet connection android

Deepak Kumar
  • 677
  • 1
  • 8
  • 22