adding items to actionbar correctly - android

I need help figuring the right way to control the actionbar.
At the onCreate function i added:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
And added actionbar.xml to the layout folder with 3 items in it: TextView (app name) and 2 ImageViews.
It works, i mean i can see this new actionbar, but i'm pretty sure i'm doing it the wrong way, in tutorials im reading they're talking about adding those items to a menu.xml file which i couldn't find, and using onOptionsItemSelected function to set the behaviour.
As I'm a begginer, would appreciate an explanation of what it is that i'm doing, why it's wrong and how to do it correctly with the menu.xml

Ok, first you are right, Android have an out of the box solution to put "Action Button" and Title on the Action Bar
1.Title
When you create an Activity, it will add some code into your AndroidManifest.xml, here is the example:
<activity
android:name="AddAddressActivity"
android:label="#string/title_activity_add_address" >
</activity>
so you change your title here by changing android:label, the best practice is you need to put all your string asset under res/values/string.xml like this
<string name="title_activity_add_address">Add New Address</string>
Action Button
First add this override method to your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.address_list, menu);
return true;
}
Later, you create address_list.xml under res/menu folder, and inside it you can put the list of your Action Button there
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.vi8e.giant.AddressListActivity">
<item
android:id="#+id/action_add"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_new"
android:title="#string/action_save"/>
Android:title is to put your menu title, this title will show up when you long press the menu
Android:icon here is the image that you want to show for the menu, put the image under res/drawable
Edit: forgot to mention about how to trigger your menu
you can put onOptionsItemSelected method on your Activity, here is the example
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
//do something
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
note: Action Bar has a limited ammount of space, so if you have a lot of menu, it will be collapse into "three dots" icon on the top-right corner

Related

the setting options in another activity

in my app on the top right of the action bar, I have the setting as three dots (as the usual setting). but when I click on it, it shows a white rectangle that has the word "setting" on it! But I don't want that!! I want just the three dots and when I click it I want it to take me to another activity right away(without showing the option setting on a rectangle).
this my code for the setting xml:
<item
android:id="#+id/action_settings"
android:orderInCategory="0"
android:title="#string/action_settings"
android:textColor="#color/colorBackground"
app:showAsAction="never" />
</menu>
The three dots you're talking about are usually there if you don't have enough room in your action bar and you want some of your menu items to show in a menu instead of directly displaying it to the action bar. The best thing for you is to create another item and set app:showAsAction to always and call a method using onOptionsItemSelected event:
<item
android:id="#+id/your_id"
android:title="your title"
app:showAsAction="always" />
Call it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.your_id:
//code here...
}
return super.onOptionsItemSelected(item);
}
Currently your app has a default menu that has a settings item added. You can create your own menu options.
Create a new menu.xml file that contains 1 item whose image is the three dots.
In your class, inside the onOptionsItemSelected method, call the activity that you want to open.
Hope this helps :)

Dynamic radio options in the Action Bar (Android)

I thought I should ask a new question for this, but for some context I was able to get the label in position thanks to the good people at How to get a label in the Android action bar
.
So now that I've got that, I want the user to be able to tap the Administrator button and then change it to a different mode (probably just "Administrator", "User", "Guest" to start with but there may be more in the future).
How can I get a list of radio boxes to appear when the button in the top right is clicked? Ideally I want to be able to define those various modes dynamically from within the Java class so that if a new type gets added to the database it will be automatically picked up.
If anyone could point me in the right direction I'd appreciate it. I have seen a few examples from Googling, but unfortunately none of them involved the sort of customised drawable I'm using - and none of them had dynamically populated radio options either.
Thanks
You probably want to have a menu which contains your different items.
Do something like this to create the menu and inflate your xml:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
And then for handling the click events:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.admin:
switchToAdminUI();
return true;
case R.id.guest:
switchToGuestUI();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And your my_menu.xml could look something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/abs_ic_menu_moreoverflow_material"
android:showAsAction="always">
<menu>
<item
android:id="#+id/admin"
android:showAsAction="never"
android:icon="#drawable/ic_admin"
android:title="#string/admin"/>
<item
android:id="#+id/guest"
android:showAsAction="never"
android:icon="#drawable/ic_guest"
android:title="#string/guest"/>
</menu>
</item>
</menu>
You can see the android:showAsAction="always" above, which means that it will always show as an action bar icon, and then you put the sub menu items in there.
Try it out and you can also read more about menus here https://developer.android.com/guide/topics/ui/menus.html

How can I remove dropdown actionbutton overflow?

