How can i change android actionbar picture - java

i am new on android and i am using android studio to develop. i want to change actionbar top right corner button image to another one but after compile to my android device the button still remain the ... but my customize image. please help!! below is my code. my public class is extend activity.
menu.xml
android:id="#+id/action_share"
android:orderInCategory="100"
android:title="Share"
android:icon="#drawable/ic_search_black"
app:showAsAction="ifRoom"

Vic3ai, That icon all the way to the right is the Action Overflow and it will always show a drop down list of action buttons that don't fit on the actionbar (either because there isn't room or because we decided to put some in the overflow on purpose).
This might give you a better idea of what the overflow is and is used for, as well as a general overview of the actionbar - http://developer.android.com/design/patterns/actionbar.html.
It sounds like what you actually want is to add an action button to your action bar. You can do that like so:
First add a new item in your menu.xml for which ever activity you want it to appear in:
<item
android:id="#+id/camera_button"
android:title="#string/camera"
android:icon="#drawable/camera_icon"
android:orderInCategory="100"
android:showAsAction="always"
/>
Here the important thing is android:showAsAction="always". This is what makes it appear as an action button on the action bar. You could also use value 'ifRoom', which does what it says on the tin: If there's room on the actionbar the button will display, otherwise it goes into the action overflow.
Now that we have the actionbutton in existence we go to our actvity.java and respond to the button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.camera_button:
//your code here, eg:
openCamera();
return true;
// if you have other buttons, more cases would go here
}
return super.onOptionsItemSelected(item);
}
So far this should give you an Action Button on the actionbar that will run whatever could you define in your activity.java but the action overflow will still be there. The easiest way to remove it is to go back to your menu.xml and remove/change any items that would appear in the overflow.
android:showAsAction="never"
will automatically put action in to the overflow
If you have a lot of action buttons and any of them have android:showAsAction="ifRoom", you might find an overflow button on the far right again
NOTE, use app:showAsAction="always" instead of android namespace if you're using appCompat compatibility.
Hope this helps!

To change the Action Overflow Icon - default is three vertical dots - you have to add an actionOverflowButtonStyle item to your main theme declaration AND then define it in styles.xml:
<resources>
<!-- base application theme -->
<style
name="AppTheme" parent="#android:style/Theme.Holo">
<!-- include overflow style in theme -->
<item name="android:actionOverflowButtonStyle">#style/OverFlowIcon</item>
</style>
<!-- styles -->
<style
name="OverFlowIcon"
parent="#android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">#drawable/ic_search_black</item>
</style>
</resources>
Of course if you're using AppCompat themes don't forget to use atributes without the android namespace or it wont work - ie
<item name="actionOverflowButtonStyle">#style/OverFlow</item>
instead of what you see above.
Also don't forget to include your theme in application tag of AndroidManifest.xml if you change make a custom theme:
<application
android:name="com.example.android.MainApp"
android:theme="#style/AppTheme">
</application>

Related

Android Status bar and Navigation issues

