Java android Add dynamically item to menu - java

I want to added to menu item but a user can choose what he want to see or hide , but I don't know how I can do it . I did this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="NARZĘDZIA">
<menu>
<item
android:id="#+id/show_point"
android:icon="#drawable/point"
android:title="Pokaż wybraną pozycję obiektu" />
<item
android:id="#+id/sync"
android:icon="#drawable/sync"
android:title="Pojedyńcze odpytanie" />
<item
android:id="#+id/show_chooce"
android:icon="#drawable/show_chooce"
android:title="Pokaż wybrane obiekty" />
<item
android:id="#+id/my_location"
android:icon="#drawable/position2"
android:title="Moja lokalizacja" />
</menu>
</item>
<item android:title="APLIKACJA">
<menu>
<item
android:id="#+id/settigs"
android:icon="#drawable/set"
android:title="Ustawienia aplikacji" />
<item
android:id="#+id/about"
android:icon="#drawable/about"
android:title="O programie" />
</menu>
</item>
</menu>
Now I want to create a Activity in which user can choose what it will be show , but I don't have idea how I can hide this item on menu

you can change java file
//menu intialation
#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);
menu.clear();
MenuItem item =
menu.add(Menu.FIRST, R.id.dashboard_nodifycation, 3, "NODIFICATION");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.FIRST, R.id.System_log, 4, "LOGOUT");
dbHelper = new Database_for_GCM_data(Activity_Drawer_Dashboard.this);
int unread_count = Integer.parseInt(dbHelper.getUnreadCount());
// calling function for nodification inflater
item.setIcon(buildCounterDrawable(unread_count, R.drawable.white_nodification, getApplicationContext()));
return true;
}

public class MyActivity extends AppCompatActivity {
private Menu mToolbarMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//init menu here
mToolbarMenu = menu;
getMenuInflater().inflate(R.menu.toolbar, menu);
setToolbarMenuItemVisibilties();
return true;
}
private void setToolbarMenuItemVisibilties() {
final MenuItem menuItem1 = mToolbarMenu.findItem(R.id.item1);
final MenuItem menuItem2 = mToolbarMenu.findItem(R.id.item2);
final MenuItem menuItem3 = mToolbarMenu.findItem(R.id.item3);
final MenuItem menuItem4 = mToolbarMenu.findItem(R.id.item4);
//Set visibilities
menuItem1.setVisible(true);
menuItem2.setVisible(true);
menuItem3.setVisible(false);
menuItem4.setVisible(false);
}
}
you can change visibilities any time in activity, but you have to inflate it first

Related

How can I implement onCreateOptionsMenu on my bottomNavigationView

Hi I wouldike to know when I click on my bottomnavigationView how to display my search toolbar ?
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_loope:
toolbar_menu.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:id="#+id/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Bottom_nav_view.xml
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_loope"
android:icon="#drawable/ic_loope"
android:title="#string/title_loope"
/>
<item
android:id="#+id/navigation_take_photo"
android:icon="#drawable/ic_take_photo"
android:title="#string/title_take_photo" />
<item
android:id="#+id/navigation_public"
android:icon="#drawable/ic_public"
android:title="#string/title_public" />
</menu>
I can't actually use getMenuInflater().inflate(R.menu.toolbar_menu, menu) I don't know how to implement it for doing the same result when i click on my loope (R.id.navigation_loope)
EDIT : Bottom_nav_view it's for my bottom bar and toolbar_menu for my topbar. If I click on the item navigation_loop, I wouldike to show the search bar on the top.

Showing an Icon in the Overflow Menu

