Android - Menu items not showing as they should - android

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>

Related

Duplicating menu items everytime it open in overflow options menu

I am trying to add two menu items in action bar with xml code:
<?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/search"
android:title="#string/search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/share"
android:title="#string/share"
android:icon="#drawable/ic_action_share"
app:showAsAction="ifRoom"
/>
And my java code is :
package com.erprakash.contacts;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
//import android.net.Uri;
import android.provider.ContactsContract;
//import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
//import java.util.Comparator;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
ArrayList<Contact> arrayList = new ArrayList<>();
int img_id = R.drawable.image1;
String name ;
String number ;
private int id;
private Contact contact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Tool bar setting
//toolbar end
// Reading Contacts -----Working
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
if (phones != null) {
while (phones.moveToNext())
{
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID));
if(id == 0){
contact = new Contact(number, name, img_id);
}else {
contact = new Contact(number, name, id);
}
arrayList.add(contact);
}
phones.close();
}
/**
name = getResources().getStringArray(R.array.person_name);
number = getResources().getIntArray(R.array.mobile_number);
int c = 0;
for(String Name: name)
{
Contact contact = new Contact(number[c],Name,img_id[c]);
c++;
arrayList.add(contact);
}
**/
recyclerView = (RecyclerView)findViewById(R.id.rvMainContennt);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new ContactAdapter(arrayList);
recyclerView.setAdapter(adapter);
}
//-------------Toolbar------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int res_id = item.getItemId();
if(res_id == R.id.search)
{
Toast.makeText(this.getApplicationContext(),"You have seleted search option",Toast.LENGTH_SHORT).show();
}
return true;
}
#Override
public boolean onPrepareOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
}
In preview the design appears correct :
But in running state :
And on clicking the option menu :
Every time when i open the option menu the content gets duplicated and goes on , i have searched solution for this but i didn't get it anywhere . help me out....Thanks in advance
Remove onPrepareOptionsMenu().
#Override
public boolean onPrepareOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
Update onCreateOptionsMenu() as below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
If you want to show menu item always on Toolbar, then update menu_main.xml as below:
<?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/search"
android:title="#string/search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always" />
<item
android:id="#+id/share"
android:title="#string/share"
android:icon="#drawable/ic_action_share"
app:showAsAction="always" />
</menu>
When I ran into this issue I called menu.clear() inside of onPrepareOptionsMenu and that fixed it.
If your wondering why I didn't switch to onCreateOptionsMenu it is because I need to use onPrepareOptionsMenu for reasons.

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

SearchView in action bar is shifted to right

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?

Refresh button in action bar of android app

i m very new into android app. I m trying to execute an app that should contain a refresh button in the action bar. While compiling a code, the studio is prompting this error
"Error:(50, 20) error: non-static method reload() cannot be referenced from a static context"
I have written this uptil now
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Menu;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl("http://www.example.com");
setContentView(mWebView);
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_refresh){
WebView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_main.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=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item
android:id="#+id/action_refresh"
android:title="#string/refresh_button"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="1"
app:showAsAction="never"
/>
</menu>
Change WebView.reload() to mWebView.reload()
In the former case, you are calling reload() on the WebView class, whereas in the latter you are telling your specific instance of WebView to reload.
This SO question may clarify things a bit more.

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