Android Activity without ActionBar - java

I have different Activities in my App and in all of them I do not want the Action Bar. I cannot find how to disable it. I have tried to find an attribute to apply it to the main_activity.xml but so far I did not find anything. Can somebody help me please?

Haha, I have been stuck at that point a while ago as well, so I am glad I can help you out with a solution, that worked for me at least :)
What you want to do is define a new style within values/styles.xml so it looks like this
<resources>
<style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name = "NoActionBar" parent = "#android:style/Theme.Holo.Light">
<item name = "android:windowActionBar">false</item>
<item name = "android:windowNoTitle">true</item>
</style>
</resources>
Only the NoActionBar style is intresting for you. At last you have to set is as your application's theme in the AndroidManifest.xml so it looks like this
<application
android:allowBackup = "true"
android:icon = "#drawable/ic_launcher"
android:label = "#string/app_name"
android:theme = "#style/NoActionBar" <!--This is the important line-->
>
<activity
[...]

There are multiple ways of doing this.
1) Change the theme in Android Manifest
Below the Application element, you will find theme tag. Replace it with this one -
android:theme="#android:style/Theme.NoTitleBar"
If you are using AppCompat, use #android:style/Theme.AppCompat.NoActionBar.
This will set the theme for all activities. If you don't need the action bar in a specific acitivity, then set the theme in the activity container, ie
<activity android:theme="#android:style/Theme.NoTitleBar" ...>
You may also set android:theme="#android:style/Theme.NoTitleBar" theme in the styles and use it later on.
2) Create a Custom Theme
Add this in res/values/styles.xml
<style name="NoActionBar" parent="#android:style/Theme.Holo.Light">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
and then set it as your activity's theme in Manifest:
<activity android:theme="#style/NoActionBar" ... />
3) Dynamically Remove the Action Bar
Put this code in the onCreate method before setting content view -
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
If you are using the support library use getSupportActionBar().

If you're using AppCompat, declare your activity in AndroidManifest.xml as follows:
<activity
android:name=".SomeActivity"
...
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />

Considering everyone is posting older ways of hiding the ActionBar here is the proper way of implementing it within styles for AppCompat support library. Which I highly suggest moving toward if you haven't already.
Simply removing the ActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
If you are using the appcompat support libraries, this is the easiest and recommended way of hiding the ActionBar to make full screen or start implementing to toolbar within your layouts.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Your Toolbar Color-->
<item name="colorPrimary">#color/primary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
Then to change your toolbar color
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/primary” />
Last note: Your Activity class should extend AppCompatActivity
public class MainActivity extends AppCompatActivity {
<!--Activity Items -->
}

Default style you getting in res/value/style.xml like
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And just the below like in your activity tag,
android:theme="#style/AppTheme.NoActionBar"

If you want most of your activities to have an action bar you would probably inherit your base theme from the default one (this is automatically generated by Android Studio per default):
<!-- 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>
And then add a special theme for your bar-less activity:
<style name="AppTheme.NoTitle" parent="AppTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="actionBarTheme">#null</item>
</style>
For me, setting actionBarTheme to #null was the solution.
Finally setup the activity in your manifest file:
<activity ... android:theme="#style/AppTheme.NoTitle" ... >

You may also change inheritance from ActionBarActivity to Activity as written here.
UPDATE
A good solution is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
}

open Manifest and add attribute theme = "#style/Theme.AppCompat.Light.NoActionBar" to activity you want it without actionbar :
<application
...
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
...
</activity>
</application>

I will try to show it as simple as i can:
DECLARE FULL SCREEN ACTIVITY IN ANDROID MAINIFEST file:
<activity
android:name=".GameActivity"
android:label="#string/title_activity_game"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
Now in Android studio (if you using it) open your Activity xml file ( in my example activity_game.xml) in designer and select theme (above mobile phone in designer click button with small circle) and then select Mainfest Themes on the left side and then NoTitleBar.Fullscreen.
Hope it will help!

Please find the default theme in styles.xml
<!-- 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>
And change parent this way
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

android:theme="#style/Theme.MaterialComponents.Light.NoActionBar"
Using this theme worked for me

Put this line in your Activity in the Manifest:
<activity android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
...
</activity>
and make sure you didn't put Toolbar in your layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>