I am trying to display action overflow menu with icons in my one application. I am not getting icons in menu. My Target SDK = 23 and Minimum SDK = 16.
My Menu.xml is like below
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_donate"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
<item android:id="#+id/menu_settings"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
<item android:id="#+id/menu_logout"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
</menu>
and Java code is like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
But it does not showing icons in menu. Let me know if anyone can help me to achieve this without changing menu structure.
Thanks
Add below line to your java class inside onCreateOptionsMenu(Menu menu):
menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
Try with below code :
#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_patient_home_screen, menu);
menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user)));
menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile)));
menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out)));
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
switch (item.getItemId()) {
case 1:
Toast.makeText(PatientHomeScreen.this, "Profile is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(PatientHomeScreen.this, "Add New User is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 3:
Toast.makeText(PatientHomeScreen.this, "Switch Profile is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 4:
Toast.makeText(PatientHomeScreen.this, "Sign Out is Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private CharSequence menuIconWithText(Drawable r, String title) {
r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());
SpannableString sb = new SpannableString(" " + title);
ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
Or, you can show icon with menu item by :
xml code :
<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_alarm"
android:icon="#drawable/ic_accept"
android:orderInCategory="100"
android:title="#string/menu_create_alarm"
android:showAsAction="always|withText"
app:showAsAction="always|withText" />
Java Code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_item, menu);
super.onCreateOptionsMenu(menu, inflater);
}
You can use the custom layout for menu item child
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_donate"
android:title="#string/facebook"
android:actionLayout="#layout/menu_donate_layout"
app:showAsAction="never" />
<item android:id="#+id/menu_settings"
android:title="#string/facebook"
android:actionLayout="#layout/menu_setting_layout"
app:showAsAction="never" />
<item android:id="#+id/menu_logout"
android:title="#string/facebook"
android:actionLayout="#layout/menu_logut_layout"
app:showAsAction="never" />
</menu>
Create menu_donate_layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/profile_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile" />
</LinearLayout>
Same create the separate layout of each menu item or change text and icon dynamically.

Target action bar item from navigation drawer

I am currently working on an app that i implemented a toolbar and a search functionality on the toolbar with a searchview to filter list views which is working fine.
I also have a navigation drawer whereby i also want one of its items to do exactly what the search functionality is doing on the toolbar. How do implement an intent to lead me to SearchViewon toolbar when searchitem is clicked from navigation drawer.
menu_main for toolbar
<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/viewMode"
android:orderInCategory="100"
app:showAsAction="never"
android:icon="#drawable/ic_view_compact_black_24dp"
android:title="#string/view" />
<item
// my search functionality
android:id="#+id/search"
android:orderInCategory="100"
app:actionViewClass= "android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_search"
android:title="#string/search" />
</menu>
activity_navdrawer
<?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/nav_notes"
android:icon="#drawable/ic_note_add_black_24dp"
android:title="#string/first_layout" />
<item
// search functionality which should lead me to searchview on toolbar
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/third_layout" />
</group>
<item android:title="#string/app_communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_share_black_24dp"
android:title="#string/app_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_email_black_24dp"
android:title="#string/app_send" />
</menu>
</item>
</menu>
from main activity
public boolean onNavigationItemSelected(MenuItem item) {
// Handling navigation view item clicks here.
int id = item.getItemId();
if(id == R.id.nav_notes) {
Intent noteIntent = new Intent(this, MainActivity.class);
startActivity(noteIntent);
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.search);
searchMenuItem.expandActionView();
// what should i implement here?
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public boolean onCreateOptionsMenu(`final` Menu menu) {
mOptionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
ArrayAdapter<Note> arrayAdapter = (ArrayAdapter<Note>) mListNotes.getAdapter();
arrayAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
Try the following code below:
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
mOptionsMenu = menu;
...
}
...
public boolean onNavigationItemSelected(MenuItem item) {
...
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.menu_search);
searchMenuItem.expandActionView();
}
...
Add <category android:name="android.intent.category.DEFAULT" /> to your search activity in manifest. Then do this:
else if(id == R.id.nav_search) {
startActivity(new Intent(Intent.ACTION_SEARCH));
}
If this works, courtesy - Start Activity Using Custom Action

Action items/icons not displaying on action bar in Android App

I am trying to have a search action on my action bar. I have set an icon and have showAsAction set to always but it is still always displayed in the overflow menu. How can I make it display on the action bar itself? Thanks!
Activity
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.
setTitle("Add");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings",
Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search",
Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML file
<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="com.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always"
android:title="#string/title_search"/>
</menu>
Either remove
android:actionViewClass="android.support.v7.widget.SearchView"
or delete your case statement for action_search in onOptionsItemSelected. Doing either of those will display the icon.
If you want to use SearchView the callback is handled differently than onOptionsItemSelected.
Documentation here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
Just change app:showAsAction instead of android:showAsAction
<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="com.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"`enter code here`
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
app:showAsAction="always"
android:title="#string/title_search"/>
</menu>

Toggling Icons In ActionBarActivity

I'm building an Android app using the AppCompat_v7 library and I'm having troubles with the action bar.
I have an AppBarActivity which extends ActionBarActivity - I'm using the same actionbar for all of my activities. MainActivity extends AppBarActivity. I've defined four actionbar icons, two of which I'd like to be displayed at a time. I've been careful to define my own namespace, ie. app:showAsAction="always"/>
In my app, I toggle the visibility of these icons in onPrepareOptionsMenu()
MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
contactOn.setVisible(!useContacts);
contactOn.setEnabled(!useContacts);
contactOff.setVisible(useContacts);
contactOff.setEnabled(useContacts);
Do I need to specify a namespace here somehow? Because it doesn't seem that these methods are doing anything, except, curiously, rearranging their names in the overflow list. Also, only three of the four buttons show up there, which I don't understand either but I expect that's a different problem.
UPDATE: posting code & xml
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inf = new MenuInflater(this);
inf.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
contactOn.setVisible(!useContacts);
contactOn.setEnabled(!useContacts);
contactOff.setVisible(useContacts);
contactOff.setEnabled(useContacts);
MenuItem locationOn = menu.findItem(R.id.location_toggle_button_on);
MenuItem locationOff = menu.findItem(R.id.location_toggle_button_off);
locationOn.setVisible(useLocation);
locationOn.setEnabled(useLocation);
locationOff.setVisible(!useLocation);
locationOff.setEnabled(!useLocation);
return true;
}
And the XML
<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="com.example.skortchm.strangerapp.MainActivity" >
<!-- Contacts Toggle, should appear as action button -->
<item
android:id="#+id/contact_toggle_button_on"
android:icon="#drawable/ic_action_call"
android:title="#string/contact_toggle_on"
app:showAsAction="always"/>
<item
android:id="#+id/contact_toggle_button_off"
android:icon="#drawable/ic_action_call_off"
android:title="#string/contact_toggle_off"
app:showAsAction="always"/>
<!-- Location Toggle, should appear as action button -->
<item
android:id="#+id/location_toggle_button_on"
android:icon="#drawable/ic_action_location_searching"
android:title="#string/location_toggle_on"
app:showAsAction="always"/>
<item
android:id="#+id/location_toggle_button_off"
android:icon="#drawable/ic_action_location_off"
android:title="#string/location_toggle_off"
app:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<!-- Add gear icon here -->
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
You need to use getMenuInflater to inflate the menu.
You are creating an instance for MenuInflater, that is not right . you have to use the instance associated with the Activity
Change your onCreateOptionsMenu as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

Categories

Resources