How to Remove Title Bar From the Activity in Android?
Android Title Bar or ActionBar or Toolbar is the header of any screen in an App. We usually keep fixed title names for every Activity. Sometimes, it is necessary to remove the bar from the activity. There are several ways of doing this. One is if we want to remove the title in all the activities, then we need to go to res > values > themes > theme.xml. and change,
<style name=”Theme.GfgItemSelect” parent=”Theme.MaterialComponents.DayNight.DarkActionBar”>
to
<style name=”Theme.GfgItemSelect” parent=”Theme.MaterialComponents.DayNight.NoActionBar”>
So basically we just need to change DarkActionBar to NoActionBar. NoActionBar theme prevents the app from using the native ActionBar class to provide the app bar. Thus it removes the title of every activity.
Another way for removing the title specifically from an activity is by using a getSupportActionBar().hide() method. Inside the activity’s kotlin file, we need to invoke getSupportActionBar().hide() method.
Kotlin
package com.ayush.gfgitemselect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
getSupportActionBar()?.hide()
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
}
|
We need to call getSupportActionBar() using ?. (safe call) as it can be nullable. getSupportActionBar is an action bar utility method. This method returns a reference to an appcompat ActionBar object.
If we want only some activity to have a title then we can use getSupportActionBar()?.show() method. In this case, we can change DarkActionBar to NoActionBar as discussed above and inside specific activity’s kotlin file we can use getSupportActionBar()?.show().
Kotlin
package com.ayush.gfgitemselect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
getSupportActionBar()?.show()
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
}
|
Last Updated :
06 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...