How to display menu options in android? - android

I am having trouble displaying my menu options on my phone. I got all the code right I think but it is not displaying. Any help would be greatly appreciated.
Here is how my manifest looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.drumloopsequencer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:screenOrientation="landscape"
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the onCreateOptionsMenu function:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
and here is the menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/file"
android:title="#string/fileTab"
android:orderInCategory="1"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/packs"
android:title="#string/packsTab"
android:orderInCategory="2"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/drumRack"
android:title="#string/drumRackTab"
android:orderInCategory="3"
android:showAsAction="ifRoom"
/>
</menu>

If you are using a fragment make sure that you have enabled the options menu with setHasOptionsMenu(true). A good place to call this might be from onCreate.
public class MyFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
}
or for an Activity
public class MyActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
}

Try this :
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow()
openOptionsMenu();
};

If you are using fragment then you have to do this:
//inside constructor
public MyClassFrag(){
setHasOptionsMenu(true);
}
or inside onCreate()
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

Related

Unable to see menu in action bar Android

i have problem putting the menu in the upper right corner. even i checked this Link. but not worked.
i have tried with android:showAsAction="always"even tried with more then 6 SDK version but not worked .
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
items.xml:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
android:showAsAction="always"
/>
<item
android:id="#+id/computer"
android:title="#string/computer"
android:icon="#drawable/computer"
/></menu>
Java:-
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.items, menu);
//inflater.inflate(R.menu.items, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
.
please suggest . any suggestion are welcome . Thanks
Solution as below-
In build.gradle(Module:app) add appcompat dependency
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
// ... other dependencies
}
change
public class MainActivity extends Activity
to
public class MainActivity extends AppCompatActivity.
change your menu.xml file as 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" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
app:showAsAction="always" />
<item
android:id="#+id/computer"
android:title="#string/computer"
android:icon="#drawable/computer" />
</menu>

Action on ActionBar doesn't show

Yesterday I had a problem with replacing fragments, so I had to change my MainActivity to extend Acivity instead of ActionBarActivity in order to avoid compatibility issues. The problem is, that after I've done that, an actionbar item disappeared. I tried following androids official tutorials on how to set it up without using the support library, but with no avail. This is what I have:
Inside the MainActivity:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().add(R.id.frag_container, new MainMenuFragment()).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;};
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As you can see, I'm implementing the needed methods for the action bar to be populated with buttons. This is my menu's 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.pszczyna.MainActivity" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
The manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jam.pszczyna"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jam.pszczyna.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
which is the minimum SDK version required for this to work. For me everything looks like it's supposed to, whats wrong with it?
try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.pszczyna.MainActivity" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
android:showAsAction="always"/>
</menu>
and in your manifest remove android:theme="#style/Theme.AppCompat" and put your own style.

None of the action buttons are showing up on action bar

I am following the guild on developer.android.com and have made it to the part to adding action button to the bar. None of them, however, are wanting to show up. I have looked through the code, did research, and even redid the whole project just the make sure I was following everything right. Nothing I do seems to work so I thought I would put it here and let a fresh (and more experience) set of eyes look at it and let me where I am going wrong.
main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myfirstapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
myfirstapp:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
myfirstapp:showAsAction="never" />
</menu>
DisplayMessageActivity.java
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
setContentView(R.layout.activity_main);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
//openSearch();
return true;
case R.id.action_settings:
//openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
screenshot
Just remove the setContentView(R.layout.activity_main);inside
#Override
public boolean onCreateOptionsMenu(Menu menu) {
setContentView(R.layout.activity_main);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
and also add return true instead of return super.onCreateOptionsMenu(menu);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;
}
try this. Its working for me.

why does the search icon not appear on action bar

i know this has been asked many times. i've gone through most of the questions here regarding the above problem. but they didn't really help. i can't find the problem in my code. so here is my code
package com.example.actionbaractivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
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 items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
now manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionbaractivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and now the menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always|withText"/>
</menu>
and finally the string xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActionbarActivity</string>
<string name="hello_world">Hello world!</string>
<string name="action_search"></string>
</resources>
Do in this way:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
</menu>
then in your Activity class:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(null!=searchManager )
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
searchView.setIconifiedByDefault(false);
return true;
}
Read more about how to add Search functionality to android App

menu is not coming on top right side corner

In virtual mobile menu is displaying.But it is not displaying in my personal mobile.I have kept mainactivity.java ,main.xml,androidmainfest.xml files here.Any answers please...
//mainactivity.java
public class MainActivity extends Activity{
String username,password; ResultSet rs =null;
boolean temcfag=false;
static boolean temqfag=true;
public static String tag="Lifecycle activity";
EditText user,pass;
AlertDialog.Builder dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog=new AlertDialog.Builder(this);
dialog.setNeutralButton("OK", null);
user=(EditText)findViewById(R.id.editText1);
pass=(EditText)findViewById(R.id.editText2);
final Button click=(Button)findViewById(R.id.button2);
click.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent launchactivity= new Intent(MainActivity.this,com.example.desisquarea.LoginActivity.class);
startActivity(launchactivity);
}
});
}
#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 true;
}
}
//menu/main.xml
<?xml version="1.0" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/login" android:title="SignUP" />
<item android:id="#+id/sign" android:title="Login" />
<item android:id="#+id/home" android:title="Home" />
</menu>
//androidmainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.desisquarea"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.desisquarea.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LayoutParams lp = dialog.getWindow().getAttributes();
lp.gravity = Gravity.TOP | Gravity.RIGHT;
myDialog.getWindow().setAttributes(lp);
dialog.show();

Categories

Resources