SearchView in action bar is shifted to right - android

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?

Related

OnCreateOptionMenu is never called

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);
}

I can't display icons in the menu/actionBar (minSDK 17 and targetSDK 25), what's wrong?

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;
}
}

Android - Menu items not showing as they should

I want to include a menu in my activity, now it has just one item for help with an icon and a text, the showAsAction is set to ifRoom but it is always shown in the action overflow. Why I don't get juste the drawable?
Here is the xml:
<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="com.mycompany.myapp.Mymainclass" >
<item
android:id="#+id/help"
android:orderInCategory="100"
android:title="#string/help"
android:icon="#drawable/ic_help"
app:showAsAction="ifRoom"/>
</menu>
The MainActivity:
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Base64;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
SharedPreferences prefs;
Menu menu;
#Override
public void onCreate(Bundle save){
super.onCreate(save);
setContentView(R.layout.pokemons_layout);
prefs = getSharedPreferences("PREFERENCES", Context.MODE_PRIVATE);
if (findViewById(R.id.fragment_container) != null) {
if (save != null) {
return;
}
CustomListFragment listFragment = new CustomListFragment();
listFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, listFragment).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onPause(){
super.onPause();
String bytearray = Base64.encodeToString(DB.bytearray, Base64.DEFAULT);
prefs.edit().putString("BYTEARRAY", bytearray).apply();
}
#Override
public void onResume(){
super.onResume();
String bytearray = prefs.getString("BYTEARRAY", Base64.encodeToString(DB.bytearray, Base64.DEFAULT));
DB.bytearray = Base64.decode(bytearray, Base64.DEFAULT);
}
}
try this because not all apps use the appcompat support lib
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
</menu>

Programmatically setting SearchView to be open

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();

Search view widget and Icon not showing up in the action bar

I am a beginner to Android. Trying to add a search a widget in the action bar.
Here is my menu file code ("main_acitivity.xml"):
<?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_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
My MainActivity class code is:
package com.example.fragment;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.fragment.MESSAGE";
#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.
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.main_activity,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view){
Intent intent= new Intent(this,DisplayMessageActivity.class);
EditText editText= (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I have tried "android.widget.SearchView" as well but it is not even showing the icon. Could you please advise what I am doing wrong ?
you missed these:
xmlns:yourappname="http://schemas.android.com/apk/res-auto"
and
yourappname:showAsAction="always"
yourappname:actionViewClass="android.support.v7.widget.SearchView"/>

Categories

Resources