PBM
PBM

Reputation: 153

Removing Toolbar from the app

I want to remove toolbar from the app in XML file, look red box in the image.

The code for that is:

<include  layout="@layout/toolbar"
            android:id="@+id/toolbar" />

I removed the above code, while running the app it get crash. Why so and how to remove that toolbar in my file?

Full code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:orientation="vertical"
    android:id="@+id/dlMain"
    android:visibility="visible"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    tools:context="com.namarsaham.society.jainvidhya.WebViewActivity">
    <LinearLayout
        android:id="@+id/dllayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include  layout="@layout/toolbar"
            android:id="@+id/toolbar" />

            <im.delight.android.webview.AdvancedWebView
                android:id="@+id/webview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="visible"/>


    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

My theme Code:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 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.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

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

</resources>

Upvotes: 1

Views: 2631

Answers (4)

Akshar Patel
Akshar Patel

Reputation: 9388

Put this inside your Activity/Fragment:

View toolbar = getActivity().findViewById(R.id.toolbar)

if(toolbar != null){
  toolbar.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
}

Upvotes: 0

Angel Koh
Angel Koh

Reputation: 13515

if you are using the auto generated Activity, it has a line of code in onCreate

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

if you have removed toolbar from the xml, then using it subsequently will be an issue and cause it to crash (toolbar will be null).

It might be because of this. you can try removing all codes related to toolbar in the activity file, or better yet, just hide/setInvisible the toolbar. It satisfy your requirements to not show the toolbar and you don't have to worry about it crashing.

Upvotes: 1

Janvi Vyas
Janvi Vyas

Reputation: 752

Try this to hide Title Bar in java file

requestWindowFeature(Window.FEATURE_NO_TITLE);

OR

you can set app theme like below :-

<application

    android:theme="@style/AppTheme" />

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

   <item name="windowNoTitle">true</item>
   <item name="android:windowNoTitle">true</item>
</style>

OR

You can also try below in the manifest.xml file:

For app:

<application android:theme="@android:style/Theme.Black.NoTitleBar">

For particular activity:

<activity android:theme="@android:style/Theme.Black.NoTitleBar">

Upvotes: 1

Gowthaman M
Gowthaman M

Reputation: 8272

simply add this line android:visibility="gone"

 <include  layout="@layout/toolbar"
              android:id="@+id/toolbar"
              android:visibility="gone"  />

Upvotes: -1

Related Questions