Android: Icons don't appear in Action Bar - android

I'm following the google tutorial for action bars but the icons don't appear on it, this is the menu xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="refresh"
android:showAsAction="ifRoom"/>
<item android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:title="settings"
android:showAsAction="ifRoom"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_new"
android:title="add"
android:showAsAction="ifRoom"/>
</menu>
and this in my main acivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_bar, menu);
return super.onCreateOptionsMenu(menu);
}

Use yourapp:showAsAction instead of android:showAsAction
And make sure your Activity extends ActionBarActivity from the compat lib

Related

Android Action Bar Menu Item Title

Why does not show the action_call.title in the second device ??
I want to show the action_call.title on each device.
this device Android 5.1.1 (api22) 7"
this device Android 6.0 (api23) 5.96"
this is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_call"
android:icon="#drawable/call"
android:title="#string/action_call"
android:visible="true"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_info"
android:icon="#drawable/info2"
android:title="#string/action_info"
app:showAsAction="never|withText" />
</menu>
this is my onCreateOptionsMenu on MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem action_call = menu.findItem(R.id.action_call);
action_call.setTitle("+99");
return true;
}

Action Bar Menu Items Duplicating on Run

I am very new to Java Programming and Android Studio.
While creating an Options Menu, I created a list of options in the main menu with two items in a group sub menu.
This is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/giveup_setting"
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:title="#string/action_option1"/>
<item
android:id="#+id/new_game_setting"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/action_option2"/>
<item
android:id="#+id/help_setting"
android:orderInCategory="3"
app:showAsAction="never"
android:title="#string/action_option3"/>
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4" />
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
<item
android:id="#+id/about_setting"
android:orderInCategory="5"
app:showAsAction="never"
android:title="#string/action_option5"/>
<item
android:id="#+id/exit_setting"
android:orderInCategory="6"
app:showAsAction="never"
android:title="#string/action_option6"/>
</menu>
An this is how I inflate the menu in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.clear();
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
// Handle item selection
switch (menu.getItemId()) {
case R.id.help_setting:
//show help screens
return true;
case R.id.menu_color:
//change a layout of my main activity
return true;
case R.id.menu_text:
//change another layout of my main activity
return true;*/
case R.id.about_setting:
//show my about screen
return true;
case R.id.giveup_setting:
//resets the game
case R.id.new_game_setting:
//restarts the app
return true;
case R.id.exit_setting:
//exits the app
return true;
default:
return super.onOptionsItemSelected(menu);
}
}
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
What am I doing wrong?
if android:id="#+id/settings_setting" is the one you want to have a submenu don't close the item tag. Instead inflate <menu> inside the <item> tag.
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
</item>

How to show menu item in action bar?

I want the menu items to show on action bar. There is a lot of space on the ActionBar, but still they don't show up. Every item shown as three dots, why?
.java file
public class GalleryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/tools">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/shareId"
android:title="share"
android:icon="#drawable/share"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/delete"
android:title="delete"
android:icon="#drawable/delete"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/searchId"
android:title="search"
android:icon="#drawable/search"
MissionAndroid:showAsAction="ifRoom"/>
</menu>
Currently, you're using "http://schemas.android.com/tools" which does not offer the functionality you're looking for and your showAsAction modifiers are being ignored.
Try updating your main_menu.xml with the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/apk/res-auto">
...
</menu>
Better yet, to follow convention, replace MissionAndroid in this file with app:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
app:showAsAction="always"/>
...
</menu>

Finding view of an icon in overflow menu

I am trying to findview of an overflow icon. After clicking and opening the overflow icon, I tried using in onoptionsitemselected:
View view = getActivity().findViewById(R.id.menu_tag); // null
View view = getActivity().findViewById(R.id.mainMenu); // not null.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/mainMenu"
android:title="#string/text"
android:orderInCategory="101"
android:icon="#drawable/ic_more_vert_white_24dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_tag"
android:icon="#drawable/tag_32"
app:showAsAction="always|withText"
android:title="#string/tags"/>
<item
android:id="#+id/menu_profile"
android:icon="#drawable/user_32"
app:showAsAction="always|withText"
android:title="#string/profile"/>
<item
android:id="#+id/menu_debug"
android:icon="#drawable/insect_32"
app:showAsAction="always|withText"
android:title="#string/debug"/>
</menu>
</item>
</menu>
It is giving me null but working fine for actionbar items.
You should try to find view in onCreateOptionsMenu method like this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.optionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menufile, menu);
MenuItem item = menu.findItem(R.id.mainmenu);
return true;
}
I found a solution by setting actionview to imagebutton and then finding the view.

SearchView Widget not showing up in Action Bar

This can be considered as a extension of this Question
I have added following to the code to get a searchView Widget
res/menu/main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Mainactivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
System.out.println("TESTING: "+searchView);
return super.onCreateOptionsMenu(menu);
}
---
---
}
The app shows up fine but on touching the search button nothing shows up.
Have set the Sdk versions as follows:(4.0.3 -4.2.2)
android:minSdkVersion="15"
android:targetSdkVersion="17"
Change you menu to,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="always" />
</menu>
You're missing "collapseActionView" in your menu:
app:showAsAction="ifRoom|collapseActionView"

Categories

Resources