0

I've created a new blank project in Android Studio. Once loaded I right clicked on the com.myapp.appname folder and selected Activity > Blank Activity. But, the activity still seems to have the title bar, as can be seen in the image below.

enter image description here

I attempted to add android:theme="@android:style/Theme.NoTitleBar" to the manifest but it doesn't seem to work. The error I am getting is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.newapp.jamesjeffery/com.newapp.jamesjeffery.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

My manifest is:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light">
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Main Activity:

package com.newapp.jamesjeffery;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

content_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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.newapp.jamesjeffery.MainActivity"
    tools:showIn="@layout/activity_main">

</RelativeLayout>

I've tried to follow this: You need to use a Theme.AppCompat theme (or descendant) with this activity ... but to no avail.

I am trying to create a blank activity with no title bar.

Any ideas where I am going wrong?

Community
  • 1
  • 1
BugHunterUK
  • 8,346
  • 16
  • 65
  • 121

4 Answers4

1

Try extending your activity style from Theme.AppCompat.NoActionBar. e.g

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Bra Saeed
  • 51
  • 1
  • 1
0

Try this :

Add this style to your styles.xml if it doesn't already exists :

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

and in your v21/styles.xml :

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

and set the style this way

android:theme="@android:style/AppTheme.NoActionBar"
Boukharist
  • 1,219
  • 11
  • 19
0
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

Remove Toolbar from your activity_main.xml

user3623735
  • 345
  • 2
  • 9
0

Your problem is

You need to use a Theme.AppCompat theme (or descendant) with this activity.

To solve this issue you must to create a custom descendant theme of Theme.AppCompat like this

<style name="Theme.AppCompat.NoActionBar"  parent="@style/Theme.AppCompat">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>      
</style>

And change in your AndroidManifest.xml the MainActivity theme:

<activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main"
    android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Hope this helps!!

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57