Android app fullscreen with no Title bar and no Action bar

时间:2016-02-03 03:07:37

标签: android

I am trying to create my first Android app with a WebView. The last issue I am trying to resolve is to have the WebView in fullscreen with no title bar and no action bar.

My Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.butlerv2">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar">

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

</manifest>

My MainActivity:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;


public class MainActivity extends AppCompatActivity {

String answer;

private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);


    ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            answer = "You are connected to a WiFi Network";
        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            answer = "You are connected to a Mobile Network";
    } else
        answer = "No WIFI Connectivity";

    Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG).show();


    WebView webView = (WebView)
            findViewById(R.id._webview);

    WebSettings webSettings = webView.getSettings();
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.domain.com/app/index.php"); 
    webView.setWebViewClient(new MyBrowser());
    webView.requestFocus(View.FOCUSABLES_ALL);



    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
public void onStart() {
    super.onStart();


    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, 
            "Main Page", 

            Uri.parse("android-app://com.domain.butlerv2/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();


    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, 
            "Main Page", shown.

            Uri.parse("http://host/path"),

            Uri.parse("android-app://com.domain.butlerv2/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}

My Style

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>



<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="generalnotitle" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
</style>

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>



</resources>

When I run the App on a Android phone the Title and Action bar are displayed. Can anyone see what I am doing wrong.

I have read so many Posts and tech papers all of which are different which has totally confused me.

Can you help.

Regards

2 个答案:

答案 0 :(得分:1)

Hope this solve your issue,

Change you AndroidManifest.xml theme from,

 android:theme="@style/AppTheme.NoActionBar"

TO

android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"

答案 1 :(得分:1)

Activity

中执行此操作
    if (Build.VERSION.SDK_INT < 16) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else
    {
        View decorView = getWindow().getDecorView();
        //Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }

并在AndroidManifest

中执行以下操作
<activity            
        ......................................
        android:screenOrientation="portrait"        <!--if you want to lock orientation-->
        android:theme="@style/Theme.AppCompat.NoActionBar" 
        ..................................... >
</activity>
相关问题