I am trying to add action button , but they do not showing in action bar, they are display at button when click on hardware menu button . And the up button is also not working .
i am sharing my code , tell me where i am wrong
this is my manifest file :
<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"
>
<!-- Parent activity meta-data to support API level 7+ -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".secindActivity"
android:label="Items"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
I am adding the up button in second activity ,. it is showing but not working .
And also trying to add action bar buttons in secondActivity . Menu file is as below :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:Digicare="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item_back"
android:icon="#drawable/home2"
Digicare:showAsAction="ifRoom"
android:title="Home">
</item>
</menu>
this is my java file of secondActivity :
it never comes in the if statement .
i put log in it , so i get it that it never comes in if statement
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if ( item.getItemId() == R.id.home){
Log.w("asdfasdfasdf","asdfasdf");
Intent upIntent = NavUtils.getParentActivityIntent(this);
NavUtils.navigateUpTo(this, upIntent);
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater Mymenu = getMenuInflater();
Mymenu.inflate(R.menu.item_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
These are problems .
how to done this ????
First issue
This is a duplicate of this question. I think that it doesn't show up because you have an options button on your device.
See this post about modifying this behavior in reflection (however, I'm not sure I would recommend it, as people have expectations about their device's behavior).
Second issue
If I followed your code correctly, then the item id is item_back and not R.id.home...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a newbie to android development and have little experience of eclipse. My android emulator is working properly but I cannot see application running on it.
Any pointers would be great. Thanks in advance.
MainACtivity.java
package com.example.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
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; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.act`enter code here`ion_settings) {
return true;
}
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.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
use the
AndroidManifest.xml
file, you can actually even have more than one launcher activity specified in your application manifest. To make an activity seen on the launcher you add these attributes to your activity in the manifest
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
set this inside your <activity> . hopefully it should work now. :)
I have received forced closed error message on running the sample code of Google API.Is update a project is a solution to this type of project? If so then how to update project on window.The method given here don't work on my case.
Here is code
Filename:HelloGoogleMaps.java
package com.hellogooglemaps.practices;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
public class HelloGoogleMaps extends MapActivity {
/** Called when the activity is first created. */
#Override
protected boolean isRouteDisplayed(){
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
#Override
protected boolean isLocationDisplayed(){
return true;
}
}
HelloGoogleMaps Manifest
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".HelloGoogleMapsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelloGoogleMaps" android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" />
<uses-library android:name="com.google.android.maps"/>
</application>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0_hYeORLgQUROOOr_RXN2TWG2u2pCDoBYfLCV_w"
/>
As you had mentioned in your code, You have only one Activity called HelloGoogleMaps and in your AndroidManifest you have declared two activities!! your HelloGoogleMapsActivity is useless. You are getting the error because you set HelloGoogleMapsActivity as the Launcher activity and this activity don't even exist!!
Solution:
change your AndroidManifest.xml like that and you will get your app work if you had managed to get the apikey for your map correctly:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
android:name=".HelloGoogleMaps" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"/>
</application>
After facing Force close Error many time what I have found is that these type of error is so common and come due to the undefined behavioral.Like when we declare Button and without assigning the view on it we use it on another function. It is also appear due to not handling the exception correctly. or Due to unavailable of resource your are trying to access in emulator like contact number. Best method to solve these type of problem is using log and printing some message on place where we feel confusing and using logcat.
In above case I had received force close error due to undefined behavior in HelloGoogleMapsActivity class
I am pretty new to java and android development; and I have been working on an app in which I want an action bar with a SearchView. I have looked at the google examples but I can not get it to work. I must be doing something wrong and I'm about to give up on app-development :D I have searched but I have not found anything helpful. Maybe you guys can help me :)
The problem: I get the search view to open as it should, but after that nothing happens at all, I've noticed that my searchManager.getSearchableInfo(getComponentName()) returns null. Also my hint that I've given is not displayed in my search box which leads me to believing that maybe the app can't find my searchable.xml? Enough talk :)
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().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()));
return true;
}
}
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_label">
</searchable>
Androidmanifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</application>
</manifest>
SearchableActivity.java
package com.example.searchapp;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SearchableActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// 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);
}
}
private void doMySearch(String query) {
Log.d("Event",query);
}
}
And here is a link to the referred project, I would be eternally grateful if someone was to help me with this!
Source code
Your problem is in your AndroidManifest. I had it almost exactly as you do, because that is what I get following the documentation. But it is unclear or mistaken.
Watching at the API Demos source I found that the "android.app.searchable" metadata must go in your "results" activity, and in the main activity (or the activity where you place the SearchView) you point to the other one with "android.app.default_searchable".
You can see it in the following file, which is the Manifest from my test project:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<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=".SearchActivity" />
</activity>
<activity
android:name=".SearchActivity"
android:label="#string/app_name" >
<!-- This intent-filter identifies this activity as "searchable" -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- This metadata entry provides further configuration details for searches -->
<!-- that are handled by this activity. -->
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
It is also important that the hint and label in the searchable.xml are references, and not hardcoded strings.
I hope it solves your problem. It took me all day to figure it out :(
Android guides explicitly says:
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Which is in your case NOT TRUE. Because you don't want to search in MainActivity but in SearchableActivity. That means you should use this ComponentName instead of getComponentName() method:
ComponentName cn = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
Also it seems that both the android:hint and android:label attributes MUST be references to strings in strings.xml. Being lazy and hardcoding the string values doesn't seem to work at all :)
i.e. Don't do:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="Search for something..."
android:label="My App">
</searchable>
But definitely do (as the OP correctly did):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_label">
</searchable>
If you are using Android Studio,
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
part may not work.
write full package name like
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.test.SearchActivity" />
be careful with "xml/searchable.xml".
If you made HARD CODE with android:hint and android:label.It will not work. Haha
Should be :
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/app_name"
android:label="#string/search_lb"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
There's actually no need use the "missing part" as described in the answers above. This method(using a SearchView) works exactly as the official docs say till Using the Search widget. Here a slight modification needs to made as follows:
Properties in a SearchableInfo are used to display labels, hints, suggestions, create intents for launching search results screens and controlling other affordances such as a voice button(docs).
Thus, the SearchableInfo object we pass to the SearchView.setSearchableInfo(SearchableInfo s) must contain proper references to the target activity which should be displayed to show the search results. This is done by manually passing the ComponentName of our target activity. One constructor for creating a ComponentName is
public ComponentName (String pkg, String cls)
pkg and cls are package names and class names of the target activity to be launched for displaying search results. Note that:
pkg must point to the root package name of your project and,
cls must be a fully qualified name of the class(i.e. the entire thing)
e.g.:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
String pkg = "com.example.musicapp"
String cls = "com.example.musicapp.search.SearchActivity"
ComponentName mycomponent = new ComponentName(pkg,cls);
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(mycomponent));
searchView.setIconifiedByDefault(false); //if using on actionbar
return true;
}
Depending on your requirements take a look at the other ComponentName constructors.
Make sure your searchables.xml file is in the correct location (i.e, in your res/xml/ folder) and that the same file does not contain any errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName) will return null, breaking your search functionality.
(Also, ensure you set componentName in the appropriate manner, because the example shown in the Android guide is for only when you are making searches and displaying searches in the same Activity.)
i was facing same issue adding search intent filter like this to searchable activity:
<activity
android:launchMode="singleTop"
android:name=".ui.activities.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<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>
Double check if the corresponding entry(ie search_hint and app_label) is present in values/strings.xml
eg
<string name="app_label">FunApp</string>
<string name="search_hint">Caps Off</string>
I'm trying to create an app that will change a setting when the user holds the search button. It seems that the combination of the WRITE_SETTINGS permission and the fact that the activity is launched from another application using the long press makes the parent task crash. For example, if I'm looking at Google Maps and hold the search button, the SearchButtonPopup does not appear and Google Maps crashes.
I tried taking the Theme.Dialog out, but it didn't make any difference. Removing the permission makes it robust again, no crashes, but I need the permission! Any fixes or workarounds?
These are my manifest and java files:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.stathissideris.searchbuttonpopup" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application android:icon="#drawable/logo" android:label="#string/app_name"
android:debuggable="true" android:permission="android.permission.WRITE_SETTINGS">
<activity android:name=".SearchButton" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
SearchButton.java
package org.stathissideris.searchbuttonpopup;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SearchButton extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SearchButtonPopup", "search held");
}
}