27

I'm trying to figure out how is it possible to hide the action bar for a splash screen activity. I did something which hide my action bar on my splash screen, but there is one problem: before my splash screen appears, there is like an activity with an action bar who appears briefly... and I don't know how to hide it!

My splash screen only appears when we touch the application for the first time like an introduction of the application.

Code :

package com.example.basicmaponline;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class Intro extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();

    setContentView(R.layout.intro);

    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(3000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }finally{
                Intent openMenu = new Intent("com.example.basicmaponline.MENU");
                startActivity(openMenu);
            }
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.basicmaponline"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/lojacidadao"
    android:label="@string/app_name"
     android:theme="@style/AppTheme">
    <activity
        android:name="com.example.basicmaponline.Intro"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="com.example.basicmaponline.Menu"
        android:screenOrientation="portrait"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.MENU" />

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

    <activity
        android:name="com.example.basicmaponline.Mapa"
        android:screenOrientation="portrait"
        android:label="@string/map_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.MAPA" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.basicmaponline.Lojas"
        android:screenOrientation="portrait"
        android:label="@string/lojas_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.LOJAS" />

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

    <activity
        android:name="com.example.basicmaponline.InfoLoja"
        android:screenOrientation="portrait"
        android:label="@string/loja_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.INFOLOJA" />

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

    <activity
        android:name="com.example.basicmaponline.Balcoes"
        android:screenOrientation="portrait"
        android:label="@string/balcoes_name" >
        <intent-filter>
            <action android:name="com.example.basicmaponline.BALCOES" />

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

    <activity
        android:name="com.example.basicmaponline.Entidade"
        android:screenOrientation="portrait"
        android:label="@string/balcao_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.ENTIDADE" />

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

     <activity
        android:name="com.example.basicmaponline.Servicos"
        android:screenOrientation="portrait"
        android:label="@string/servicos_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.SERVICOS" />

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

     <activity
        android:name="com.example.basicmaponline.InfoServico"
        android:screenOrientation="portrait"
        android:label="@string/servico_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.INFOSERVICO" />

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

     <activity
        android:name="com.example.basicmaponline.Pesquisar"
        android:screenOrientation="portrait"
        android:label="@string/pesquisa_name" >
        <intent-filter>
            <action android:name="com.example.basicmaponline.PESQUISAR" />

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

</application>
</manifest>
Vivek Barai
  • 1,338
  • 13
  • 26
Damiii
  • 1,363
  • 4
  • 25
  • 46

5 Answers5

62

why not add to the manifest the theme of the first activity to exclude the action bar, and maybe even the notification bar?

something like:

<application
    android:allowBackup="true"
    android:icon="@drawable/lojacidadao"
    android:label="@string/app_name"
     android:theme="@style/AppTheme">
    <activity
        android:name="com.example.basicmaponline.Intro"
        android:screenOrientation="portrait"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

This should work if you extend the normal Activity class.

If your Activity extends AppCompatActivity instead, use this theme for example:

  <style name="AppTheme.Splash" parent="@style/Theme.AppCompat.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
  </style>

meaning :

 <activity ...            
    android:theme="@style/AppTheme.Splash" >
    ...

BTW, the reason you have the action bar is because you have set the default theme to have it, in the application tag, so it's not an activity before yours, it's really your own splash activity.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    It worked ! Thank you ! "android:theme="@android:style/Theme.NoTitleBar.Fullscreen" So the "android:Theme.Holo" from Android, will put the action bar for each activity. Ok ! :) – Damiii May 29 '13 at 21:07
  • yes, and if you wish to support adding action bar to old android versions (gingerbread and below), you can use the ActionBarSherlock library, but also use their styles and classes, of course. – android developer May 29 '13 at 22:40
  • I try this method but it change my theme with dark background while my theme is **android:theme="@style/Theme.AppCompat.Light.DarkActionBar"** how come it happen like this? – Jiazzy user May 06 '14 at 03:56
  • @Jiazzyuser You should also set the theme to the application tag itself. – android developer May 06 '14 at 08:01
  • but my theme in application tag is already set to Light background – Jiazzy user May 06 '14 at 16:25
  • @Jiazzyuser You are right. it does sound weird. If you post a new question, you can show your manifest and other relevant clues so that I could try to help you. put the link here if you wish. – android developer May 06 '14 at 19:42
  • getting java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity – Ananta Prasad Apr 14 '19 at 19:23
7

if your build target sdk 5.0 or over (AppTheme style is Theme.AppCompat.Light.DarkActionBar.)

<activity
    android:name=".Splash"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
Asil ARSLAN
  • 1,066
  • 2
  • 14
  • 31
2

You're problem are these lines

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();

They are not needed as there should be no ActionBar to call at all, the splash screen does not use one and should be a completely separate Activity than your others.

You need two Activities. One for the Splash Screen with it's own layout. The 2nd is for the main Activity, which can be a login screen or welcome screen etc.

The splash screen class should look like this

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.yourlayoutfile);

    Thread loading = new Thread() {
        public void run() {
            try {
                sleep(5000);
                Intent main = new Intent(SplashScreen.this, Main.class);
                startActivity(main);
            }

            catch (Exception e) {
                e.printStackTrace();
            }

            finally {
                finish();
            }
        }
    };

    loading.start();
}

}

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"    
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"    
tools:context=".SplashScreen" >

// if not using an image replace with whatever is to be shown on the splash screen
<ImageView  
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo" />

</LinearLayout>

Now the 2nd Activity has the layout with the ActionBar etc.

i_me_mine
  • 1,435
  • 3
  • 20
  • 42
1

Small And Sweet, eazy

Do on In OnCreate

View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
Vithani Ravi
  • 1,807
  • 3
  • 13
  • 19
  • 1
    Since it's your splash screen you would probably want to keep the action bar hidden even if someone touches the screen. However, with this approach, touching anywhere on the screen causes the bar to reappear and remain visible. – Tariq Jun 04 '20 at 18:39
0
//add to AndroidManifest for SplashScreen
<activity
         android:name="<YOUR_PACKAGENAME.ACTIVITY>"
         android:theme="@style/AppTheme.NoActionBar"
         .........../>
//add this styles to  styles.xml

 <style name="AppTheme.NoActionBar">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

//add this code to Activity
public class SplashScreen extends AppCompatActivity {
.
.
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    .
    .
    .
    }

}
sirmagid
  • 1,090
  • 1
  • 16
  • 23