i use action bar sherlock and i set custom layout in the menu:
<item
android:id="#+id/action_menu"
android:actionLayout="#layout/menu_item"
android:icon="#drawable/b_menu"
android:orderInCategory="1"
android:showAsAction="always"/>
<item
android:id="#+id/action_audio"
android:actionLayout="#layout/audio_item"
android:icon="#drawable/b_audio"
android:orderInCategory="2"
android:showAsAction="always"/>
<item
android:id="#+id/action_mySong_details"
android:actionLayout="#layout/my_song_properties"
android:icon="#drawable/b_mysong"
android:orderInCategory="2"
android:showAsAction="always"/>
to control those items i use:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Create an option menu from res/menu/items.xml */
getSherlock().getMenuInflater().inflate(R.menu.main_tab, menu);
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id.action_menu).getActionView();
Button b = (Button) v.findViewById(R.id.btnMenu);
return super.onCreateOptionsMenu(menu);
}
for some reason the action bar add unnecessary item, does anybody knows how this case happene?
You are using Action bar sherlock. So use SupportMenuInflater.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_menu:
// Intent i=new Intent(A.this,B.class);
//startActivity(i);
return true;
}
return false;
}
I found the mistake: i remove the android:orderInCategory from all the items and it's ok now, i this case i don't need this option.
Related
I have added two menu item . Both of them works good but whenever i press menu button new menu items appear beside the old one . Like below u can see
below is my menu item xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:icon="#drawable/search_white_24dp"
android:id="#+id/searchmenu"
app:showAsAction="always">
</item>
<item
android:icon="#drawable/settings_white_24dp"
android:title="Setting"
android:id="#+id/settingmenu"
app:showAsAction="always"
/>
</menu>
and here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchmenu:
editText = DuwaManager.openSearchBox(this,
getSupportActionBar(), "DuwaListView");
break;
case R.id.settingmenu:
break;
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
please help where i am wrong ?
Sorry this will get to long but , I have second Question
2) My second question i have another menu. xml in which there is share icon in place of search icon , But when i am trying to show on action bar with same code menu item does not show on action bar . below is my another menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="Share"
android:icon="#drawable/share_white"
android:id="#+id/share_tarika"
app:showAsAction="always"
/>
<item android:title="Setting"
android:icon="#drawable/settings_white_24dp"
android:id="#id/settingmenu"
app:showAsAction="always"/>
</menu>
and its code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tarika_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share_tarika:
break;
case R.id.settingmenu:
break;
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public void onCreateOptionsMenu(Menu menu ) {
menu.clear();// use menu.clear
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu."your current activity name ", menu);
return true;
}
use menu.clear()
The best to do is as this :
#Override
public void onCreateOptionsMenu(Menu menu ) {
getMenuInflater().inflate(R.menu."your current activity name ", menu);
return true;
}
if you have just one menu. Then set menu from Activity page and return true then no need to clear menu. and no duplicate menu will appear.
But if you use two fragment and have to change activity menu as per fragment then only need to clear menu and reset it with new one.
And if you are using onCreateOptionsMenu(Menu menu) in fragment then remove them.
I've tried using the following code but no checkbox appear, only text :
<item
android:id="#+id/menuShowDue"
android:actionViewClass="android.widget.CheckBox"
android:title="#string/string_due"
android:checkable="true"
app:showAsAction="ifRoom" />
Is it not possible to add it via menu ? Should I do something else ?
I'm using android.support.v7.widget.Toolbar as Toolbar.
Please make changes as shown below to app:actionViewClass and app:showAsAction and it should work for you.
<item
android:id="#+id/menuShowDue"
android:checkable="true"
android:title="#string/string_due"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom|withText" />
Also make the relevant changes to onCreateOptionsMenu(). Sample text pasted below;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
CheckBox checkBox = (CheckBox) menu.findItem(R.id.menuShowDue).getActionView();
checkBox.setText("Sample Text");
return true;
}
You can set the title at OnCreateOptionsMenu. Use MenuItem should be sufficient.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem checkBoxMenuItem = menu.findItem(R.id.menuShowDue);
checkBoxMenuItem.setTitle("Sample Text");
return true;
}
In OptionsItemSelected, check if checkbox selected or not:
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuShowDue:
if(item.isChecked())
{
item.setChecked(false);
}else{
item.setChecked(true);
}
break;
}
Trying to add actionar icons to actionbar and i'm using support library here is link i'm following developerspage
and here is my code to implement it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == R.id.toggle) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return true;
}
and menu.xml is
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/toggle"
android:icon="#drawable/menu"
android:orderInCategory="100"
android:title="menutoggle"/>
In your xml, you can include this in each item you want to be clickable:
android:onClick="aRandomMethod"
Then in your activity, you must implement a method that supports the click, in this case, a method called aRandomMethod, like this:
public void aRandomMethod(MenuItem item){
/// stuff to do when you click in the button
}
If you want your icon to be always displayed in the action bar, add:
android:showAsAction="always"
The onOptionsItemSelected method that you have in your code is when you have the settings icon (three small dots) and then a submenu in the icon; is to handle the submenu.
How I can attach icon of progress bar to action bar that display loading process of some Internet data?
For example:
And icon while load data
First of all you have to create a custom menu to use inside your activity.
Secondly, you have to request the feature of progress bar inside your "onCreate".
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
// rest of your code
[....]
}//end of onCreate
Then your my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:icon="# drawable/ic_menu_refresh "
android:orderInCategory="100"
android:showAsAction="always"
android:title="Refresh"
android:visible="true"/>
</menu>
Back to your activity, you have to inflate this menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked
//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}
PS: All the above code is working using the ActionBar Compat Library.
EDIT:
Ok, to hide the action bar icon do this:
-Create a menuitem outside your functions:
MenuItem refreshIcon;
Edit your onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
//Initializing menu item
refreshIcon = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}
Then when you press the button change the visibility:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked
//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
//hiding action icon
refreshIcon.setVisible(false);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}
Finally, when your data is finished updating set the visibility to "true" and hide progressbar:
[...] //progress inside your AsyncTask or wherever you have your update
//show Refresh Icon again
refreshIcon.setVisible(true);
//hide the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
I want to call an activity after clicking on share icon in action bar. For this I
create a menu named 'flip' which contains an item named 'menu_share' for Android
2.2+. Flip menu is inflated in 1st activity named 'ShareActivity'. After this I want to call other activity after clicking on shareicon in action bar but icon not responding.
Code for 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/menu_share"
android:title="share"
android:icon="#drawable/shar"
yourapp:showAsAction="ifRoom"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Code for 1st activity.
public class ShareActivity extends ActionBarActivity {
SocialAuthAdapter adapter;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
ActionBar ab=getSupportActionBar();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
#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.flip, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.menu_share:
startActivity(new Intent(this,ShActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You have set an ActionProvider with yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider". This is likely intercepting the event, thus preventing your Activity from getting the call to onOptionsItemSelected().
Since you want to start your own Activity instead of using Android's default share behavior, simply remove this line.