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);
}
}
Related
I want to create a search for the user in the ActionBar. I use the SearchView Widget as an Action View. But when I include the
part in the manifest, it also pops up a second search on top. Both the view on top and the ActionBar Search work. When I remove the part from the manifest, the second Search doesn't pop up and only the one in the ActionBar does, but nothing happens when I hit the search button.
This is how it looks:
I only want the second view not the white search on top. Perhaps somebody easily sees whats wrong.
This is my AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/BlueTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".helper.SearchResultsActivity" />
</activity>
<activity android:name=".helper.SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
The part of the Fragment(as part of MainActivity) which handles the search:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.persons, menu);
Log.d("ml Person", "creating Options Menu for Persons");
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_search:
getActivity().onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
widget in menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:orderInCategory="10"
android:title="#string/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint_persons" />
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);
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.
I am trying to make my app consist a SINGLE activity. This activity should be able to create a search and also receive a search. Unfortunately, I am getting a "double" search bar in my SearchView when I click on the search button in the action bar. I mean that there is a search bar (dark-- SearchView) that appears for a second in the action bar, and then a second one (white) appears OVER the action bar. Any help? What am I doing wrong?
Sorry, this search thing is all new and confusing to me.
MainActivity (the only activity):
public class MainActivity extends ActionBarActivity {
Menu mMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getSupportActionBar().setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_main);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
#SuppressLint("NewApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
#SuppressLint("NewApi")
#Override
public boolean onSearchRequested() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
MenuItem mi = mMenu.findItem(R.id.search);
if(mi.isActionViewExpanded()){
mi.collapseActionView();
} else{
mi.expandActionView();
}
} else{
//onOptionsItemSelected(mMenu.findItem(R.id.search));
}
return super.onSearchRequested();
}
}
main.xml (the menu xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.brianco.andypedia="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/search"
android:title="#string/action_settings"
android:icon="#drawable/ic_launcher"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
com.brianco.andypedia:showAsAction="always|collapseActionView"
com.brianco.andypedia:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
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"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<activity
android:name="com.brianco.andypedia.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
</activity>
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity" />
From Setting Up the Search Interface in the Android Documentation:
In your searchable activity, handle the ACTION_SEARCH intent by
checking for it in your onCreate() method.
Note: If your searchable activity launches in single top mode
(android:launchMode="singleTop"), also handle the ACTION_SEARCH intent
in the onNewIntent() method. In single top mode, only one instance of
your activity is created and subsequent calls to start your activity
do not create a new activity on the stack. This launch mode is useful
so users can perform searches from the same activity without creating
a new activity instance every time.
Please try to add the following attribute to your <activity> in your manifest file:
android:launchMode="singleTop"
This will make the same activity to receive the search intent.
More info here: http://developer.android.com/guide/topics/manifest/activity-element.html
Also, you have <intent-filter> declared twice, you should merge it into one element.
Okay, the problem had to do with calling onSearchRequested() in onOptionsItemSelected(MenuItem item). That is redundant when I have a SearchView and should only be called on older platforms.
So, I created a separate menu item for devices under Honeycomb. It is removed at runtime for newer devices. The SearchView is removed at runtime for older devices.
See updated code below:
#SuppressLint("NewApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//remove old
menu.removeItem(R.id.search_old);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
} else{
//remove new
menu.removeItem(R.id.search);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_old:
onSearchRequested();
return true;
default:
return false;
}
}
#SuppressLint("NewApi")
#Override
public boolean onSearchRequested() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
MenuItem mi = mMenu.findItem(R.id.search);
if(mi.isActionViewExpanded()){
mi.collapseActionView();
} else{
mi.expandActionView();
}
} else{
//onOptionsItemSelected(mMenu.findItem(R.id.search));
}
return super.onSearchRequested();
}
THANKS,
This has worked for me.
Manifest:
<activity
android:name=".Buscar"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_buscar"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Activity:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
query = intent.getStringExtra(SearchManager.QUERY);
mysearch(query);
}
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.