Create an new Style with any name, in my case "Theme.Alert.NoActionBar"
<style name="Theme.Alert.NoActionBar" parent="Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
set its parent as "Theme.AppCompat.Dialog" like in the above code
open AndoridManifest.xml and customize the activity you want to show as Alert Dialog like this
<activity
android:excludeFromRecents="true"
android:theme="#style/Theme.Alert.NoActionBar"
android:name=".activities.UserLoginActivity" />
in my app i want show my user login activity as Alert Dialog, these are the codes i used. hope this works for you!!!

It's Really Simple Just go to your styles.xml change the parent Theme to either
Theme.AppCompat.Light.NoActionBar or Theme.AppCompat.NoActionbar and you are done.. :)

Related

Change launch activity style programmatically

I have an activity (MainActivity.java) that is used as the launch screen while the main fragment is being loaded and other functionality takes place in background. This launch screen shows a brown tile background and an icon always. What I want is to show that backgound (in R.style.AppTheme_NoActionBar_LauncherNight) only when the variable dayMode is false (variable in Constants.java). Otherwise, the background should be the one in R.style.AppTheme_NoActionBar_LauncherDay (a white background and the same icon).
If I specify one or another background in the android:theme part of my Manifest, it is shown nicely. But what I want is to set one theme or another programmatically, depending on the value of dayMode, on the onCreate method of the activity. This is what is not working.
I tried using setTheme before calling super.onCreate or setContentView, as I read in other answers, but it is not working. I only find answers explaining the order in which you should call setTheme and setContentView, but they do not solve this problem.
My styles:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="autoCompleteTextViewStyle">#style/cursorColor</item>
<item name="android:textColorSecondary">#color/yellow_light</item>
</style>
<style name="AppTheme.NoActionBar.LauncherNight">
<item name="android:windowBackground">#drawable/launch_screen</item>
</style>
<style name="AppTheme.NoActionBar.LauncherDay">
<item name="android:windowBackground">#drawable/launch_screen_day</item>
</style>
My Manifest:
<activity
android:name="com.AlbaRam.myApp.MainActivity"
android:configChanges="keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar.LauncherNight"
android:launchMode="singleInstance"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My ActivityMain:
#Override
protected void
onCreate(Bundle savedInstanceState) {
//This is not working
if (Constants.dayMode){
super.setTheme(R.style.AppTheme_NoActionBar_LauncherDay);
} else {
setTheme(R.style.AppTheme_NoActionBar_LauncherNight);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//rest of functionality
}
After some research I've discovered that this is not possible. Launch screens are used when we want to show something while our main functionality is loading, so we include the drawable we want to show in the Manifest to have this appearing fast while our main activity loads. This launch screen, as it is in the Manifest, appears before anything else, so if we could change the theme of the launch screen dinamically, we would lose that fast appearing while everything else loads.
Try this code
theme.xml
<resources>
<style name="AppThemeLight" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<style name="AppThemeDark" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<!-- This will set the fade in animation on all your activities by default -->
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
activity
#Override
protected void onCreate(Bundle savedInstanceState) {
AppSettings settings = AppSettings.getInstance(this);
setTheme(settings.getBoolean(AppSettings.Key.USE_DARK_THEME) ? R.style.AppThemeDark : R.style.AppThemeLight);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_theme);
//
}
public void onCreate(Bundle savedInstanceState) {
if (Constants.dayMode){
setTheme(android.R.style.yourTheme);
} else {
setTheme(android.R.style.yourTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.actvity);
}

Removing Title Bar on Android App

So I know there are a ton of these questions, but every time I try what they say, my app does not seem to remove the title bar. I have done the style change:
<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>
<item name="android:windowNoTitle">true</item> <!-- This should work but it does not -->
</style>
I have also tried:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); //This guy here
setContentView(R.layout.activity_title);
The above works but I still want to use the space the title bar takes for content in my app, which is why I want to be able to modify the XML code rather than use java. Any suggestions?
You can either:
<style name="noActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and in your layout(at the root layout)
android:theme="#theme/noActionBar"
Or
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
However, the last one also hides the Statusbar. You can also set the theme to a default theme(look through the themes and find those with NoActionBar). The last one is useful if you have no plans to style the activity(for an instance if you use SurfaceView) with custom colors, etc.
For an instance you can change AppBaseTheme to have the parent Holo.Light.NoActionBar or any similar theme, but the name has to contain NoActionBar, othervise it has an actionbar
Add the below theme to style.xml:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Then set this theme to your Activity in manifest:
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar" />

Hide or remove Action Bar on specific Activity | Android

