Menu does not show up in Android studio - java

This is my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="k.coursera.mylocation">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".test"></activity>
</application>
</manifest>
When I tried to change the theme to:
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
The menu will show up but the other function of my act wouldn't work (such as start a new activity).
And here is the piece of code for my menu XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/deleteCountry" android:title="delete" />
</menu>
And my java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.deleteCountry:
mAdapter.removeAll();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Related

Android - SearchView not working in single fragment activity

Good morning everybody, I'm trying to implement search features in a single activity with search features by implementing SearchView. From UI-side, everything is working.
But unlike almost of example I found, SearchView does not start another activity but is supposed so send request to the single running activity named HomeActivity.
Following some documentation I found on Android developers website and other sources, I try to do like below. Normally, I would supposed to handle search request in the event "onNewIntent" but it's never called. Click on the button after entering text has no effect (it just closes the keyboard).
I'm beginner in Android development and probably missed something simple.
Can you please help me ?
HomeActivity
public class HomeActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private FrameLayout myFrameLayout;
private Fragment fragmentContracts, fragmentHardware, fragmentMaps, fragmentUsers;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
// Switch to fragment ...
}
return loadFragment(fragment);
}
};
private void handleIntent(Intent intent) {
Log.i("LOG_myapp", "Called new intent !!!");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
String msg = MessageFormat.format("Search query = {0}", query);
Log.i("LOG_myapp", msg);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(query)
.setTitle("Search request");
AlertDialog dialog = builder.create();
dialog.show();
}
}
#Override
protected void onNewIntent(Intent intent){
// *** NEVER CALLED :( ***
Log.i("LOG_myapp", "Event onNewIntent raised");
handleIntent(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
myFrameLayout = (FrameLayout)findViewById(R.id.MyFrameLayout);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Fragment defaultFragment = new places();
defaultFragment.setRetainInstance(true);
loadFragment(defaultFragment);
Toolbar toolbar = (Toolbar)findViewById(R.id.activity_main_toolbar);
setSupportActionBar(toolbar);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.application, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.MenuItemSearch).getActionView();
searchView.setSubmitButtonEnabled(true);
ComponentName componentName = new ComponentName(this, HomeActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.MenuItemSettings:
Intent intent = new Intent(this, preferences.class);
startActivity(intent);
break;
}
return true;
}
#Override
public boolean onQueryTextChange(String s){
Log.i("LOG_myapp","Search query: " + s);
return true;
}
#Override
public boolean onQueryTextSubmit(String s){
Log.i("LOG_myapp","Submitted search query: " + s);
return true;
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
try {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.MyFrameLayout, fragment);
transaction.addToBackStack(null);
transaction.commit();
return true;
}
catch (Exception ex)
{
return false;
}
}
return false;
}
}
Menus.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/MenuItemSearch"
android:title="Search"
android:icon="#drawable/baseline_search_white_18dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/MenuItemSettings"
android:icon="#drawable/baseline_settings_white_18dp"
app:showAsAction="always"
android:title="Settings"/>
</menu>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vermilionenergy.myapp">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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=".HomeActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<!--action android:name="android.intent.action.MAIN" /-->
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.DEFAULT" />
<!-- category android:name="android.intent.category.LAUNCHER" / -->
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/>
<meta-data android:name="android.app.default_searchable" android:value=".HomeActivity" />
</activity>
<meta-data android:name="android.app.default_searchable" android:value=".HomeActivity" />
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".maps"
android:label="#string/title_activity_maps" />
<activity
android:name=".preferences"
android:label="Preferences" />
<!-- activity android:name=".users" / -->
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TextActivity"></activity>
</application>
</manifest>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="myapp"
android:hint="Search items"
>
</searchable>
I finally found the solution myself.
It looks like the direct strings in searchable.xml don't work. I replaced them by variable as below and it worked :)
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_title"
android:hint="#string/search_hint"
>
</searchable>

How can I stop my Android webview app from refreshing when I rotate my phone?

