How to remove the Action Bar from a Activity - android

I am trying to remove the action bar from an activity.
I have done following but its still keeping the Action bar on my Activity
public class MyActivity extends AppCompatActivity{ .. }
Layout
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context=".TM.TMActivity"
style="#style/AppTheme.NoActionBar"
>
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/AppTheme.NoActionBar"
/>
</android.support.design.widget.CoordinatorLayout>
Style.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Use Theme.AppCompat.Light.NoActionBar its working for me
e.g
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>

In styles.xml in values folder,
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

ActionBar actionBar = getSupportActionBar();
actionBar.hide();
In your activity You can disable.

Check if the correct style has mentioned in the manifest file.
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme">
And in your style file:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Check your build.gradle(app) also for dependency.
compile 'com.android.support:appcompat-v7:x.x.x'
There is no need to mention style in Coordinator/Frame layout. Remove the following line from all the layout and try.
style="#style/AppTheme.NoActionBar"

Related

How do I change the text in the toolbar in each activity?

There are questions answered on this, however, they don't seem to work with the new AndroidX
My Main Activity is saying "Home" in the toolbar, when I want it to say something else. I'm only using the default navigation drawer activity from Android Studio
Here's my Main Activity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Let's say I want to set the text of the toolbar as something, how do I do it?
Here's the styles.xml too for reference
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- 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.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Depending on your requirements, you could statically change it by using:
getSupportActionBar().setTitle("Some Title Here");
There are couple of ways to do that, as i am using Material Design Dependency i change my AppTheme from resources to this
<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>
</style>
</resources>
Specifically this is the tag i am talking about
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar>
EDIT: In case if you want Material Design Dependency , here it is:
// New Material Design
implementation 'com.google.android.material:material:1.0.0'
Then go to your particular activity's XML and add custom toolbar, like so:
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"/>
</LinearLayout>
Notice, here i have used Linear Layout just not to mess with constraints..
Then into your activity, do this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)// this is your toolbar ID
setTitle("Hello World") // your toolbar title
}
ThankYou!
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"android.support.v7.widget.Toolbar
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="?attr/colorPrimary"
android:text="<your text>">
</androidx.appcompat.widget.Toolbar>

How to delete the default bar in android studio?

I am currently developing my first app in android studio and i am facing a problem that i tried to fix for a while now.
I am trying to remove the first action bar.
I tried changing the theme to a .noActionBar one but to result
Please check out the image for a visual explanation
this is my 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"
tools:context="com.example.user.survay_1.SecondActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/toolbar2"
android:background="#00a4f9"
android:visibility="visible">
</android.support.v7.widget.RecyclerView>
change your theme NoActionBar at styles.xml
<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>
also add
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
write below code in Style.xml:
<style name="LoginTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
Now u call this theme in manifest.xml with adding this line android:theme="#style/LoginTheme"
See manifest.xml look like :
<activity
android:name=".MainActivity"
android:theme="#style/LoginTheme"
android:windowSoftInputMode="stateHidden" />
getSupportActionBar().hide();
In your Activity just use this :- only one line
You can do it in two ways.
First way is to disable it in styles.xml file:
styles.xml
<style name="CustomTheme.NoActionBar" parent="CustomTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And in your AndroidManifest.xml you can use it for all activities:
<application android:theme="#style/CustomTheme.NoActionBar">
Or for one activity only:
<activity android:theme="#style/CustomTheme.NoActionBar">
The second way is to hide action bar from your activity:
Activity.class
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
declare custom theme in your style.xml file like this..
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
</style>
then in your manifest give your activity theme like this..
<activity android:name=".ActivityName"
android:theme="#style/AppThemeNoActionBar">
</activity>

Android - ViewPager fitsSystemWindows (fullscreen) not working

