My onCreateOptionsMenu works only in my MainActivity and when I try to put another onCreateOptionsMenu in another activity to inflate a different menu it does not display my menu bar (note that I have it setup exactly the same in both activities).
I do not get any errors, it just does not display my menu bar on my secondary activity.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.expanded_view_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//noinspection SimplifiableIfStatement
int id = item.getItemId();
if (id == R.id.action_back) {
finish();
}
return super.onOptionsItemSelected(item);
}
Also note that I changed getMenuInflater() to super.getMenuInflater() and got same result.
In case you are using tool bar in your activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
}
try adding method of super in your callback onCreateOptionsMenu implementation.
return super.onCreateOptionsMenu(menu);
The problem in my case is if extend Activity, then onCreateOptionsMenu is not triggered. Did it manually with in onCreate:
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.menu_main);
Of course, toolbar is defined in CoordinatorLayout
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
don't forget to call parent function like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
....
}
you should building a new xml menu (Second.xml).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);//Second es your new xml.
return true;
}
I was having the same problem: - I found the solution that worked for me.
1>Check to what you extend class: - In my case, I have tried my activity with extending AppCompatActivity class and the menu is showing proper and when I changed it to activity class then it stopped displaying.
Not Working: -
public class GridViewActivity extends Activity
2>Try to extend your class with AppCompatActivity class instead of activity class.
Working: -
public class GridViewActivity extends AppCompatActivity
Call setHasOptionsMenu(true) from onCreate.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Related
Why I create toolbar I do not have three dots who will appear. How can I do ?
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
And XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="woowin.woowin.Main">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#393939"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
And can you explain me, how add options in this three dots after that ?
thank you
you forgot to add your menu in your activity use below code to add menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}
You need to add at least one menu item either via XML or in java code using add into your menu to see the menu options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,"title");
return true;
}
I want to set toolbar on my activity which extends FragmentActivity. I know that for use setSuppoertActionBar(toolbar) method we extends AppCompatActivity instead of FragmentActivity but I override the onMenuItemSelected(int featureId, MenuItem item) method which is final in AppCompatActivity and final method cannot override. so I'm restricted to extends FragmentActivity.
Here is my code:
public class MainActivity extends FragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar); -> error is here
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()){
case R.id.action_search:
onSearchRequested();
break;
}
return super.onMenuItemSelected(featureId, item);
}
I saw many answers related to that question but everyone says extends AppCompatActivity instead of FragmentActivity but I want to set toolbar as well as override onMenuItemSelected(int featureId, MenuItem item) method.
what should I do, please help.
This thing is good when you are using NavigationDrawer
use this:-
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
then set Toolbar Title according to different fragment with different Titles in onNavigationItemSelected :-
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
sfm = getSupportFragmentManager();
Fragment fragment = new Fragment();
int id = item.getItemId();
if (id == R.id.nav_id) {
fragment = new YourFragment();
toolbar.setTitle("SET TOOLBAR NAME");
}else if (id == R.id.nav_id2) {
fragment = new YourFragment();
toolbar.setTitle("SET TOOLBAR NAME");
}
For single fragment, first customize your style.xml like this :-
<style name="YourStyleName" parent="Theme.AppCompat.Light.DarkActionBar">
// ToDo as you want to do or as per your requirement
</style>
then apply into your custom toolbar:-
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/YourStyleName" >
// ...........
</android.support.v7.widget.Toolbar>
You can also create your own toolbar:
First set up the main theme to extend Theme.AppCompat.Light.NoActionBar
<style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Remember to apply the theme:
#Override
protected void onCreate(Bundle savedInstanceState) {
this.setTheme(R.style.style_1);
// ...
}
then in your Activity's xml you can set your own custom toolbar:
<include layout="#layout/my_toolbar"/>
where #layout/my_toolbar may look like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
app:contentInsetStart="0dp"
app:layout_collapseParallaxMultiplier="1.0">
<!-- insert your views here -->
</android.support.v7.widget.Toolbar>
First set style as
<style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
then
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Home");
You should extend your Activity from AppCompatActivity as this includes support for Fragments and the Toolbar.
And then override the
onCreateOptionsMenu() and onOptionsItemSelected() methods
You shouldn't be overriding
onMenuItemSelected()
public class MainActivity extends AppCompatActivity { ...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FragmentTransaction ft = getSupportFragmentManager.beginTransaction();
....
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
I already have made a class that extends PreferenceFragment to display my apps preferences. It works, but I decided I should make the fragment appear in a new activity so that the user can back out of it. Right now, it just gets swapped into the main fragment and displayed. How can I simply make an activity that displays the entire PreferenceFragment? There is no layout defined for the Fragment since it's a PreferenceFragment.
Make a new activity that contains one fragment. Insert the preferences fragment in it. Here is a copy of what I use in my project.
public class SettingsActivity extends Activity{
public final static String SETTINGS_NATIVE_IGNORE = "pref_native_ignore";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
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();
switch(id)
{
case android.R.id.home:
onBackPressed();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
then the xml for it
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:baselineAligned="false"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="...SettingsActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="#+id/preferences_fragment"
android:name="...fragment.PreferencesFragment"
tools:layout="#layout/fragment_preferences"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And finally the preferences fragment
public class PreferencesFragment extends PreferenceFragment {
public PreferencesFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
I aim to shared the action bar in all activities. So I create a based class call "MyActionBarActivity" as follows"
public class MyActionBarActivity extends ActionBarActivity {
public final static int TL = Toast.LENGTH_LONG;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "I am settings", TL).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
and other activities inherit from this class to share the action bar.
public class Activity1 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}}
public class Activity2 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}}
Is this a good way to implement the action bar?
In my opinion this is a fine way to apply the common action bars in more than one activity.
But inflating in separate activities also won't make much difference.
You can use either ways.
I am not getting how to show only selected action items on some activities. Below is my code:
public abstract class BaseActivity extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 0:
Intent loginIntent = new Intent(BaseActivity.this,LoginForm.class);
startActivity(loginIntent);
return true;
case 1:
Intent saveIntent = new Intent(BaseActivity.this,SaveForm.class);
startActivity(saveIntent);
return true;
}
return false;
}
}
In the above code I am setting action items so that I can reuse this action items in some activities without repeating the code. Whenever I want these action items to be displayed on particular activity, I am extending this BaseActivity on the present activity as below.
public class FirstActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
....
....
}
So, whenever I need these action items on the particular activities, I am extending the baseactivity to the activities on which I need. Now my problem is how to set only selected action items on particular activity. Suppose I don't want to show up "login" action Item on one activity. How can I get this done? Code snippets would be appreciated.
Now it's a reasonable approach but I would only have items that were reused for all in the baseactivity. That said I think the correct way of handling this would be to override the methods with the implementation you want in these other activities.
You could have some variables that is set in onCreate(), if these are true/false handle it accordingly in onCreateOptionsMenu()
Edit
To override in your own activity you simply do the same thing as in the baseactivity but without calling initializing those buttons you don't need.
If you instead want to make toggle if buttons should be there or not simply add the variables to your baseactivity:
protected boolean mIsLoginButton = true;
Change the onCreateOptionsMenu() method in baseactivity to something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(mIsLoginButton) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Then in your firstActivity you simply do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIsLoginButton = false;
}