None of the action buttons are showing up on action bar - android

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.

Related

can't click and move activity with the menu

I make a menu and only have one item with an icon, I intend to make the menu when I click move to another activity.
but with the code like below I can't even move to another activity maybe also can't click it, because there is no animation effect click on the menu,
I use minimum fire level 17 and run this in the PIE room, maybe fire level 28
Main activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.tambah) {
Intent intent = new Intent(this, InsertAndViewActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Menu.xml from res > menu > menu.xml
<?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/tambah"
android:icon="#android:drawable/ic_menu_add"
android:orderInCategory="300"
app:showAsAction="ifRoom"
android:title="tambah" />
</menu>
in this section I have made activity 3 namely (MainActivity.java, SpalashScreen.java and InsertAndViewActivity.java) as well as the layout for that activity, and I changed AndroidManifest.xml to the following
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".InsertAndViewActivity"></activity>
<activity
android:name=".SplashScreen"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" ></activity>
</application>
I intend to make the menu when I click move to another activity.
I hope this will work for you
Write your menu method's like this,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.tambah:
Intent intent = new Intent(MainActivity.this, InsertAndViewActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How to display menu options in 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);
}

How to open a new activity to show results from a SearchView?

I've been working in an application that uses a SearchView Widget as an ActionView in the ActionBar.
The problem occurs when I type a search and hit the search button, it opens the same activity, what I want to do is to open a new Activity and show the results on a ListView, how can this be fixed?
This is my AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andres.hotelsoria" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher_hotel"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".SearchableActivity"
android:label="#string/title_activity_searchable" >
</activity>
</application>
You can start a new activity by attaching a OnQueryTextListener to the SearchView.
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(getApplicationContext(), SearchableActivity.Class);
startActivity(intent);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
read this post
http://developer.android.com/guide/topics/search/index.html
as mentioned in this document follow these steps
1)Create a folder in res->xml->searchable.xml paste content as mention in documentation
2)Go to AndroidManifest.xml and change Activity(where you want to deliver result for search) to this
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
3)Declare the SearchView in menu.xml file as
<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="your activity context" >
<item
android:id="#+id/mi_search"
android:title="#string/search"
android:orderInCategory="2"
android:icon="#drawable/searchicon"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
4)in onCreateOptionsMenu(Menu menu) do this code
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget;
5)in onOptionsItemSelected.
->fetch Id of search view
->simple paste onSearchRequested() inside block
public boolean onSearchRequested() {
return super.onSearchRequested();
}
6)register searchView with onQueryTextListener and do what you want to do
http://developer.android.com/reference/android/widget/SearchView.OnQueryTextListener.html
//You hav to start the new activity like this
SearchView.OnQueryTextListener textListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//use intent here to start new activity and pass "query" string.
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
};
searchView.setOnQueryTextListener(textListener);

Android options menu not displaying at all

Alright, Here's my AndroidMenuActivity.java file.
package com.example.caliexpeditionapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class AndroidMenuActivity extends Activity {
private WebView browser;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option, menu);
return true;
}
public boolean onContextItemSelected(MenuItem item)
{
onOptionsItemSelected(item);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.menu_refresh:
browser = (WebView) findViewById(R.id.webkit);
browser.reload();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
and my option.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_refresh"
android:icon="#drawable/ic_menu_refresh"
android:title="#string/refresh" />
</menu>
and finally my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.caliexpeditionapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity android:name="com.example.caliexpeditionapp.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="com.example.caliexpeditionapp.AndroidMenuActivity">
</activity>
</application>
</manifest>
Loads up no problem. Press the menu button... nothing. no error, just nothing.
I know this is most likely a duplicate question but I didn't know what else to search for.
You said that your XML menu file is options.xml -
So you need to change inflater.inflate(R.menu.option, menu); to binflater.inflate(R.menu.options, menu);
Add onMenuItemSelected().
Example:
/**
* Hook called whenever an item in the options menu is selected.
* #param item The menu item that was selected.
* #return false to allow normal menu processing to proceed, true to consume it here.
*/
#Override
public boolean onMenuItemSelected (int featureId, MenuItem item) {
final int itemId = item.getItemId();
Toast.makeText(this, "Menu item clicked, index: " + itemId, Toast.LENGTH_SHORT).show();
return super.onMenuItemSelected(featureId, item);
}

SearchWidget does nothing

Even after reading the samples and a few questions on SO, I still dont figure out why my searchwidget does nothing ... !
Manifest.xml simplified:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".ResultActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
</activity>
xml/searchable.xml :
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
menu/activity_main.xml (searchwidget in action bar..) :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
MainActivity where the search is supposed to happen:
public class MainActivity extends Activity implements OnClickListener {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchView.setSubmitButtonEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
// app icon in action bar clicked; go parameters
Intent intent = new Intent(this, ParametersActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
case R.id.menu_search:
onSearchRequested();
return true;
}
return true;
}
}
and Resultactivity :
public class ResultActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
Toast.makeText(this, "QUERY : " + query, Toast.LENGTH_SHORT).show();
}
}
I've followed API guide, and I dont see where is my mistake, if anyone can help, I'd appreciate !!
Thanks.
Nico.

Categories

Resources