I have a ViewPager in my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent" />
</RelativeLayout>
And I want all Fragments to be in fullscreen mode (the screen should be also under status bar). I also set in that Activity:
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
);
and tried several thinks but nothing work. In a different Activity this code works OK.
Do you know where the problem is?
Look at the screenshot. It's still below the Status bar:
put this in style.xml
<style name="Login" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/white</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="colorControlNormal">#color/white</item>
</style>
and this in manifest
<activity
android:name=".ui.activities.LoginActivity"
android:theme="#style/Login"/>
For Fullscreen of Activity use fullscreen theme from manifest for that particular activity
<activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
.../>
or you can also apply theme programmatically using Activity.setTheme()
If you want to include your app content as a background of statusbar, it is not possible, yes in newer versions you can customize the color of statusbar background.
Remove android:fitsSystemWindows="true". That flag is used when you want to keep some components from displaying over the status and navigation bars.

Hide ActionBar title just for one activity

If I apply theme for whole application it hides ActionBar title successfully:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>
assign in manifest:
<application
android:name="myapp"
android:allowBackup="true"
android:icon="#drawable/action_bar_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
I need ActionBar title hidden just in one activity.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
...other things...
</style>
<style name="MainActivityTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>
setting this theme explicitly for one activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/mainLayout"
android:orientation="vertical"
android:theme="#style/MainActivityTheme">
and it still shows title in MainActivity ActionBar. I know how to hide the title in java, I need to know what I do wrong here in XML.
Set the theme on your activity in the manifest.
<activity
android:name=".MyActivity"
android:theme="#style/MainActivityTheme" />
You probably want to create a sub-theme which hides the action bar:
<style name="AppTheme.NoActionBar">
<item name="android:windowActionBar">false</item>
</style>
and then apply it to your activity in the manifest like so:
<activity android:name="NonActionBarActivity"
android:theme="#style/AppTheme.NoActionBar" />
Using the dot notation (AppTheme.NoActionBar) makes this NoActionBar theme inherit everything from AppTheme and override the setting on android:windowActionBar. If you prefer you can explicitly note the parent:
<style name="AppThemeWithoutActionBar" parent="AppTheme">
<item name="android:windowActionBar">false</item>
</style>
Also, if you're using the AppCompat libraries, you probably want to add a namespace-less version to the sub-theme.
<item name="windowActionBar">false</item>
If you want to remove actionbar title then just remove label attribute from <activity> tag in your manifest file.
Such as,
<activity
android:name=".YourActivityName"
android:label="#string/title" />
And after removing,
<activity
android:name=".YourActivityName" />
Done!

Removing bottom shadow on ActionBar - Android

I want to disable ActionBar shadow but only in one Activity. If I use this code it will be change in whole aplication.
<style name="MyAppTheme" parent="android:Theme.Holo.Light">
<item name="android:windowContentOverlay">#null</item>
</style>
I tried this code, but it is not working
getSupportActionBar().setElevation(0);
Any suggestion...?
You can set your own style for Activity for this:
<!-- Your main theme with ActionBar shadow. -->
<style name="MyAppTheme" parent="android:Theme.Holo.Light">
....
</style>
<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="MyNoActionBarShadowTheme" parent="MyAppTheme">
<item name="windowContentOverlay">#null</item>
<item name="android:windowContentOverlay">#null</item>
</style>
So in Manifest.xml you can set different style for all Activities:
<!-- Activity with ActionBar shadow -->
<activity
android:name=".ShadowActivity"
android:theme="#style/MyAppTheme"/>
<!-- Activity without ActionBar shadow -->
<activity
android:name=".NoShadowActivity"
android:theme="#style/MyNoActionBarShadowTheme"/>
Or you can set the right theme programmatically in onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MyNoActionBarShadowTheme);
super.onCreate(savedInstanceState);
//...
}
Add app:elevation="0dp" to appbarlayout as shown below:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/home_tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
Create a new Theme that inherits your App theme:
<style name="MyAppTheme.NoShadow" parent="MyAppTheme">
<item name="android:windowContentOverlay">#null</item>
</style>
and let your activity use this theme. In your Manifest:
<activity
android:name="...MyShadowlessActivity"
android:theme="#style/MyAppTheme.NoShadow" .../>

Categories

Resources