Recently I used this java code in Main_Activity:
(getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS))
for status Bar Transparent purpose. All went well, but my Android Navigation Bar also went to transparent policy.
Now I need only transparent my Status Bar, but not the Navigation Bar.
You have two options to do, first, one which I recommend for you would be using an external library for status-bar styling like StatusBarUtil library, which enables you to fulfill your requirement in just one line:
StatusBarUtil.setTransparent(Activity activity) // yo be placed in every activity you want the status bar to be transparent in
And this library doesn't affect the navigation bar.
Just make sure first to implement the library in your app level Gradle:
implementation'com.jaeger.statusbarutil:library:1.5.1'
Second option, is to go for layout customization, as mentioned in this tutorial, you can add the following to your style.xml:
<style name="AppTheme.TransparentTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
And apply this style to the Activity in the manifest:
<activity
android:name=".HomeActivity"
android:theme="#style/AppTheme.TransparentTheme">
</activity>
As described in the tutorial:
I have set android:windowDrawsSystemBarBackgrounds to true.
This is a flag whose description is given below:
Flag indicating whether this Window is responsible for drawing the
background for the system bars. If true and the window is not
floating, the system bars are drawn with a transparent background and
the corresponding areas in this window are filled with the colors
specified in {#link android.R.attr#statusBarColor} and {#link
android.R.attr#navigationBarColor}. Corresponds to {#link
android.view.WindowManager.LayoutParams#FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS}.
Don't forget to add the attribute android:fitsSystemWindows="true" to the top root of your activity layout.

Overlay image on Action Bar

How do I overlay image over an Action Bar and status bar? Here is what I want it to look like -
I read this but it gives different results than what I want. The Action Bar still has some opacity and the status bar has no effect at all.
Is there any way to do this?
Add these two lines of code to your styles-v21.xml file:
<item name="android:windowTranslucentStatus">true</item>
<item name="windowActionModeOverlay">true</item>
Edit:
You want it in the v21 file because Android versions below 5.0 don't support this.
Also...for Material Design, you should be using a ToolBar not an Action Bar. Here's how to change the color of a Toolbar.
toolbar.getBackground().setAlpha(0);
Check this answer.
This one worked perfectly in my project.
This library is also great to explore:
https://github.com/ManuelPeinado/FadingActionBar

Creating bodyless activity

Currently I am trying to make an activity that can be moved on the screen , in other words this activity can float on the screen. I know I am missing something that should be added to achieve this functionality.
What should I do to achieve floating activity which can be moved around anywhere on the screen?
If you're looking to do something similar to the Facebook chat circles (where you have a view on top of other apps) then take a look here: http://www.cloudinfy.com/2013/06/android-chat-head-view-like-in-facebook.html
Basically you need to start a service and add a view to the window manager.
If you're looking to simply create an activity to look like a dialog then have a look here: Android Activity as a dialog
This is very similar to a normal activity but the theme is set to Theme.Dialog
There's a good solution here: http://cases.azoft.com/android-tutorial-floating-activity/
They just add an attribute to the theme of a tablet Activity:
<item name="android:windowIsTranslucent">true</item>
If any problems on KitKat devices, add some lines to the theme:
<item name="android:windowIsFloating">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:backgroundDimEnabled">true</item>

How to change the background colour of the spinner on the aciton bar

I'm working on an android app and have come across an issue, I have managed to get the list items on the action bar to highlight to the color i want but the 'spinner' area around the item is staying the default blue, does anyone know how i can change this color?
I've fixed it!
I used this website which allows you set colors etc. for your action bar and then you just put them in your drawables folder and link it up with the style sheet.
I'm not sure if its the cleanest fix but it works.
http://jgilfelt.github.io/android-actionbarstylegenerator/
Try using android:background within your spinner.
Try this:
(without Sherlok)
<style name="Theme.MyTheme" parent="#style/YourTheme">
<item name="android:selectableItemBackground">#drawable/ab_selector</item>
<item name="android:actionBarWidgetTheme">#style/WidgetStyle</item>
</style>
<style name="WidgetStyle" parent="Widget">
<item name="android:itemBackground">#drawable/ab_menu_selector</item>
</style>

Interaction of Themes, background colors, and Action bar

I am failing to understand the mode of interaction between the appearance of action bar and and thematization. My app is set to use the default theme, which I take to be dark:
<style name="AppBaseTheme" parent="android:Theme">
</style>
Removing the action bar from the app via app-wide style results in black background for the main activity:
<activity
android:name="com.atlarge.motionlog.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
>
Without the android:theme="#android:style/Theme.NoTitleBar.Fullscreen" line, the activity background is white. If the action bar is removed in code in onCreate() method of the activity, the action bar is also gone, but the background remains white:
ActionBar actionBar = getActionBar();
actionBar.hide();
TL;DR: Summary of behavior:
Action bar present: white background
Action bar removed via code: white background
Action bar removed via XML: black background
Why is that? Can someone explain (or point to a good resource) on the interaction of appearance of action bar via code vs. XML and the background color?
Removing the action bar in the onCreate is just hiding the ActionBar view.
It is not changing the theme.
Setting android:theme="#android:style/Theme.NoTitleBar.Fullscreen" is setting a theme to your activity which comes with any inherited styles in that theme hierarchy.
If you take a look at themes.xml in the android source, you'll see that in the style <style name="Theme"> theres the item <item name="colorBackground">#android:color/background_dark</item>
Then <style name="Theme.NoTitleBar"> which inherits all the styles of Theme sets <item name="android:windowNoTitle">true</item>
Then <style name="Theme.NoTitleBar.Fullscreen"> which inherits from NoTitleBar and Theme sets <item name="android:windowFullscreen">true</item> and <item name="android:windowContentOverlay">#null</item>
which explains why you have a dark background when applying that style.
If you set your theme to Theme.Light.NoTitleBar.Fullscreen you'd achieve the same effect but you'd inherit <item name="colorBackground">#android:color/background_light</item> from <style name="Theme.Light"> which should be a light color.
Alternatively, you can extend any them you want and override any of the styles.
so, for example, you could do something like...
<style name="MyTheme" parent="#android:style/Theme.NoTitleBar.Fullscreen">
<item name="colorBackground">#color/my_light_color</item>
<item name="windowBackground">#drawable/screen_background_selector_light</item>
</style>
Here are some resources that might help you understand themes a bit more:
http://developer.android.com/guide/topics/ui/themes.html
http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/
And if you want to create or extend your own themes, its always worth looking at the android source to see what you can extend and override:
http://developer.android.com/guide/topics/ui/themes.html#PlatformStyles
Hope that helps.

Categories