Don't display icon on MenuBar in Android [duplicate] - android

This question already has answers here:
showAsAction = "always" is ignored in Toolbar
(2 answers)
Android 4.3 menu item showAsAction="always" ignored
(13 answers)
Closed 5 years ago.
I'm building an app with AndroidStudio. I want to display an ActionBar in my application.
I want to add a menu into my AppCompactActivity. This is the menu:
<?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="#android:drawable/ic_menu_save"
android:showAsAction="always"
android:title="Salva"/>
</menu>
This is the layout that I can see from Android Studio preview.
This is my AppCompactActivity:
public class setting extends AppCompatActivity{
public Program program;
public TextView textTargetUri;
public TextView labelUrl;
public ImageView targetImage;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
img = (ImageView)findViewById(R.id.ImageView01);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_setting, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
getSchermataHome();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
But this is the layou that I can see:
How can I fixed it?

Try using the following 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/action_search"
android:icon="#android:drawable/ic_menu_save"
android:title="Salva"
app:showAsAction="ifRoom|always"/>
</menu>
change always to ifRoom|always with app attribute

Change your android:showAsAction="always" to app:showAsAction="always"
So now your code would be
<?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="#android:drawable/ic_menu_save"
app:showAsAction="always"
android:title="Salva"/>
</menu>
Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices.

Related

How to add multiple icons on action bar in android?

I want to add 2 or 3 icons on action Bar in android app. I already took the empty activity and added the toolbar. I also set the Icon at left side. Now i want to add another two icons on it. But there is no Menu folder in my project directory structure. So any one tell me how i can do this all with proper guidelines?
My code is here :
My activity file
public class ActionBarActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_bar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.left_nav);
getSupportActionBar().setTitle("");
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
my .xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:fitsSystemWindows="true"
tools:context="firstapp.vaibhav.com.firstapp.ActionBarActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Screen shot of my project directory structure
1. Create a menu folder in your existing resource res folder. (Ex. .../res/menu)
2. Create a main.xml file in menu folder. (Ex. .../res/menu/main.xml)
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/action_item_one"
android:title="Camera"
android:icon="#drawable/ic_menu_camera"
app:showAsAction="always" />
<item
android:id="#+id/action_item_two"
android:title="Send"
android:icon="#drawable/ic_menu_send"
app:showAsAction="always" />
</menu>
3. In your activity, Override onCreateOptionsMenu() and onOptionsItemSelected() to work with option menus.
ActionBarActivity.java
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_camera) {
// Do something
return true;
}
if (id == R.id.action_send) {
// Do something
return true;
}
return super.onOptionsItemSelected(item);
}
OUTPUT
Hope this will help~
create menu.xml with item like this
<?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/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />-->
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_autorenew"
android:title="Search"/>
<item
android:id="#+id/action_search"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_search"
android:title="Search"/>
</menu>
and use it in activity
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// MenuInflater inflater1 = getActivity().getMenuInflater();
inflater.inflate(R.menu.cartmenu, menu);
return ;
}
in your res/menu/menu_main.xml:
add
<?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/icon_id"
android:visible="true"
android:title="#string/icon_name"
android:icon="#drawable/your_image"
app:showAsAction="always">
</item>
</menu>
in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// return true so that the menu pop up is opened
return true;
}
To access your menu item in activity add:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.your_item_id) {
// your code
return true;
}
return super.onOptionsItemSelected(item);
}
You can use showAsAction option found at item in menu resource file.
1) If you want to add popup menu then write app:showAsAction="never"
2) If you want to add icons as an action (multiple icons in actionbar) then write app:showAsAction="always"

ActionBar options and icons [duplicate]