How I can remove Action Bar on specific activity not for all application, for example I have Sign Up activity and for this activity I want to remove Action Bar
In your AndroidManifest.xml use NoActionBar theme for your Activity like this:
<activity android:name=".SignUpActivity"
android:theme="#style/AppTheme.NoActionBar"/>
and in your styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
from within your activity:
getActionBar().hide();
or
getSupportActionBar().hide();
I would suggest to migrate to ToolBar, new Android API for the same purpose. The main benefit of ToolBar is that you can treat it like other views, it guarantees to you a more flexible approach.
<!-- 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>
If you have an ActionBar, you can use getActionBar().hide();
If you have a SupportActionBar, you can use getSupportActionBar().hide();
Remove Action Bar on specific Activity go to Activity java file here your Activity class extends with AppCompatActivity remove the AppCompatActivity and extends with Activity and go to xml file and select Theme NoTitleBar or go to Manifests file select your Activity and add Theme on your Activity android:theme="#android:style/Theme.NoTitleBar"/>
For kotlin you can use supportActionBar?.hide()

FullScreen Layout doesn't work on Android

I have an activity_login.xml with a FrameLayout in Android Studio, which doesn't appear in fullscreen mode with this configuration:
AndroidManifest.xml:
<activity
android:name=".Activities.LoginActivity"
android:label="#string/title_activity_login"
android:launchMode="singleTop"
android:theme="#style/LoginScreenTheme.TranslucentBar" >
</activity>
Styles.xml:
<style name="LoginScreenTheme.TranslucentBar" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
The Toolbar is still appearing in the Layout.
Look at your activity Layout, most likely there is Toolbar view that AndroidStudio generated for you. If that does not work add to the style:
<item name="android:windowFullscreen">true</item>
Modify onCreateView() as below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
In order to remove the Toolbar/ActionBar you don't need to do anything special at all. Just keep everything as default and go to the styles.xml and change the AppTheme to Theme.AppCompat.Light.NoActionBar and also make sure that your activity extends AppCompactActivity. It always works for me. Hope it will help you as well. And if you also wants to remove the StatusBar then follow #EKN solution above ;)
I don't do anything and today when I started my project, the activity was in fullscreen.

How to change all the activity transitions at once in Android application?

I know I can change activity transition using the following code right after startActivity() or finish()
activity.overridePendingTransition(R.anim.activity_close_enter, R.anim.activity_close_exit);
But if I have ten activities in my app, I have to do that ten times; and it is quite hard to modify. So I'm wondering if there is a way to set transition for all activities within the application at once. Is there any corresponding configuration in AndroidManifest.xml?
Thanks!
You want to first create a <style> in res/styles.xml, like this:
<style name="YourAnimation.Activity" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/your_in_down</item>
<item name="android:windowExitAnimation">#anim/your_out_down</item>
</style>
Then you can apply the style to a theme, in the same file:
<style name="YourTheme" parent="android:Theme.Translucent">
...
<item name="android:windowAnimationStyle">#style/YourAnimation.Activity</item>
</style>
And finally apply the theme to your activities in the manifest:
<activity
android:name=".YourActivity"
android:theme="#style/YourTheme" />
Look at these links for reference:
Android Reference - Apply A Theme
Android Reference - WindowEnterAnimation
I know this has been answered but here is what I did in mine. We still support API 14 so there are some animations missing that I had to pull into the project from API 22( slide_in_right, slide_out_left). What this does is to slide in the screens when you open a new activity and slides the closing one out to the left. When you press back it will then do the opposite, sliding from the left the previous screen and closing out to the right the current screen.
<style name="YourTheme" parent="android:Theme.Translucent">
...
<item name="android:windowAnimationStyle">#style/YourAnimation.Activity</item>
</style>
<style name="YourAnimation.Activity" parent="#android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">#anim/slide_in_right</item>
<item name="android:activityOpenExitAnimation">#anim/slide_out_left</item>
<item name="android:activityCloseEnterAnimation">#android:anim/slide_in_left</item>
<item name="android:activityCloseExitAnimation">#android:anim/slide_out_right</item>
</style>
Step 1: Create one base activity
Step 2: Extend all your activity to this base activity
Step 3: In your base activity add following code
#Override
protected void onStart() {
super.onStart();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
#Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
}
}
My solution is mostly like of others...
<style name="YourAnimation.Activity" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/slidefromright</item>
<item name="android:windowExitAnimation">#anim/slidetoright</item>
</style>
<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>
<item name="android:windowAnimationStyle">#style/YourAnimation.Activity </item>
</style>
</resources>

Categories

Resources