I am trying to show two icons in the actionbar, one of which is a menu with a few children. However, I am not able to. I have tested a few answers from other similar questions in SO but to no avail. Not even one icon is showing up. Here is the code. Thanks for your help.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.semantic.semanticOrganizer.sem.activities.HomeActivity" >
<item android:id="#+id/viewTags"
android:icon="#drawable/ic_action_labels"
app:showAsAction="always"
android:title="Tags"
/>
<item
android:id="#+id/a_More"
android:icon="#drawable/ic_more_vert_white_36dp"
app:showAsAction="always"
android:title="More">
<menu>
<item android:id="#+id/action_view_notes"
android:title="Notes"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/action_view_habits"
android:title="Habits"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/action_view_checklists"
android:title="Checklists"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/action_view_tags"
android:title="Tags"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
</item>
</menu>
Java Code
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
stuffWhichDoesNotInvolveGettingActionBar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_view_tags) {
return true;
}
if (id == R.id.action_view_notes) {
return true;
}
if (id == R.id.action_view_checklists) {
return true;
}
if (id == R.id.action_view_habits) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void stuffWhichDoesNotInvolveGettingActionBar(){
//Code
}
Edit: As Neutrino suggested, I tried to shift to appCompat Theme. But then this error showed up on Win64 machine. Resource not found error to the attribute "actionModeShareDrawable". I changed the compileSdkVersion to 21 and it dint work. So, I might have to do it without AppCompat for now. I am currently using Holo. So please suggest a fix for Holo theme.
Edit
I found a work around for the error I mentioned by using v20 of the support library instead of v21 since I found out that Gradle 2.3 build system is not around in Android yet and the bug was only fixed in that version (A Bug Fixed Jar of Groovy 2.3.5 was released). Then I was able to use ActionBarActivity and now the action bar icons are visible. Thanks everyone and especially Quark.
Extend ActionBarActivity instead of Activity, ie make
public class HomeActivity extends Activity
as
public class HomeActivity extends ActionBarActivity
EDIT
ActionBarActivity is now deprecated to support toolbar. To use toolbar, use -
public class HomeActivity extends AppCompatActivity
Be sure to include the latest version of support library in build.gradle (Module: app) -
dependencies {
compile 'com.android.support:appcompat-v7:22.1.1'
}
Read more about toolbar and AppCompatActivity here, here and here.
I tried compiling your code and the following worked for me.
Replace app:showAsAction with android:showAsAction for all the menu items for which you want an icon to appear.
Related
I'm trying to use Android ActionBar in my app, and have an option that's hidden away in the overflow menu.
There's a lot of documentation out there, but it's confusing because most of it is only relevant to very old versions of Android, and when you try applying the same concepts, they don't work anymore or work differently.
This is in my Activity layout
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:titleTextColor="#android:color/white"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
And this is in my Activity's onCreate() method
// sets up activity toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.showOverflowMenu();
myToolbar.setTitleTextColor(R.color.lightPrimaryText);
I've also tried inflating a menu xml file from the onCreateOptionsMenu(), but that also didn't give me the results I wanted.
Define a Menu for your Toolbar in the res/menu resource folder, for example:
toolbar_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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.MainActivity">
<item
android:id="#+id/action_sign_out"
android:title="#string/toolbar_sign_out"
app:showAsAction="never"/>
</menu>
Setting app:showAsAction="never" ensures that this MenuItem will not be shown in the Toolbar, but placed in the overflow menu instead.
The theme of your Activity should be (or derive from) one of the NoActionBar themes (Theme.AppCompat.NoActionBar for example, or Theme.MaterialComponents.NoActionBar if you're using Material Components).
In your Activity, set up your Toolbar:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
And override onCreateOptionsMenu() to inflate your previously defined menu resource:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
You can override onOptionsItemSelected() to define the onClick behaviour of your MenuItem(s):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sign_out: {
// do your sign-out stuff
break;
}
// case blocks for other MenuItems (if any)
}
return true;
}
in manifest file declare
android:theme="#style/AppTheme.NoActionBar"
like this :
<activity
android:name=".ActivityName"
android:label="#string/label"
android:theme="#style/AppTheme.NoActionBar" />
and add this to your style :
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and call this in Activity onCreate() :
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
override this method in activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.product_list, menu);
//U can find item set icon and stuff...
MenuItem item= menu.findItem(R.id.action_search);
return true;
}
and declare your menu like this for overflow menu:
<?xml version="1.0" encoding="utf-8"?>
<menu >
<group>
<item
android:id="#+id/sign_out"
android:title="#string/sign_out" />
<item
android:id="#+id/about"
android:title="#string/about" />
</group>
</menu>
and for handle item selection call this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.sign_out:
//do stuff
break;
}
return super.onOptionsItemSelected(item);
}
done :)
Simple do This copy this code on your MainActivet`
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Group gp=(Group)findViewById(R.id.order);
return true;
}
return super.onOptionsItemSelected(item);
}`
Now Make Directory file for menu name for this go on Android_Studio->app Folder->Right_Click->New->Directory-> Enter name menu now Create a xml file in there with menu2.xml name
and past this code on menu2.xml file
<?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_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"
android:icon="#android:drawable/ic_input_add"
/>
</menu>
if any Query Please text me
I am trying to add DPAD navigation support for my app. However, I found it is not possible to move the focus to the action menu items on Android 5. But it works fine on Android 4.4.4, see attached screenshot below, the robot icon is focused and highlighted.
Same code does not work on Android 5.1.1 Nexus7. Does anyone have any idea?
Attached my code and xmls.
MainActivity.java:
package com.example.zzztest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#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.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML:
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_asdf"
android:icon="#drawable/ic_launcher"
android:title="#string/action_settings"
app:showAsAction="always"/>
</menu>
Layout XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="always"/>
I used app:showAsAction="always".
Press Dpad "Menu" Button and it can be selected.
I just created an app including the new Android toolbar. My problem is: How would I hide this overflow menu icon/button: http://prntscr.com/62mmus ?
I already tried this, but it's not working:
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.hideOverflowMenu();
Just use:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
This is how I did it.
This is my original menu_main.xml file under the menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
Delete the item:
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
Go to your main activity and comment out the following if (id == R.id.action_settings) :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
Run your app - the overflow menu icon is gone.
what worked for me was:
add the following:
android:visible="false" to the menu item in the menu file (global.xml) in the menu folder.
ex:
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"
android:visible="false"
/>
Just return false when calling the method onCreateOptionsMenu
Ex:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!showToolbarMenu)
return false;
getMenuInflater().inflate(R.menu.main_activity, menu);
return super.onCreateOptionsMenu(menu);
}
Use the command
invalidateOptionsMenu();
And your problems will be solved.
This worked for me, I just deleted the line getMenuInflater().inflate(R.menu.main_activity, menu);
Don't implement the onCreateOptionsMenu(Menu menu) method. If you do anyway ,don't use this method getMenuInflater().inflate(R.menu.main, menu); as this line in the onCreateOptionsMenu method inflates the overflow button with the menus declared in resource R.menu.main.
I have made a custom theme for my app which uses action bar.Now when i run my app the actionbar is visible but button in it is not visible.I dont know what went wrong.
The button is visible when i click on menu button (the 3 hardware button) but not displaying on the action bar
Style.Xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="TripLoggerTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/TripLoggerActionBar</item>
<!--<item name="android:homeAsUpIndicator">#drawable/test</item>-->
</style>
<!--use to style the actionbar-->
<style name="TripLoggerActionBar" parent="android:Widget.ActionBar">
<item name="android:background">#6e784c</item>
<item name="android:showAsAction">ifRoom|always|collapseActionView|never|withText</item>
<item name="android:displayOptions">homeAsUp|showHome|showTitle</item>
</style>
Menu.Xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/images"
app:showAsAction="always" />
</menu>
Code
public class MainActivity extends Activity {
private int[] images = {R.drawable.images, R.drawable.images_2, R.drawable.images_1, R.drawable.images_4, R.drawable.images, R.drawable.images_2};
private GridView gridView;
private LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
linearLayout = (LinearLayout) findViewById(R.id.root);
getActionBar();
// gridView = (GridView) findViewById(R.id.grid);
// gridView.setAdapter(new CustomAdapter(MainActivity.this, images));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
linearLayout.setVisibility(View.VISIBLE);
break;
}
return true;
}
As the screenshots shows,when i click on the 3 dots from below test is visible but it should be visible on the top where i have marked a cross
I tried finding out what could be the problem but all in vain.
I believe what you are experiencing is the standard Android behaviour. You may want to read the following text on the official docs on Action Overflow:
The overflow icon only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key.
First, from snapshot, it seems that you are testing in emulator. Check if same happens on the device.
I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
I inflate the action bar menu with this on each activity I use it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
Note:
ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import
import com.actionbarsherlock.view.MenuItem;
and not
import com.android.view.MenuItem;
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
which #adneal mentions.
In my opinion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreateDialog(getTaskId());
}
});
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/add_text_id" android:title="Add"
android:icon="#drawable/ic_add_btn"
android:orderInCategory="100" app:showAsAction="ifRoom" />