I am trying to complete my org's first app (we lost our dev due to health issues) and it seems to be working except that when the phone is rotated it refreshes back to the login screen. I realize that this question was answered previously here but this information is 4 years old and I get some compiling errors.
Here is our code - I really hope that someone can help as I am completely new to this and not really a dev myself, but I feel like what we have is close to a solution.
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.ruffarc.flytest">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main Activity:
package ca.ruffarc.flytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("https://flyapp.ca/en/authentication");
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}
If you look at the AndroidManifest.xml from the link you gave you will see that <activity> has property android:configChanges="orientation|keyboardHidden".
So you need to do the same for your activity in the manifest with small difference:
android:configChanges="orientation|screenSize|keyboardHidden".
Here's a full example of Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.ruffarc.flytest">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

I have created an android app in eclispe. when i install i got two apps installed

Friends,
I Have created an android file in eclipse and Exported as an android file. When i install it i got two android files. I have two java files in the app. one is second.java and the other is first.java.
When i install the app in blue stacks , it installed two files . One is first and other is second.
I have a button in my first.java which goes to the second.java file. How can i solve it by installing only one app in app tray...???
Here is my code
first.java
package com.zacter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class first extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
#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;
}
public void addListenerOnButton(){
final Context context=this;
button=(Button) findViewById(R.id.continuebutton);
button.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent intent = new Intent(context,second.class);
startActivity(intent);
}
});
}
}
Second.java
package com.zacter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boostram);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.boostram, menu);
return true;
}
}
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zacter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.zacter.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zacter.SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName="android.app.LauncherActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.app.LauncherActivity" />
</activity>
<activity
android:name="com.zacter.Boostram"
android:label="#string/title_activity_boostram" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Replace 'LAUNCHER' with 'DEFAULT'
See here for further explaination
In your manifest files, you are having more than one activities with the intent filter category as launcher.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The intent category means you are going to have two launcher icons, or two launch points for your application.
You can read more about it here
http://developer.android.com/guide/topics/manifest/category-element.html
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zacter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.zacter.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zacter.SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName="android.app.LauncherActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.app.LauncherActivity" />
</activity>
<activity
android:name="com.zacter.Boostram"
android:label="#string/title_activity_boostram" >
</activity>
</application>
</manifest>
just Because of these lines.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
keep this intent filter for only one activity which you want to launch at starting. if you keep category launcher for any other activities it will creates multiple launcher icons in applications. for more details check this link here
Keep this tag in only first activity remove from Boostram activity tag
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<category android:name="android.intent.category.LAUNCHER" /> will create launcher activity, so if you have multiple LAUNCHER category in AndroidManifest.xml file, multiple launcher activity will generate.
Modify your AndroidManifest.xml like below.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".first"
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>
<activity
android:name=".second"
android:label="#string/title_activity_main" >
</activity>
</application>

can't add search button on action bar

So i'm new to android programming
im going to add two action bar , but when i add the second action bar , it fills the old one , not making a new one .
i already use the holo light theme .
this is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orionpreneur"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow">
<!-- Splash Screen Activity -->
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name=".LoginActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/about_us"
android:parentActivityName=".LoginActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.orionpreneur.MainActivity" />
</activity>
<activity
android:name=".About"
android:label="#string/title_activity_about"
android:parentActivityName=".LoginActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.orionpreneur.MainActivity" />
</activity>
</application>
</manifest>
this is my main_activity_menu.xml code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/about"
android:title="#string/about"
android:showAsAction="always"/>
</menu>
this is my MainActivity.java code
package com.example.orionpreneur;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class LoginActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
#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_activity_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#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.
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "you've pressed the search button !", Toast.LENGTH_SHORT).show();
return true;
case R.id.about:
Intent in = new Intent(this, About.class );
startActivity(in);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
i wanted the search button to be added in separated bar like this
https://developer.android.com/images/training/basics/actionbar-actions.png
i'm desperate , sorry if my question is already asked .
thanks :)))
*remove this *
android:uiOptions="splitActionBarWhenNarrow"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen

Actionbar not rendering using nbandroid

Cannot get the Actionbar to load. It works using Eclipse and Android Studio but I prefer to work in Netbeans if at all possible. New to Android development
MainActivity.java
package action.bar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
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) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbarmenu, menu);
return super.onCreateOptionsMenu(menu);
}
}
actionbarmenu.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/contacts"
android:title="Private Contacts"
/>
<!-- android:icon="#drawable/ic_action_person" -->
</menu>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="action.bar"
android:versionCode="15"
android:versionName="15.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<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>

Categories