the setting options in another activity - android

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 :)

Related

How to parametrize options of a menu on Android?

I want to show/hide some options of a menu on an Android application depending of some strings that I will set to true/false on strings.xml file.
As an example, I will have these strings on strings.xml file:
<string name="option1">false</string>
<string name="option2">true</string>
<string name="option3">true</string>
So in this case only option2 and option3 have to be shown.
On the other side, I mean, in code, I tried the following:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getMenuInflater().inflate(R.menu.main, menu);
if((getResources().getString(R.string.option1)).equals("false")){
menu.getItem(R.id.option1).setVisible(false);
}
return true;
}
But option1 is still being shown on the menu.
What should I do to parametrize these options? I could just remove these options but I would like that in the future if my clients want these options active again, I could do it easily changing false value to true.
EDIT: Here is my menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/option1"
android:title="Option 1" />
<item
android:id="#+id/option2"
android:title="Option 2" />
<item
android:id="#+id/option3"
android:title="Option 3" />
....
</group>
</menu>
LAST UPDATE
I have notice that I was getting NullPointerException because the name of the menu was wrong. I have fixed it and now it does not give me any error.
But my problem comes because I have two different layouts that contains two different menus (one in each layout) and I inflate each layout depending of one string that comes on the login.
For example, considering that the string to inflate the first layout is "hello" I display the layouts as follows on onCreate function of my MainActivity:
if("hello".equals(stringForLayout)){
setContentView(R.layout.activity_main);
}else{
setContentView(R.layout.activity_main_secondary);
}
How can I parametrize the options of both menus so if the first one is being shown and I want to hide one option, I would be able to refer to that option of that menu?
Thanks in advance!
If you use Strings (e.g. s1 & s2), you have to compare them like this:
if(s1.equals(s2)) {...}
Btw.: You can also look at Shared Preferences in the android documentation to get a cleaner solution. There you can define boolean variables for the different menu options.
Finally, I found where my problems were.
The problem came because I did not notice that onCreateOptionsMenu and onPrepareOptionsMenu were not used so I deleted them.
Further, as I was using my menu inside a NavigationView I had to use, after inflate the NavigationView on my onCreate function of my MainActivity class, the following statement:
navigationView.getMenu().findItem(R.id.option1).setVisible(false);
There, I did not have any problem to hide my menu item succesfully.
Try this if you want to hide a specific menu item for a specific activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem mSearch = menu.findItem(R.id.mSearch);
mSearch.setVisible(false);
invalidateOptionsMenu();
}

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

adding items to actionbar correctly

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

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.

Changing view on Menu Item in Action Bar

I'm attempting to implement a refresh button in my app to allow a user to manually re-sync to a web server. The code works, but I'm having trouble figuring out the action views (at least, I think that's what I'm supposed to be using).
My menu item is here:
<item
android:id="#+id/main_menu_refresh"
android:enabled="true"
android:icon="#drawable/refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/refresh"
android:actionViewClass="android.widget.ProgressBar">
</item>
The problem is, it always shows the ProgressBar. I wondered if it worked like the search widget (the only example I really see online) and added the collapseActionView tag to the showAsAction and that prevented it from showing up immediately. However, when I click the refresh button, the icon disappears (good), but so does the Title in the action bar, and the ProgressBar appears on the left side of the window where the title used to be. Also not what I wanted.
As something of a last ditch effort, I attempted to add this to my code, and remove the actionViewClass from the XML:
MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
Log.w("MyApp", "Have Menu");
ProgressBar pb = new ProgressBar(ReadingList.this);
refresh.setActionView(pb);
That didn't work either, giving me a null pointer error on setActionView.
I need a solution that I can call from any function (there is an auto-sync period at the beginning I would like the ProgressBar to display during as well), and be able to return it to it's static icon after.
I tried reading through this question, but I am having trouble understanding what the answer means. I feel like I was trying to do just what it says, but I guess not. Any assistance is much appreciated.
Edit: For sastraxi's suggestion.
public class IconSwitcher extends LinearLayout{
public IconSwitcher(Context context) {
super(context);
ProgressBar pb = new ProgressBar(context);
ImageView iv = new ImageView(context);
addView(iv);
addView(pb);
}
}
This is my class thus far. However, when I try and reference it with:
MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
IconSwitcher ic = (IconSwitcher) refresh.getActionView();
I get a null pointer error. on creating the IconSwitcher. The button XML is as follows:
<item
android:id="#+id/main_menu_refresh"
android:enabled="true"
android:icon="#drawable/refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/refresh"
android:actionViewClass="IconSwitcher">
</item>
I'm just having a difficult time on referencing that IconSwitcher View.
Edit 2: I'm appearantly having trouble referencing the Menu Item at all.
MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
refresh.setVisible(false);
Also gives me a null pointer when I try and set the visibility. What is wrong with my references?
Instead of setting your action view class to a ProgressBar, set it to a custom LinearLayout class, as such:
class MyViewItem extends LinearLayout
Add a ProgressBar and an ImageView as children in its constructor with addView, and set these childrens' visibilities as either GONE or VISIBLE when your code requires it.
Try to use:
<item
android:id="#+id/main_menu_refresh"
android:enabled="true"
android:icon="#drawable/refresh"
android:orderInCategory="1"
android:title="#string/refresh"
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.ProgressBar">
</item>
Note this two lines:
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.ProgressBar"
Did you try to fetch your menu item like this?
MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);

Categories

Resources