I've used a library downloaded from this link to create a Circular Menu using Circular Layout in an application.
https://android-arsenal.com/details/1/1454
The menu contains 6 items. I create the menu in onCreate and I want to create that menu again in onResume as I want to update some of the menu items.
I want to create a Circular Menu with 6 items. As I have called the menu create function in both, number of menu items have got doubled, which is 12.
Below is a code excerpt which uses Circular Menu.
String[] mItemTexts = new String[] { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6" };
int[] mItemImgs = new int[] { R.drawable.im1,
R.drawable.im2, R.drawable.im3, R.drawable.im4, R.drawable.im5, R.drawable.im1};
mCircleMenuLayout = (CircleMenuLayout) findViewById(R.id.id_menulayout);
mCircleMenuLayout.setMenuItemIconsAndTexts(mItemImgs, mItemTexts);
What can I do to avoid getting the Circular Menu doubled?
Any suggestions or workarounds are much appreciated.
Thank you.
PS:
EDIT
I am using creation of the Circular Menu in onResume in the Activity. I don't finish the Activity with Circular Menu when starting another Activity, thus when I return from the newly started Activity again I find the Circular Menu has got doubled. That's the issue.
Simply call your method only in OnResume and remove form onCreate
as onResume is called after the onCreate
see this link
Related
In my application I created a Navigation Drawer which has 5 items, once an item is clicked in the Navigation Drawer, I can correctly check the item and show the new fragment using the fragment manager.
But currently I have three problems:
My application has floating action buttons, that of course perform actions and open new fragments, so I need to handle that "Fragment changing" in the navigation drawer too, setting as checked the current item which corresponds to the fragment showed.
I SORT OF solved this problem, but using a single method that
performs the fragment transaction in the MainActivity and meanwhile
checks the new item. So everytime a Fragment want to call another
Fragment, it uses this function passing the nav_drawer_item_id as a
parameter. I think that this solutions sucks, so if you have a better idea, it would be really welcome!
My biggest problem is that when the user press the Android Back Button, the fragment pops back to previous one (or sometimes I call popback() programmatically because I am done with that specific fragment )
I really don't know how to solve this problem because I can't check the correct item in the navigation drawer once a fragment is popped back, I need to know which fragment it is.
I need to show the new fragment once the navigation drawer is closed in order to respect "Android Rules" ( And because it's nice ).
I sort of solved this problem too, by setting a "nextFragment" inside the navigation drawer onNavigationItemSelectedListener and by showing the new fragment inside the drawer onDrawerClosed. Is it the only solution?
I'm not posting any code because it really doesn't deserve to be seen, so here is some extra info:
I have a single activity which contains the Navigation Drawer and currently handles the fragment transactions.
I have 6 fragments, 5 called from the Navigation Drawer and 1 called only from others UI components, but this one doesn't need to be checked on the drawer of course.
I tought about a solution and it was like "Setting the checked item FROM the fragment that is showed", but I didn't find any way to get the Navigation Drawer from the Fragment in order to set the proper item checked.
Implement FragmentManager.OnBackStackChangedListener in your activity to get notified whenever something is popped off the back stack and sync the navigation drawer from the callback. That should get rid of the problem caused when the back button is pressed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.....
getSupportFragmentManager().addOnBackStackChangedListener(this);
}
#Override
public void onBackStackChanged() {
// Get the currently active fragment
Fragment fragment =
getSupportFragmentManager().findFragmentById(R.id.fragments_frame);
// Check which fragment is active, check the
// correct selection and set the title accordingly
if (fragment instanceof FragmentAtPosition0) {
setCheckedAndTitle(0);
} else if (fragment instanceof FragmentAtPosition1) {
setCheckedAndTitle(1);
}
}
private void setCheckedAndTitle(int position) {
MenuItem item = navView.getMenu().getItem(position);
item.setChecked(true);
setTitle(item.getTitle());
}
As for the other two cases that you mentioned, I think the solutions you have mentioned are the preferred ways.
I have created with Android Studio 1.4 a Blank Activity where I can set menu's items to diplay.
My problem is that when I create a new activity "MainActivity2" I see the Action Bar with its colours but I'm not able to "connect" a menu with its elements. By default "onOptionsItemSelected" and "onCreateOptionsMenu" are not created automatically in this new activity.
Thanks
First activity,
MainActivity2
I found the solution. I saw some tutorials in Internet saying that to change activity I have to use "setContentView(R.layout.activity_main2)". By the way using this the application will override only the design and will never be executed the "onCreateOptionsMenu" where menu items are going to be added. The correct action to execute on the button listener is " startActivity(new Intent(MainActivity.this, Main2Activity.class)) ", so that the code can be executed.
I am trying to hide and show a menu item in my action bar.
when i click on the menu item, i run a function called showAddFrag(). Inside that function I am trying to hide the menu item. so my code is something like this
public void showAddFrag(){
Menu menu = new Menu (); // giving me this error: "Cannot instantiate the type Menu"
menu.findItem(R.id.add_item).setVisible(false); //hiding the add_item menu item
....
}
I know I am doing it wrong, but can someone point me in the right direction. Is there a function that I can reference to the context menu object?
Thanks in advance
You can use this callback: onPrepareOptionsMenu() and this method: menu.findItem(..) to save your button as an instance variable
Then set an onClickListener for your button that will disable it once clicked. And enable it back when you have to.
I hope this is what you're trying to do
I've got an optionsmenu looking like this right now:
Lets say that if I click item 1, i want two new items added to the menu looking like this:
I'm having problems doing this at runtime(while it's open) since onCreateOptionsMenu is only called once and onPrepareOptionsMenu seems only to be called when the menubutton of the phone is clicked. I just want it to refresh with these new items added.
Thanks for any help!
When you select an option item it causes the system to close the options menu. This happens after onOptionsItemSelected() runs. I'm guessing what you need to have happen is for that entire process to complete then have the options menu programmatically opened again so onPrepareOptionsMenu() is called.
Try this:
In onOptionsItemSelected(), when the user selects your "add two more options" option, set a flag in your activity but don't do anything else.
Override onOptionsMenuClosed(). Check if the flag is set. If it is, then post to be executed by the UI thread on its next pass. It should do nothing but open the options menu again, e.g.
runOnUiThread(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
});
Override onPrepareOptionsMenu(). Check if the flag is set. If it is, then add your two additional menu items and un-set the flag. You'll need to do some additional work to prevent the "add more items" menu item from continuing to add new items every time its pressed, unless that's the behavior you're looking for.
I just tried the current Google sample for ExpandableListiew:
This sample seem very simple and easy to use, but what I would like to do is to say that one of the category has no child. I removed all the children but the problem is that the arrow still appears on this current line.
For instance, imagine that I remove all "Cat Names", the arrow is still there and when I click on it, the arrow just change. How to remove this arrow and launch an activity instead?
If you're talking about the arrow that is used to collapse/expand the list then you can remove it by using setGroupIndicator()
in the activity you can call
getExpandableListView().setGroupIndicator(null);
that will remove the arrow permanently though. If you want to only hide it if the list is empty you can do it through xml attributes like this
To launch an activity when the list is expanded/collapsed you can override onGroupExpanded (or collapsed) in your ListAdapter implementation and used that to fire up your activity
In general, if you want a specific area of your activity to be refreshed when you come back from another activity; you got to write in the onResume() method.
Steps:
1 – In the Activity you want it to be refreshed, override the onResume() method:
#Override
protected void onResume() {
super.onResume();
// TODO: PUT YOUR REFRESH CODE IN HERE
}
2- You will need to refresh your Expandable Adapter.
I suggest you take the same code you used to instantiate your expandable list and put it again, like follows:
SimpleExpandableListAdapter expandableListAdapter =
new SimpleExpandableListAdapter(
this,
YourPrentsList, // List of your parent
R.layout.parents_layout, // Layout for the Parents
new String[] { "groupKey" }, // Key in the Group Data maps to display
new int[] { 1 },
childrenList, // List of the children
R.layout.childs_layout, // Layout of the children
new String[] { "keys"},
new int[] { 1}
);
setListAdapter( expandableListAdapter );
I hope this solves your concern...
Thanks,
Mohamed.