Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to hide the action bar? #340

Open
programthis opened this issue Aug 12, 2024 · 13 comments
Open

How to hide the action bar? #340

programthis opened this issue Aug 12, 2024 · 13 comments

Comments

@programthis
Copy link

I am attempting to hide the action bar (title bar) in my Turbo Native application in Android Studio but the usual way doesn't seem to be working.

I am using the call inside my MainActivity.kt:

supportActionBar?.hide()

Full code:

package com.strategiccoach.winstreak

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dev.hotwire.turbo.activities.TurboActivity
import dev.hotwire.turbo.delegates.TurboActivityDelegate

class MainActivity : AppCompatActivity(), TurboActivity {
    override lateinit var delegate: TurboActivityDelegate

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
        setContentView(R.layout.activity_main)
        delegate = TurboActivityDelegate(this, R.id.main_nav_host)
    }
}

And I've also made sure the themes.xml has no action bar set like so:

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

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

But neither of these seem to work. Is something in Turbo setting the action bar?

@LeeGoTech
Copy link

Here's how to hide the action bar based on the documentation of Turbo Android.

In WebFragment.kt,

package YOUR_PACKAGE

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import dev.hotwire.turbo.fragments.TurboWebFragment
import dev.hotwire.turbo.nav.TurboNavGraphDestination

@TurboNavGraphDestination(uri = "turbo://fragment/web")
class WebFragment : TurboWebFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.web_fragment, container, false)
    }

    override fun shouldObserveTitleChanges(): Boolean {
        return false
    }
}

In res\layout\web_fragment.xml,

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        layout="@layout/turbo_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>

@programthis
Copy link
Author

@LeeGoTech Thanks for your reply. I'm a bit confused as to the package line. Should I be using line "package com.strategiccoach.winstreak" in the first file (which is in my MainActivity.kt)?

When I do so, I'm getting an error "Unresolved reference: WebFragment" when I try to build based on this code.

@LeeGoTech
Copy link

Please provide me a screenshot so I can assess the issue further.

@programthis
Copy link
Author

@LeeGoTech Sure, here is my code sample

package com.strategiccoach.winstreak

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import dev.hotwire.turbo.fragments.TurboWebFragment
import dev.hotwire.turbo.nav.TurboNavGraphDestination

@TurboNavGraphDestination(uri = "turbo://fragment/web")
class WebFragment : TurboWebFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.web_fragment, container, false)
    }

    override fun shouldObserveTitleChanges(): Boolean {
        return false
    }
}

And here's what the build error looks like
Screenshot 2024-08-20 at 10 26 11 AM

@programthis
Copy link
Author

One that shows the error clearer

Screenshot 2024-08-20 at 10 29 57 AM

@LeeGoTech
Copy link

I see the problem. There's no need to import the WebFragment class since it's already included in the registeredFragments method.

Let me know if there are still any problems related to hiding the action bar.

@programthis
Copy link
Author

@LeeGoTech So I did try removing this line as well, which does remove the action bar, but then I'm left with this giant space on my app so I put the line back in. Any ideas?

Screenshot 2024-08-20 at 10 53 16 AM Screenshot 2024-08-20 at 10 53 54 AM

@programthis
Copy link
Author

It's easy to trigger said space, just by clicking into any input box and then clicking out of it.

@LeeGoTech
Copy link

Refer to this issue: #215

@programthis
Copy link
Author

@LeeGoTech This method does work to remove the space but it then prevents the screen from shifting up when clicking into the input that's lower in the page. Here's a demo (note: I'm trying to click into the second input down the page which makes the typepad cover it up). Previously, it would shift the whole page up:

screen-20240821-100232.mp4

@LeeGoTech
Copy link

Use this in your web_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@android:color/transparent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </com.google.android.material.appbar.AppBarLayout>

    <include
        layout="@layout/turbo_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_bar" />

</androidx.constraintlayout.widget.ConstraintLayout>

@programthis
Copy link
Author

@LeeGoTech I tried this code block but no change unfortunately.

@LeeGoTech
Copy link

I forgot something. Change adjustPan to adjustResize in AndroidManifest.xml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants