I have a TabGroup in my app. How can I have different actions items for each Tab ? I just found an example from here, but it is not enough for me.
Can someone provide some hints or at least a link ?
I'm using SlidingTabLayout, and this is the way i changed the Toolbar icons for each Tab.
I've 3 tabs, each tab got its own fragment, and in each fragment I've created the following:
Tab 1 fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.tab_1_menu, menu);
}
Tab 2 fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.tab_2_menu, menu);
}
And so on, hope this help .
Full Example with getItemId / Click
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Set The Menu View
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//Menu Items, every fragment menu item
//Must have different ID
if (id == R.id.settings) {
//Do Something here
);
return true;
}
if (id == R.id.About) {
//Do Something here
);
return true;
}
if ( id == R.id.exit)
{
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Related
I am building an application in which each fragments have different menus but the code I am using to add menu but the menu option doesn't actually exists.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.logoutitem) {
((tailorDashboard)getActivity()).logoutwork();
return true;
}
return super.onOptionsItemSelected(item);
}
sir basically i am using this code to make a option menu in fragments i have used android.support.v7.widget.toolbar option to make my custom toolbar. but the code i have already used is not actually making the option menu.
`#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.logoutitem) {
((tailorDashboard)getActivity()).logoutwork();
return true;
}
return super.onOptionsItemSelected(item);
}'
I am trying to hide the menu in the TableLayout with ViewPager I want the menu only in the solutions tab. I used the onPrepareOptionsmenu to hide the menu in tabs except the solutions tab. the thing is my onOptionsItemSelected is not working.
code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.simpleadd,menu);
// +getMenuInflater().inflate(R.menu.simpleadd, menu);
onPrepareOptionsMenu(menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (viewpager.getCurrentItem()==0){
menu.findItem(R.id.simpleadd).setVisible(false);
} else if(viewpager.getCurrentItem()==1){
menu.findItem(R.id.simpleadd).setVisible(false);
} else if(viewpager.getCurrentItem()==2){
menu.findItem(R.id.simpleadd).setVisible(true);
} else if(viewpager.getCurrentItem()==3){
menu.findItem(R.id.simpleadd).setVisible(false);
}else if(viewpager.getCurrentItem()==4){
menu.findItem(R.id.simpleadd).setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.simpleadd:
startActivity(new Intent(this,NewSolution.class));
}
return super.onOptionsItemSelected(item);
}
thanks in advance,
You have used the layout's name in the switch case of onOptionsItemSelected.
Use menu item's id instead.
First, you need to get item id and then set compare with layout ids,
this helps you to set the functionality. Please find the below sample
code for your reference.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.simpleadd) {
// execute your code here
}
return super.onOptionsItemSelected(item);
}
I have a option Menu in my toolbar in app.but I want to Is unseen Some places.
What solution do you recommend to it my friends?
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//codes...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item1) {
return true;
}
return super.onOptionsItemSelected(item);
}
TO:
Just remove onCreateOptionsMenu and onOptionsItemSelected method from activity.
try : Open menu xml using ctrl + click on R.menu.optionmenu in your code. Delete all "item" in xml, if you want to add menu items afterwards.
or
try : remove onCreateOptionsMenu(Menu menu) and onPrepareOptionsMenu(Menu menu) from Activity if you don't want to use action bar menus at all.
You can change this code and try again :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if("Your condition")
{
getMenuInflater().inflate(R.menu.optionmenu, menu);
}
return true;
}
I have this scenario:
I have a activity, lets call itAcitivty1 with
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
I open a fragment from Activity1 lets call it Fragment1 with:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.layout, container, false);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menu != null){
menu.clear();
}
if (!boolean) {
inflater.inflate(R.menu.menu1, menu);
} else {
inflater.inflate(R.menu.menu2, menu);
}
}
This fragment will be called again from activity as a new instance.
Based on the boolean in onCreateOptionsMenu() I'm deciding what menu should be loaded in the fragment so, during the second instance if I click on a menu item, I see the objects of first instance fragment.
I have no clue, why is this happening?
How is the workflow for displaying menu options...
if (menu != null){
menu.clear();
}
That piece of code might be the root cause.
You're telling the system to clear the menu if it's null. Well, FYI, the menu will never be null in the first place; it is supplied by the system. It might have no items inside, but it'll never be null.
One way to check if a menu already contains an item (or more) is to call hasVisibleItems().
From the documentations:
public abstract boolean hasVisibleItems()
Returns True if there is one or more item visible, else false.
Therefore, this is how you should do it:
if (menu.hasVisibleItems()){
menu.clear();
}
In a case like this, you need to put the menu in activity and update the menu dynamically in onPrepareOptionsMenu()
You need to inflate menu in onCreateOptionsMenu(..) of your Activity1 and need to make return true to display menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Your code here
getMenuInflater().inflate(R.menu.main1, menu);//My menu
return true;
}
After that you get menu in your fragment also.
Edits:
If you use single menu file and show / hide MenuItem of your menu. it will solve your problem.
Add all menu in single file.by default all menu items are visible false using android:visible="false"
See example code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem item1 = menu.getItem(0);
MenuItem item2 = menu.getItem(1);
MenuItem item3 = menu.getItem(2);
MenuItem item4 = menu.getItem(3);
if(!boolean){
//visible items which you want to show when boolean is false
item1.setVisible(true);
item2.setVisible(true);
}
else
{
//visible items which you want to show when boolean is true
item3.setVisible(true);
item4.setVisible(true);
}
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menuItem1) {
return true;
}
if (id == R.id.menuItem2) {
return true;
}
...
return super.onOptionsItemSelected(item);
}
I would like to rename menu on click of an item. I tried the following but it didn't change.
I tried item.setTitle("LandScape"); or item.setTitle("Portrait");
Declare the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Do something on click on the menu item:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
boolean result = true;
switch(item.getItemId())
{
case R.id.oscillation_mode:
{
if(getScreenOrientation() == 1)
{
Log.e("Orientation","ABC"+getScreenOrientation());
item.setTitle("LandScape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
item.setTitle("Portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
}
The text is the same. The default it is Portrait.
when you call setRequestedOrientation() your Activity is destroyed and recreated, so any changes you make to in memory objects will be dropped. You should make these changes when the menu is shown instead
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem oscillation = menu.findItem(R.id.oscillation_mode);
if (getScreenOrientation() == 1){
item.setTitle("LandScape");
}else{
item.setTitle("Portrait");
}
return true;
}