I'm trying to display a custom icon in the ActionBar, but it doesn't work.
I've tried it both programmatically with
getSupportActionBar().setIcon(R.drawable.forum_logo);
and
getSupportActionBar().setLogo(R.drawable.forum_logo);
and in AndroidManifest.xml with
android:logo="#drawable/forum_logo"
However, none of the two works. forum_logo.png is an image file of 24x24.
Other methods such as
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle((currentSubforum.getTitle()));
works as intended.
I'm using android.support.v7.app.ActionBarActivity and each Activity extends ActionBarActivity.
The android:icon attribute in AndroidManifest is already linked at a different drawable and the android:theme is "Theme.AppCompat.Light.DarkActionBar"
The answer with the Toolbar was correct, however was missing one crucial detail.
If you use a Toolbar, your theme must have the ActionBar disabled. There's a specific theme for that:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
Now in your onCreate set the Toolbar like this:
Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);
Note that you should use the support Toolbar: import android.support.v7.widget.Toolbar;
The Toolbar in your layout would look like this:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="#40ffffff"/>
That being said, I found it really weird not being able to display the app icon with ActionBarActivity. Hopefully there's another way around this.
After Fragment.onActivityCreated(...) you'll have a valid activity accessible through getActivity(). You'll need to cast it to an ActionBarActivity then make the call to getSupportActionBar().
((ActionBarActivity)getActivity()).getSupportActionBar().setSubtitle(R.string.subtitle);
You do need the cast. It's not poor design it's backwards compatibility.
in the manifest use your costume theme.
<application
android:name="com.example.ifis.MyApplication"
android:allowBackup="true"
android:icon="#drawable/ifis_logo"
android:label="#string/app_name"
android:theme="#style/MyTheme" >
customize your theme in the style folder as below and add your icon to the icon
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/ifisGreen</item>
<item name="android:icon">#drawable/your_logo</item>
<item name="android:textColor">#color/ifisGreen</item>
<item name="android:height">30dp</item>
</style>
ActionBar actionBar = getActionBar();
actionBar.setIcon(R.drawable.ic_action_video);
actionBar.setDisplayShowTitleEnabled(true);
Only use 3 things..
Related
I implemented a theme for my ActionBar:
<style name="MyTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#color/actionbar_background</item>
<item name="android:displayOptions">useLogo|showHome</item>
<item name="android:logo">#mipmap/ic_launcher</item>
<item name="logo">#mipmap/ic_launcher</item>
<item name="background">#color/actionbar_background</item>
</style>
And I applied it in AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme">
...
I know that my theme works as I see a color change, however my actionbar icon never shows up. I see it only when I explicitly call this code in onCreate method of my activity:
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.mipmap.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
I feel like it's not correct to copy-paste the same code for every activity. Please help me to solve this problem.
The new compat library doesn't use logos nor app icons in the Toolbar (former ActionBar). You should check the style guides about that: https://www.google.com/design/spec/layout/structure.html#structure-toolbars
Starting with lollipop you get new features like tinting the notification bar on this screen:
A cool new feature is that tinting which can color all ui elements like you need it for your branding.
It is better to use android.support.v7.widget.Toolbar , you can setup your app's action bar as follows:
Toolbar toolbar = (Toolbar) findViewById(R.id.apptoolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.your_app_logo);
for more info you can read at Structure Of App Bar(which was called as ActionBar)
I am using the android support library ToolBar and would like to use the same background color to match the ActionBar.
I looked in the support library souce code and found the ActionBar style I want to use:
<style name="Base.Widget.AppCompat.Light.ActionBar.Solid">
<item name="background">?attr/colorPrimary</item>
<item name="backgroundStacked">?attr/colorPrimary</item>
<item name="backgroundSplit">?attr/colorPrimary</item>
</style>
But when I put it in my layout as a style to the toolbar, it doesn't work:
<android.support.v7.widget.Toolbar
android:id="#+id/actiontoolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/Base.Widget.AppCompat.Light.ActionBar.Solid"
/>
However, if I just use "android:background" instead of "style" then it works:
android:background="?attr/colorPrimary"
Does anyone know why it doesn't work when I reference the support library style in the toolbar layout. I am using appcompat 22.2.0 in AndroidStudio.
I've also tried the following and they don't work either:
android:theme="#style/Base.Widget.AppCompat.Light.ActionBar.Solid"
app:theme="#style/Base.Widget.AppCompat.Light.ActionBar.Solid"
It is because the toolbar requires a theme. Firstly define your color attributes in a theme with appcompat parent.
<style name="MyTheme" parent="Theme.AppCompat">
<item name="background">#color/colorPrimary</item>
<item name="backgroundStacked">#color/colorPrimary</item>
<item name="backgroundSplit">#color/colorPrimary</item>
</style>
Just to make sure, make use of color values.
In toolbar
app:theme="#style/MyTheme"
Also, make sure you have
xmlns:app="http://schemas.android.com/apk/res-auto"
IDE may ask you to auto correct this
The final look of your toolbar will be
<android.support.v7.widget.Toolbar
android:id="#+id/actiontoolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:theme="#style/MyTheme"
/>
And as for an update from Gabriele: With appcompat 22.1.0 you can use
android:theme
I upgraded to avtionbar app compat v21 for material design purpose.
But I observed that its not showing the app icon in actionbar and navigation menu and back button is taking more space compare to old appcompat libs.
Have any one faced this issue? I searched a lot but did not found anything useful.
Below line is also not working.
getSupportActionBar().setLogo(R.drawable.ic_launcher);
I am using style for actionbar which is like
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/ic_action_bar</item>
<item name="android:background">#drawable/action_bar_bg</item>
<item name="android:windowContentOverlay">#null</item>
<!-- Support library compatibility -->
<item name="background">#drawable/action_bar_bg</item>
</style>
I am aware of other solution like Toolbar. But I am in middle of release and due to time constraint I am looking for some quick solution if available. Adding a Toolbar will require more time.
setIcon/setLogo method will only work if you have set DisplayOptions Try this -
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);
You can also set options for displaying LOGO(just add constant ActionBar.DISPLAY_USE_LOGO). More information - displayOptions
I added this to my theme:
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="windowActionBar">true</item>
and then the ActionBar was back.
Note: if you use supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); then you can't just do that, but will have to add the Toolbar to your layout (which isnt that much work, anyway)
Use getSupportActionbar instead of actionbar
Actionbar actionbar = getSupportActionBar()
actionbar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionbar.setIcon(YOUR ICON);
In my app there is this title bar at the top where the overflow menu would be, but I don't need settings and only have one screen.
When I change the theme like described in many other questions I get the old 2.2 theme. I want to have the modern theme just without the bar at the top.
Go to styles.xml and change .DarkActionBar for .NoActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
becomes
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
if colors are irrelevant to your app, you can actually go for
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" />
In the manifest file Change:
android:theme="#style/AppTheme" >
android:theme="#style/Theme.AppCompat.NoActionBar"
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
works in onCreate() when put before setContentView() is invoked.
otherwise it crashes
Check this
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
In styles.xml file, change DarkActionBar to NoActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
Go to Project -> app -> main -> res -> values -> styles.xml
Change this line if you want to delete it for every view
`<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">`
to
`<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">`
if you want to make it just for one view you can change it in youre manifest data. Go to Android -> manifests -> AndroidManifest.xml. do following:
Search the View you want to get this type of change <activity android:name"...">
Add android:theme=""#style/Theme.AppCompat.NoActionBar"
This works for me
getSupportActionBar().hide();
This was working for me on values/styles.xml add items:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
In the manifest file change to this:
android:theme="#style/Theme.AppCompat.Light.NoActionBar" >
open styles.xml
insert
<style name="no_title" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
you can change name="---------"
open Androidmaifest.xm
find android:theme="#style/AppTheme" chang to android:theme="#style/no_title"
click select Theme on menubar (it's green color near MainActivity)
click project Theme
click no_title (on you right hand Side)
click OK
For Beginner Like Me. Just Do it what I say.From your Android Project.
app -> res -> values -> style.xml
replace this code
<resources>
<!-- 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>
</resources>
Enjoy.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
Just simply put getSupportActionBar().hide();
between super.onCreate and setContentView method.
do this in you manifest file:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
delete the following from activity_main.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
contact me for more help
Newer versions replaced styles.xml with themes.xml
Go to Project -> app -> res -> values -> themes -> themes.xml
and change e.g.
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- omitted for brevity -->
</style>
to
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- omitted for brevity. -->
</style>
Switching the parent to NoActionBar
Do this for both themes.xml files
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
in onCreate() works!
Try changing styles to NoActionBar if that doesn't works add this code to your main activity
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_main);
}
This works for me i hope this work for you as well
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
Best way is to use actionbar function setTitle() if you wish to show logo or have some other stuff in you actionBar but dont want to see name of the app, write this code in MainActivity.java or anywhere you wish to hide title in onCreate:
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.full_white_logo_m); // display logo
actionBar.setTitle(""); // hide title
This way your app won't have reason to crash.
Just use setTitle(null) above
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The title will disappear then you can use the logo of your choice.....
I have faced the same problem as you, and solve this problem just by changing the class which I extended.
//you will get the app that does not have a title bar at the top.
import android.app.Activity;
public class MainActivity extends Activity
{}
//you will get the app that has title bar at top
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{}
I hope this will solve your problem.
In Kotlin, this is how to do.
val actionBar: ActionBar? = supportActionBar
actionBar?.hide()
The easiest way is to just double click on this button and choose "NoTitleBar" .
In AndroidManifest.xml of your application you'll find the android:theme for example set to #style/AppTheme
Now go to styles.xml, find the style tag, make sure that name is set to AppTheme the same as in manifest and set parent to android:Theme.Holo.Light.NoActionBar
(also do this in styles(v21) if you have it in your project)
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
<!-- ... -->
</style>
Just call setTitle(null); in onCreate()
try:
toolbar.setTitle(" ");
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
}
To simply remove the title bar (which means the bar contains your app name) from an activity, I simply add below line to that activity in the manifests\AndroidManifest.xml:
android:theme="#style/AppTheme.NoActionBar"
It should now look like below
<activity
android:name=".MyActivity"
android:theme="#style/AppTheme.NoActionBar"></activity>
Hope it'll be useful.
At Project tab, find
app>res >values > themes >theme.xml
Change the value of parent in the above shown image.
New versions of Android Studio have themes.xml instead of styles.xml. So go to res/values/themems/themes.xml and change .DarkActionBar to .NoActionBar
This also solved my problem, but before it did I couldn't find the styles.xml resource.
Eventually I found a solution and it turns out that the theme attribute is now inside the themes.xml not the styles.xml Here is my example
I'm looking through the Holo.Light theme, and I can't seem to find the magic style to override to get rid of the title text that briefly shows up when my app first launches.
How can I do that?
Try:
getActionBar().setDisplayShowTitleEnabled(false);
For v.7:
getSupportActionBar().setDisplayShowTitleEnabled(false);
I think this is the right answer:
<style name="AppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="android:displayOptions">showHome|useLogo</item>
<item name="displayOptions">showHome|useLogo</item>
</style>
I'm very new to Android so correct me if I'm wrong, but I think if you decide to use navigation tabs on the Action Bar, they seemed to not be completely left aligned because the title text color is only transparent and hasn't gone away.
<style name="CustomActionBarStyle" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">useLogo|showHome</item>
</style>
worked for me.
Got it. You have to override
android:actionBarStyle
and then in your custom style you have to override
android:titleTextStyle
Here's a sample.
In my themes.xml:
<style name="CustomActionBar" parent="android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/CustomActionBarStyle</item>
</style>
And in my styles.xml:
<style name="CustomActionBarStyle" parent="android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/NoTitleText</item>
<item name="android:subtitleTextStyle">#style/NoTitleText</item>
</style>
<style name="NoTitleText">
<item name="android:textSize">0sp</item>
<item name="android:textColor">#00000000</item>
</style>
I'm not sure why setting the textSize to zero didn't do the trick (it shrunk the text, but didn't make it go away), but setting the textColor to transparent works.
In your Manifest
<activity android:name=".ActivityHere"
android:label="">
Write this statement under
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowTitleEnabled(false);
if extending AppCompactActivity use getSupportActionbar() if extending Activity use getActionBar() else it may give
null pointer exception
using android:label="" in AndroidManifest.xml for activity also works but your app won't appear in Recently used apps
you have two choice:
first:
getActionBar().setDisplayShowTitleEnabled(false);
If usign getActionBar() produced null pointer exception, you should use getSupportActionBar()
Second
getSupportActionBar().setTitle("your title");
or
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().hide();
You can change the style applied to each activity, or if you only want to change the behavior for a specific activity, you can try this:
setDisplayOptions(int options, int mask) --- Set selected display
or
setDisplayOptions(int options) --- Set display options.
To display title on actionbar, set display options in onCreate()
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
To hide title on actionbar.
getSupportActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
Details here.
i use this code in App manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:logo="#drawable/logo2"
android:label="#string/title_text"
android:theme="#style/Theme.Zinoostyle" >
my logo file is 200*800 pixel
and use this code in main activity.java
getActionBar().setDisplayShowTitleEnabled(false);
it will work Corectly
I tried this. This will help -
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar should be called after setSupportActionBar, thus setting the toolbar, otherwise, NullpointerException because there is no toolbar set.
Hope this helps
The only thing that really worked for me was to add:
<activity
android:name=".ActivityHere"
android:label=""
>
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().hide();
hope this will help
If you only want to do it for one activity and perhaps even dynamically, you can also use
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE);
as a workaround just add this line incase you have custom action/toolbars
this.setTitle("");
in your Activity
Use the following:
requestWindowFeature(Window.FEATURE_NO_TITLE);
While all of these are acceptable, if your activity only contains a main activity, you can edit res\values\styles.xml like so:
<resources>
<!-- 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>
</resources>
I've updated parent and added the .NoActionBar property to the Light theme from Android Studios Create Blank Application Wizard.
Simply extends your java file from AppCompatActivity and do this:
ActionBar actionBar = getSupportActionBar(); // support.v7
actionBar.setTitle(" ");
In your manifest.xml page you can give address to your activity label. the address is somewhere in your values/strings.xml . then you can change the value of the tag in xml file to null.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title_main_activity"></string>
</resources>
I am new to Android so maybe I am wrong...but to solve this problem cant we just go to the manifest and remove the activity label
<activity
android:name=".Bcft"
android:screenOrientation="portrait"
**android:label="" >**
Worked for me....
I remove the default appbar and run a custom toolbar instead using Theme.AppCompat.Light.NoActionBar then add a textview or something that extend to full toolbar width using attribute layout_width=match_parent and it pushes the title out of the toolbar. If you want to do this, you must study how to make a toolbar.
ActionBar actionBar = getActionBar();
actionBar.setTitle("");