Can't hide ActionBar Android Studio Giraffe properly

225 views Asked by At

I'm using Android Studio Giraffe | 2022.3.1 and I can't properly hide the ActionBar.

I saw both this and this related questions but they didn't help me.

Here is my MainActivity.kt file:

package com.example.motivation

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.motivation.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)

        // Hide ActionBar
        supportActionBar?.hide()

        // Events
        binding.buttonNewPhrase.setOnClickListener(this)

    }

    override fun onClick(view: View) {
        if (view.id == R.id.button_new_phrase){
        }
    }
}

Also in the themes.xml there is no ActionBar by default, haven't change it

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.Motivation" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.Motivation" parent="Base.Theme.Motivation" />
</resources>

And here is the AndroidManifest.xml:

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Motivation"
        tools:targetApi="31">
        <activity
            android:name=".UserActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>
        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

When I execute the app, the ActionBar is still there:

app

Why I can't hide it at all?

2

There are 2 answers

0
MEGHA RAMOLIYA On

1.First way to hide ActionBar :

         1.1 Hide Action Bar permanently from theme.xml file
        
        <resources >
            <!-- Base application theme. -->
        
            <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
                <!-- Customize your theme here. -->
                <item name="colorPrimary">@color/colorPrimary</item>
                <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                <item name="colorAccent">@color/colorAccent</item>
                <item name="android:windowBackground">@android:color/transparent</item>
            </style>
        
         1.2 Add theme in AndroidManifest.xml file
        
        <application
                android:allowBackup="true"
                android:dataExtractionRules="@xml/data_extraction_rules"
                android:fullBackupContent="@xml/backup_rules"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/AppTheme"
                tools:targetApi="31">
        ......
        </application>
        
  1. Second way to Hide Action Bar for a particular activity:

      <activity android:name=".activity.HomeScreen"
      android:theme="@style/Theme.AppCompat.Light.NoActionBar"
      android:screenOrientation="portrait" />
    

    3.Third way is that Add this code in kotlin file

      supportActionBar?.hide()
    
    1. Hide Action Bar using Window Manager
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
0
shembo On

Replace this line in themes.xml

<style name="Base.Theme.Motivation" parent="Theme.Material3.DayNight.NoActionBar">

with the line

<style name="Base.Theme.Motivation" parent="Theme.MaterialComponents.DayNight.DarkActionBar" >

after this it will be possible to hide the ActionBar using supportActionBar?.hide()