This question already has answers here:
Can't display an icon in the actionbar
(2 answers)
Closed 7 years ago.
I'm trying create an ActionBar for my Android app with Android Studio, but when I execute, the items don't appear. I set the showAsAction to always or ifRoom but nothing happened.
Menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/itmp"
android:icon="#mipmap/ic_launcher"
android:title="Profile"
android:showAsAction="always"/>
<item android:id="#+id/itap"
android:icon="#mipmap/ic_launcher"
android:title="My order"
android:showAsAction="ifRoom" />
<item android:id="#+id/ithc"
android:icon="#mipmap/ic_launcher"
android:title="Historic"
android:showAsAction="ifRoom" />
<item android:id="#+id/itsr"
android:icon="#mipmap/ic_launcher"
android:title="Exit"
android:showAsAction="ifRoom"/>
</menu>
MainActivity code:
public class MainScreen_Activity extends AppCompatActivity {
GridView grid;
Button c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainscreen);
c = (Button) findViewById(R.id.btnmaincar);
c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_actionbar, menu);
return super.onCreateOptionsMenu(menu);
}
}
when I execute the app in my smartphone anything doesn't appear. Only an option icon with all the items.
When using AppCompatActivity, you need to use app:showAsAction instead of android:showAsAction.
Your menu XML should look more like this:
<?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/itmp"
android:icon="#mipmap/ic_launcher"
android:title="Profile"
app:showAsAction="always"/>
</menu>

how to add action bar items to second activity

This is my project (How can i start a different activity on item click from a custom listview?)
i was able to sort it out.
However when i try to add action bar items like search to the second activity,the items are not being displayed.I know its a simple procedure but i am stuck.I followed the developer's guide (https://developer.android.com/training/basics/actionbar/adding-buttons.html) but to no success.
Any help will be appreciated guys.
Here's the second activity code:
public class School extends ActionbarActivity{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.school);
Intent newActivity1=new Intent();
setResult(RESULT_OK, newActivity1);
//finish();
}
}
main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
</menu>
Try to extend your activity to ActionBarActivity:
public class School extends ActionBarActivity
edit:I've tested this code and it's working for me:
<?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/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
</menu>
There is another option for Action Bar.
that is toolbar.
The code for it:
//First define toolbar
Toolbar toolbar;
//Then find it by id
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//then we can add list of menu options in menu.xml
by adding items

Android ActionBar not showing icons

I have seen this question asked many times, however, none of those solutions have helped me. My problem is that I have an ActionBar menu and I want its items to always be displayed on the ActionBar instead of the drop down menu.
I have tried actions suchs as "ifRoom", "always", etc. and they still show only with text on the drop down menu.
Menu: devotional_fragment_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
app:showAsAction="always"/>
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_go_to_today"
android:title="#string/date_action"
app:showAsAction="always"/>
<item android:id="#+id/action_next"
android:icon="#drawable/ic_action_next_item"
android:title="#string/next_action"
app:showAsAction="always"/>
</menu>
On my fragment I have:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// action bar
inflater.inflate(R.menu.devotional_fragment_actions, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection of action bar
switch (item.getItemId()) {
case R.id.action_search:
showDatePickerDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The menu works fine but is not displaying as I want it. Thanks
In each item use android namespace instead of app. You can even remove this namespace declaration xmlns:app="http://schemas.android.com/apk/res-auto".
Try set it like in code below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
android:showAsAction="always"/>
</menu>

Android action buttons in action bar not appearing?

I want to create an add button in the action bar but it doesn't seem to appear when i run thee code.
Here is my main_activity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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_actions, 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.
int id = item.getItemId();
if (id == R.id.action_add) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
This is my main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_add"
android:icon="#drawable/ic_add"
android:title="Add"
android:showAsAction="always"
/>
</menu>
Please Help
I cant figure out what's wrong !
Since your Activity extends ActionBarActivity you would be using AppCompat from support library.
So change to
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_add"
android:icon="#drawable/ic_add"
android:title="Add"
yourapp:showAsAction="always"
/>
...
</menu>
Quoting docs
Notice that the showAsAction attribute above uses a custom namespace
defined in the tag. This is necessary when using any XML
attributes defined by the support library, because these attributes do
not exist in the Android framework on older devices. So you must use
your own namespace as a prefix for all attributes defined by the
support library.
Change your menu_xml to this :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_add"
android:clickable="true"
android:icon="#drawable/ic_add"
android:showAsAction="always"
android:title="action_location_found">
</menu>
Replace your menu file with below menu file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="#style/Theme.AppCompat.Light"
xmlns:tools="http://schemas.android.com/tools"
tools:context="yourpackagename.Mainactivity" >
<item android:id="#+id/action_add"
android:icon="#drawable/add_plush" app:showAsAction="always"></item>
</menu>
thats it...

Categories

Resources