Discussion Forums  >  Status Bar, Navigation Bar

Replies: 11    Views: 573

fusionsch
I hate code!
Profile
Posts: 508
Reg: Dec 28, 2010
Montreux Switze...
11,530
03/18/15 01:21 AM (4 years ago)

Android: how to display a custom icon in ActionBar?

Hi there!

I've been googling for a while to find an easy way of setting a custom icon in ActionBar. Found many things to customize it, but nothing as simple as this: just display a different icon.

I guess the solution is some custom code around here:

//setup the android action bar with a custom view for the title text...
actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_USE_LOGO);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setTitle(getString(R.string.loading));

(lines 125-131 in BT_activity_host.java)

It's a trick that would be quite useful: imagine you wish to re-use the icon of your app in the image header of your menu, without hiding the navig bar (aka ActionBar). So... you now have a beautiful header with a beautiful icon... but the same icon is displayed in tiny size just above... Not so beautiful, isn't it? Would be more elegant with some other image!

Any simple way to achieve this?

Thanks in advance for any tip!

Cheers

Jack
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/18/15 08:13 AM (4 years ago)
Just on certain screens?
 
fusionsch
I hate code!
Profile
Posts: 508
Reg: Dec 28, 2010
Montreux Switze...
11,530
like
03/18/15 09:21 AM (4 years ago)
No, overall!

A screen-by-screen solution would be nice too, but at the moment I'm not this far in the discovery of Android ;-)

Cheers

Jack
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/18/15 10:33 AM (4 years ago)
add this:

actionBar.setIcon(R.drawable.bt_no_icon);

after this:

actionBar.setDisplayUseLogoEnabled(true);

instead of "bt_no_icon", use the name of your image.make sure to put the image in the drawable folder

I think that should work.if not try

actionBar.setLogo(R.drawable.bt_no_icon);
 
fusionsch
I hate code!
Profile
Posts: 508
Reg: Dec 28, 2010
Montreux Switze...
11,530
like
03/18/15 10:51 AM (4 years ago)
Works great, thanks a lot, Chris!

BTW... In Advanced Menu, the header image is supposed to resize automatically. It does, but ratio is not kept. I've tried to change values in cr_menu_advanced.xml... no way!

Code looks like this:

<ImageView
android:id="@+id/headerImageView"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:scaleType="fitXY">
</ImageView>

Is there some error in Chris1 code?

Cheers

Jack
 
fusionsch
I hate code!
Profile
Posts: 508
Reg: Dec 28, 2010
Montreux Switze...
11,530
like
03/18/15 10:54 AM (4 years ago)
P.-S. I didn't try "actionBar.setLogo(R.drawable.bt_no_icon);"

as "actionBar.setIcon(R.drawable.bt_no_icon);" was ok at 1st try!
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/18/15 01:11 PM (4 years ago)
Is it the height that doesn't scale?
 
fusionsch
I hate code!
Profile
Posts: 508
Reg: Dec 28, 2010
Montreux Switze...
11,530
like
03/18/15 03:54 PM (4 years ago)
Yes, the height seems to be set to a fixed value, and there is something about scaling in the code:

android:layout_height="150dip"
android:scaleType="fitXY"

... but the height definitely doesn't scale! The right ratio isn't kept.
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/18/15 04:16 PM (4 years ago)
Try
"wrap_content"

For the height instead of the 150dp
 
fusionsch
I hate code!
Profile
Posts: 508
Reg: Dec 28, 2010
Montreux Switze...
11,530
like
03/19/15 01:45 AM (4 years ago)
I had already tried, and I retried: the header fills the full screen with "wrap_content"... Strange!
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/19/15 07:34 AM (4 years ago)
I have code for one of plugins where I did this. I'll post code later. you have to build apk for maps to work on android
 
fusionsch
I hate code!
Profile
Posts: 508
Reg: Dec 28, 2010
Montreux Switze...
11,530
like
03/19/15 07:45 AM (4 years ago)
;-)

Yes, I've seen that. To see the maps in action, I build APK and transfer it to phone with Instashare.

Thanks!
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.