I have a problem.
I implemented a navigation drawer with a listView, and I want disable a item of this listview. I want that the user see the item disable, because it take a different color, and you can't clicked it.
The problem, only the activity knows when it be disabled.
an obvious example of a button:
Button but =...
but.setEnables(false);
and now the button change the colour, and you can't click it.
I want do the same for an element of listView.
I tryed this:
nameListView.getChildAt(position).setEnabled(false);
but this don't work.
What can i do?
Thanks.
How about using Adapter? Like this:
nameListView.getAdapter().getItem(position).setEnabled(false);
My solution:
In the class who represent the item of navigation drawer, I add a boolean attribute (enable)
public class NavDrawerItem {
....
private boolean enable;
.....
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
}
And in my adapter, override te method isEnable(position):
#Override
public boolean isEnabled(int position) {
return getItem(position).isEnable();
};
Now, in my activity, when the navigation drawer is open, execute this method:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
NavDrawerItem itemToDisable = (NavDrawerItem) mDrawerList.getAdapter().getItem(0);
itemToDisable.setEnable(centroComercial!=null); //Condition to disable item
return super.onPrepareOptionsMenu(menu);
}
Before this method, system "redrawer" navigation drawer, and now the item isn't enable.
The only thing that does not change color, for more usability.
Related
I'm using SearchView (android.support.v7.widget.SearchView) to filter items in my listview. I have a listview which includes items with a checkbox (the checkbox is a marker to select the item). When i click the checkbox, i call invalidateOptionsMenu(). Then there appears a new menu item which can do some things with the "selected" items. Here some Code:
SearchView in menu.xml:
<item
android:id="#+id/search_movie_icon"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/search_title"
hmkcode:actionViewClass="android.support.v7.widget.SearchView"
hmkcode:showAsAction="always|collapseActionView"/>
Fragment:
#Override
public void onCreateOptionsMenu(Menu p_Menu, MenuInflater p_Inflater) {
p_Menu.clear();
p_Inflater.inflate(R.menu.movie_actionbar, p_Menu);
m_SearchView = (SearchView) MenuItemCompat.getActionView(p_Menu.findItem(R.id.search_movie_icon));
m_SearchView.setIconified(false);
m_OnQueryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String p_NewText) {
m_MovieManager.setFilter(p_NewText);
// this call will get the new movie list from the moviemanager and update the view
updateViews();
return false;
}
#Override
public boolean onQueryTextSubmit(String p_Submit) {
// do nothing -> this will be called on pressing enter
return false;
}
};
m_SearchView.setOnQueryTextListener(m_OnQueryTextListener);
//some more unimportant stuff
}
Checkbox in listadapter:
l_ViewHolder.m_cbMarked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p_View) {
l_Movie.setMarked(((CheckBox) p_View).isChecked());
// This should update the optionsmenu to see the new menu item
((Activity) m_Context).invalidateOptionsMenu();
}
}
My problem is that the call invalidateOptionsMenu() removes the string in my searchview.
I know that there were some changes in SearchView:
Before:
#Override
public void onActionViewCollapsed() {
clearFocus();
updateViewsVisibility(true);
mQueryTextView.setImeOptions(mCollapsedImeOptions);
mExpandedInActionView = false;
}
Now:
#Override
public void onActionViewCollapsed() {
setQuery("", false);
clearFocus();
updateViewsVisibility(true);
mQueryTextView.setImeOptions(mCollapsedImeOptions);
mExpandedInActionView = false;
}
I tried some workarounds:
StateFulSearchView from SearchView in ActionBar -- problems with the *Up* button
Here i have some focus and icon problems...
A callback which removes my OnChangeTextListener before calling invalidateOptionsMenu() and readd it after this call (to avoid the filter actions) -> it didn't work...
Safe the filter before calling invalidateOptionsMenu() and set the old filter after that call -> then i can clearly see the flicker effect in my listview :(
I can't find a good solution...
Can someone help me? Let me know if you have questions.
I have this code
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openSearch();
return true;
}
});
into onCreate( ) of my Activity.
OpenSearch function call opens up google now like search view. This only happens when the user clicks on the search action item in the toolbar. In my case I want the search view to open up automatically when the activity starts. How can this menu item click be done programmatically.
I can't call openSearch directly because it needs the menu to be created.
Is there there any callbacks informing that action menus have been created ?
You can try something like this :
tollbarLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
tollbarLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
tollbar.performclick();
}
}
});
In my app I set the toolbar and status bar as shared objects as suggested in option #2 in this post
The general behavior and outline of the toolbar and tabs are excellent - the only issue is that when I move to activity B, some of the UI elements of the Toolbar are not participating in the content transition, particularly the Toolbar title and the menu icons.
I tried adding a SharedElementCallback and in it to loop over the children of the toolbar and tabs and add them all to a Fade transition - but it didn't affect the behaviour of the toolbar and tab content.
Any idea how to proceed from here? The desired effect is to have the elements of the Toolbar (title, up button, menu icons) participate in the content transition.
Added screenshots after comment:
Activity A
Activity B
Each activity has your own menu, so you have to create the menu for each one, even if they are the same.
However, if you prefer, you can create just one menu and create a custom class for manipulate the menu; Then you call this custom class on onCreateOptionsMenu and onOptionsItemSelected from whatever activity.
The following code is an example.
Custom class:
public class MenuActionBar {
public static void createOptionsMenu(final Activity activity, Menu menu) {
activity.getMenuInflater().inflate(R.menu.yourmenu, menu);
// Do whatever you wanna do
}
public static boolean optionsItemSelected(Activity activity, MenuItem item) {
// Do whatever you wanna do
}
}
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuActionBar.createOptionsMenu(this, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuActionBar.optionsItemSelected(this, item)
? true : super.onOptionsItemSelected(item);
}
I have an Activity that extends ActionBarActivity taken from the ActionBarCompat code sample and I'm trying to show/hide menu items (actions) at runtime.
I've tried using setVisible() on the MenuItem and works for ICS, but in pre-ICS it only change visibility of menu items (menu button press) whereas the ActionBar doesn't get notified of menu changes.
Any solution? Thanks in advance!
I created multiple alternatives of the action bar items under /res/menu/. I keep a member to indicate which one I am using right now. to replace the menu, I call:
protected void setMenuResource(int newMenuResourceId)
{
_menuResource = newMenuResourceId;
invalidateOptionsMenu();
}
And I override onCreateOptionsMenu() to:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
if (_menuResource != 0)
{
getSupportMenuInflater().inflate(_menuResource, menu);
return true;
}
return super.onCreateOptionsMenu(menu);
}
Now, if I want to change the action Items, I call:
setMenuResource(R.menu.actionbar_menu_X);
This is how i solved it:
In ActionBarHelperBase.java of actionbarcompat project
...
private View addActionItemCompatFromMenuItem(final MenuItem item) {
final int itemId = item.getItemId();
....
The creator of this class copy properties of object, but didn't copy the id of item, so it is impossible to find it later with fiven id.
So i added it in that method:
...
actionButton.setId(itemId);
...
and in the same class i just use:
#Override
public void hideMenuItemById(int id, boolean show){
getActionBarCompat().findViewById(id).setVisibility(show? View.VISIBLE: View.GONE);
}
Hope it helps You.
You have to call supportInvalidateOptionsMenu() which is the relevant method for an ActionBarActivity:
http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportInvalidateOptionsMenu()
I'm developing for android 3+
In my action bar i have a drop-down list(see how to hide/unhide the actionbar list on android 3? for the dropdown i intend). The problem is i need to do a certain action when the user selects something, but Android calls onNavigationItemSelected() as soons as it draws the view, so no selection actually happened.
How can i detect if the user actually pressed something and it is not a fake call from android ?
public class ListDittaListener implements OnNavigationListener{
private BaseActivity activity;
private ListDittaListener()
{
}
public ListDittaListener(BaseActivity activity)
{
this.activity = activity;
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId)
{
MyApp appState = ((MyApp)this.activity.getApplicationContext());
appState.setDittaSelezionata( (int) itemId);
SharedPreferences settings = this.activity.getSharedPreferences(MyApp.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("ditta_id_selezionata", (int) itemId);
////////restart activity this.activity.recreate();
return false;
}
}
You can easily just ignore the first call to onNavigationItemSelected if you like:
public class Whatever implements OnNavigationListener {
private boolean synthetic = true;
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (synthetic) {
synthetic = false;
return true;
}
// do whatever you really wanted here
}
}
Method onNavigationItemSelected(int itemPosition, long itemId) will be called anyway by the action bar.
What you may want to do is to tell action bar what itemPosition it should pass to the method on the first call. (In other words, to tell action bar what navigation item should be set after activity is created). Here is the code:
mActionBarMenuSpinnerAdapter = ...;
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mActionBarMenuSpinnerAdapter, this);
mActionBar.setSelectedNavigationItem(###your_default_navigation_item_here###);
After doing this you can solve your problem by applying changes in the onNavigationItemSelected(int itemPosition, long itemId) if only itemPosition is different.
The android system will call onNavigationItemSelected(0, 0) after the activity is setup. (Which means later than onResume()).
As other guys mentioned, you'd better not do any hack like ignore first call, otherwise the android system won't call onNavigationItemSelected() again when you select the first index. (The system thought the first item is already selected)
My solution is call actionbar.setSelectedNavigationItem(the real item# you want) after you setup the actionbar. Then the system will call onNavigationItemSelected() twice. First onNavigationItemSelected(0, 0) and then the onNavigationItemSelected(the real item#).
Well I cannot see anything wrong in your current code.
How did you create your dropdown elements. And what element is "select" by Android after the view is created. And what are your doing in your onCreate method where the ActionBar is initialized.
I did it as instructed here and it worked for me:
http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
I have viewpager with fragments and I need set custom action bar for every fragment in pager
In desired page I have navigation list, fragment fires onNavigationItemSelected automatically when I swipe pages, want to avoid this behavior and run tasks only if I selected nav item manually.
public class MyFragment extends Fragment implements ActionBar.OnNavigationListener {
private boolead fireReady = false;
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// every time make it false, this method invoked on swipe action
fireReady = false;
if (isVisibleToUser) {
// setup actionbar, you also can setup action bar in activity
String[] array = getActivity().getResources().getStringArray(R.array.users_order);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActivity().getActionBar().setListNavigationCallbacks(adapter, this);
}
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (fireReady) {
// task fire only when you directly press navigation item
UsersTask task = new UsersTask(getActivity());
task.setTaskListener(this);
task.execute(usersUrls[itemPosition]);
} else {
// make it true first time when page displayed
fireReady = true;
}
return false;
}
}
}