The ActionBar does not change, I don't know why:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
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) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.testing_menu, menu);
return true;
}
}
I have included Android support library v7.
The testing_menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/go_back"
android:icon="#drawable/back"
android:title="Go back"
/>
</menu>
The "Go Back" does not showing up.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.testing_menu, menu);
return true;
}
why don't you call return super.onCreateOptionsMenu(menu); ?
Related
I'm not able to dispay MenĂ¹ in my main Activity and all the other Activities.
This is my main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/cardview_dark_background">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="#color/cardview_light_background">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/articoli"
android:onClick="visualizzaArticoli"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Articoli"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is my main.java file:
package com.mcsolution.easymanagementandroid.main;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Toast;
import com.mcsolution.easymanagementandroid.R;
import com.mcsolution.easymanagementandroid.beans.Program;
import com.mcsolution.easymanagementandroid.connection.MyDatabase;
import com.mcsolution.easymanagementandroid.utility.SyncDati;
public class Main extends Activity {
public MyDatabase db;
public ProgressDialog dialog;
public String url="";
private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.care_home, menu);
return super.onCreateOptionsMenu(menu);
}
private void apriConnessioneDB(){
db=new MyDatabase(getApplicationContext());
db.open(); //apriamo il db
}
public void visualizzaArticoli(View view){
Intent intent = new Intent(this, com.mcsolution.easymanagementandroid.articoli.viewArticoli.class);
startActivity(intent);
}
}
This is my menu care_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_logout"
android:orderInCategory="100"
android:title="Logout"/>
</menu>
If I try to start my application, I don't have any error but I can't to see MenuBar. I have try to set a debug point into OnCreateOptionMenu method but, the method is never called.
How can I fixed it ?
You need to return true; in onCreateOptionsMenu() as shown in the code below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.care_home, menu);
return true;
}
Simply use this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.care_home, menu);
return true;
}
Your activity class extends Activity, not AppCompatActivity. The default theme for these activities does not include an Action Bar, which is where the menu would live. You will need to add a <Toolbar> to your activity's layout file, and then call setActionBar() from your onCreate() method.
For example, add this as the first child of your top-level <LinearLayout> tag:
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And add this just after you call setContentView() in your onCreate():
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setActionBar(toolbar);
Now you will have an Action Bar in your activity, and so your menu will appear.
You should return true instead of super like this:
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (menu != null) {
}
return true;
}
Hope it helps.
You have to use both methods on your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
compileSdkVersion 25
minSdkVersion 17
dependencies compile 'com.android.support:appcompat-v7:25.2.0'
This is my menu in /res/menu/main.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/create_order"
android:title="Create Order"
android:icon="#drawable/ic_playlist_add_black_24dp"
android:orderInCategory="1"
app:showAsAction="always"/>
<item
android:id="#+id/action_setting"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never"/>
</menu>
And this is the MainActivity:
package com.example.fulvio.bitsandpizzas;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
I'm not getting where my code is wrong, neither I can see the icon directly in the ActionBar and in the overflow menu, but only text.
I've already read the other similar questions (and answers), but none helped me
Android does not show icon anymore by default in menu. You have to use retrospection to achieve this (in your activity):
#Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
if(menu != null){
if("MenuBuilder".equals(menu.getClass().getSimpleName())){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
//
}
}
}
return super.onPrepareOptionsPanel(view, menu);
}
I'm not fond of this method, but I did not find any other solution for this issue
You need to change
app:showAsAction = "always"
to
android:showAsAction = "always"
Or
You can use AppCompatActivity instead of Activity :
package com.example.fulvio.bitsandpizzas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I added SearchView to my action bar, but it appears to be shifted to right if there is back button (or drawer toggle) in action bar.
main_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:title="123"
android:icon="#mipmap/ic_launcher"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
</menu>
MainActivity.java:
package org.example.myapplication;
import android.os.Bundle;
import android.view.Menu;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
If setDisplayHomeAsUpEnabled(false):
If setDisplayHomeAsUpEnabled(true):
How can I fix this?
I have a SearchView in my activity using this code:
import java.util.List;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.SearchView;
import android.widget.TextView;
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener {
private SearchView mSearchView;
private TextView mStatusView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
mStatusView = (TextView) findViewById(R.id.status_text);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
return true;
}
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextChange(String newText) {
mStatusView.setText("Query = " + newText);
return false;
}
public boolean onQueryTextSubmit(String query) {
mStatusView.setText("Query = " + query + " : submitted");
return false;
}
public boolean onClose() {
mStatusView.setText("Closed!");
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
}
menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="Search"/>
</menu>
and layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
</LinearLayout>
What I want to do is when the Activity is started, I want the SearchView to start off opened. It starts off "closed" like this:
but I want it to be "open" like this:
Anyway I can do this? Thanks
It's at the top of the documentation: http://developer.android.com/reference/android/widget/SearchView.html
setIconifiedByDefault(false)
Edit:
In order to have it collapsible but expanded at the start then programatically expand it in onCreateOptionsMenu with
searchItem.expandActionView();
and you'll probably need
mSearchView.requestFocus();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// mEditor=(TextView)findViewById(R.id.text);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
Log.d("sayem", "onCreateOptionMEnu");
return true;
}
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="schemas.android.com/apk/res/android"; >
<item android:id="#+id/settings"
android:title="#string/settings_label"
android:visible="true"
android:alphabeticShortcut="#string/settings_shortcut"
android:icon="#drawable/violet" />
</menu>
take a look link1 or link2.
Or if don't want to inflate menu.xml, you can do this :
Try the following one, you don't need to inflate xml in this.
package com.menusample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MenuSampleActivity 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) {
//menu.add(int groupId,int itemId,int orderId, Charsequence title);
menu.add(0, 0, 0, "title1");
menu.add(0, 1, 1, "title2");
menu.add(0, 2,2, "title3");
menu.add(0, 3, 3, "title4");
return super.onCreateOptionsMenu(menu);
}
}
The only difference that I can see in the code from my code is that in your XML file, the line
<menu xmlns:android="schemas.android.com/apk/res/android"; >
has a semi-colon at the end.
Mine does not, and the example from Android, at http://developer.android.com/guide/topics/ui/menus.html, does not have a semi-colon either.
The line of code should look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">