I'm trying to remove the dropdown menu ActionButton Overflow, from Action Bar. In the case, it would be the one that has 3 points and default option "Settings".I would like to remove this item "Settings", so that, by clicking on the menu icon, he has to take action automatically. Any idea?
go to folder menu->main then remove item settings then you can add another item (for example)
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:icon="#drawable/refresh">
</item>
and in your activity:
onOptionsItemSelected(MenuItem item) // write your switch case to handle the clicked item.
the line that allows the option to be directly on the action bar is:
android:showAsAction="ifRoom"
You cannot get rid of the overflow. You can ask for the action bar to consider making one (or more) of your action bar items be toolbar-style buttons in the action bar. To do that:
if you are using the native action bar, add android:showAsAction="ifRoom" to the <item> in your menu resource
if you are using the appcompat-v7 action bar backport, add app:showAsAction="ifRoom" to the <item> in your menu resource (assuming that you have the appcompat-v7 namespace set to be app)
However, the decision on whether or not to show the item as a toolbar button or to put it in the overflow is up to the action bar, not you. For example, if you ask for 8 items to all be toolbar buttons, while that may be possible on a 10" tablet in landscape, there will not be enough room for all of them on a phone-sized screen in portrait. Some of your items will be toolbar buttons; the rest will go into the overflow.
The Menu is inflated in the onCreateOptionsMenu method, and uses a menu XML in the menu folder (default) to retrieve items. If you would like to perform an action on the keycode menu, you can use the following
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
Toast.makeText(getApplicationContext(), "Menu Pressed", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keycode, e);
}
if you have no use for menu items, you can also go ahead and get rid of the onCreateOptionsMenu and onOptionsItemSelected methods, as well as the menu XML, that is up to you though
You have to remove all methods that create the menu (onCreateOptionsMenu etc).
Then in the XML file for the Toolbar, you have to add in the toolbar a custom icon. For example:
<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/ThemeOverlay.AppCompat.Light">
<----YOUR CUSTOM ICON/BUTTON HERE---->
</android.support.v7.widget.Toolbar>
Then programmatically you can add an onClickListener on the custom icon/button and you can do whatever you want with it then.

How to remove overflow menu 3-dots?

Please help me understand. I read somewhere saying that if I set up android:targetSdkVersion="14.
These 3-dots will be gone.
Why these 3-dots still show up on mine?
They don't show up until I come back from another activity. I pressed the menu key after I came back from another activity, they showed up....
I am using actionbar menus so I don't need these ugly 3-dots on my app.
How can I remove them completely?
Thanks
Manifest:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyAppActionBarTheme" >
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the actionbar menu; this adds items to the action bar.
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Add the following function to your activity:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.action_settings);
settingsItem.setVisible(false);
return false;
}
Edit:
Sorry try with return false instead of return true!
in menu.xml u can add android:showAsAction="" with these values never,ifRoom,always,withText,collapseActionView you can use them in | combinations.
in you case you can use android:showAsAction="always"
If you go into you ~res/menu/main.xml there should be some code within the tag
<item>
//setting button code is here
</item>
Just delete all that. Then go into your main activity and delete the corresponding code which should be in onOptionsItemSelected().
C'est voila.
I just thought i should share the easiest process of removing the three dots...
go to the menu folder
go to the second dropdown .xml
view the design on the right pane after clicking on the layout you will see a checkbox which says visible check and uncheck it to make it invisible.
then the 3dots is gone

Trouble changing icons on ActionBar

My application calls a web service on startup to see if the user is logged in. If they are logged in it needs to show a logout icon. If they aren't logged in, it needs to show a login icon. If their version of the application doesn't support logging in, it need to continue showing no icon.
How do I get the icon to show when the web service returns? I can't call invalidateOptionsMenu() because I'm using 2.3. Also, if I try to add it in onPrepareOptionsMenu() it shows up on the menu when you press the menu button instead of on the ActionBar.
I've done something similar this way:
In your action bar menu, have both the log in and log out items already there (I would advise putting them in an xml instead of creating it in the code so you can easily assign the items an id). Then just hold a reference to your menu when you create it so you can modify it later.
Menu myActionBarMenu;
/**
* Creates action bar items.
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menuNameHere, menu);
myActionBarMenu = menu;
}
Then, when you find out the user is logged in, set the visibility of the log in item to false like this:
myActionBarMenu.findItem(R.id.logInAction).setVisible(false);
myActionBarMenu.findItem(R.id.logOutAction).setVisible(true);
reverse the visibility in the case the user is logged out.
This would be what your xml menu would look like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/logInAction"
android:showAsAction="ifRoom"
android:title="Log In"/>
<item
android:id="#+id/logOutAction"
android:showAsAction="ifRoom"
android:title="Log Out"/>
</menu>
I use this code to add menu items to the ActionBar:
/*************************************/
/* Create the actionbar options menu */
/*************************************/
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Settings")
.setIcon(R.drawable.ic_menu_moreoverflow_normal_holo_light)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
You should be able to add an if statement in there and change the icon accordingley

Categories

Resources