Adding icons to the ActionBar on Android - java

Im following the next tutorial: Adding Action Buttoms, I did it step by step but when I run my app doesn´t shows any icon. I was debugging but everything seems correct and I have any nullPointerException or something like that.
This is my mainActivity:
package com.training.training.appone;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = " com. training.example.appone.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_my, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activiy_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.txt_edit_message);
String message = editText.getText().toString();
// El primer parametro es el KeyName:
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
And I put this 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_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
I have an error in the last line "android:showsAsAction="never", I have the libray downloaded and referenced in my build gradle: compile 'com.android.support:appcompat-v7:22.0.0' I also tryed changed "android" to "res-auto", but it says that the items must have a title (they have them):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/res-auto">
<!-- 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="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
My library:
In this point I have no idea what happends.

Make the following changes in your menu.xml
<menu
....
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
...
app:showAsAction="never" />
</menu>
And if you want to display your menu on actionBar then it should be app:showAsAction="always".

In your XML file, Please change this line
app:showAsAction="always"
instead of
android:showAsAction="never"

Probably as you are using AppCompat library, you should use
app:showAsAction="never"
instead
android:showAsAction="never"

If you look at the xml schemas in the tutorial you have both:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
Notice the xmlns:android and xmlns:yourapp are different. So all you need to do is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
yourapp:showAsAction="never" />
</menu>
The android and yourapp tags can be named however you want. They are just a named reference to the schema.

Related

Menu won't show up. I don't know why

I'm just following another tutorial but the result is different. When I'm trying to run my apk, menu icon won't show up on primary layout (activity_main.xml) Anyone knows what I missed?
menu_main_activity.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">
<!-- NEW NOTE -->
<item
android:id="#+id/action_create"
android:enabled="true"
android:icon="#android:drawable/ic_menu_add"
android:orderInCategory="0"
android:title="create"
android:visible="true"
app:showAsAction="always" />
<!-- SETTINGS -->
<item
android:id="#+id/action_settings"
android:enabled="true"
android:icon="#android:drawable/ic_menu_manage"
android:orderInCategory="10"
android:title="settings"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>
MainNote.java:
package com.example.lenovo.home;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import android.view.View.OnClickListener;
public class MainNote extends AppCompatActivity {
private ListView mListNotes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_note);
mListNotes = (ListView) findViewById(R.id.main_listview);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create: //run NoteActivity in new note mode
startActivity(new Intent(this, NoteActivity.class));
break;
case R.id.action_settings:
//TODO show settings activity
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
super.onResume();
//load saved notes into the listview
//first, reset the listview
mListNotes.setAdapter(null);
ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());
//sort notes from new to old
Collections.sort(notes, new Comparator<Note>() {
#Override
public int compare(Note lhs, Note rhs) {
if (lhs.getDateTime() > rhs.getDateTime()) {
return -1;
} else {
return 1;
}
}
});
if (notes != null && notes.size() > 0) { //check if we have any notes!
final NoteAdapter na = new NoteAdapter(this, R.layout.view_note_item, notes);
mListNotes.setAdapter(na);
//set click listener for items in the list, by clicking each item the note should be loaded into NoteActivity
mListNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//run the NoteActivity in view/edit mode
String fileName = ((Note) mListNotes.getItemAtPosition(position)).getDateTime()
+ Utilities.FILE_EXTENSION;
Intent viewNoteIntent = new Intent(getApplicationContext(), NoteActivity.class);
viewNoteIntent.putExtra(Utilities.EXTRAS_NOTE_FILENAME, fileName);
startActivity(viewNoteIntent);
}
});
} else { //remind user that we have no notes!
Toast.makeText(getApplicationContext(), "you have no saved notes!\ncreate some new notes :)"
, Toast.LENGTH_SHORT).show();
}
}
}
And, the last is my activity_main_note.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.lenovo.home.MainNote">
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</LinearLayout>
UPDATE
This is my styles code
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
This is my theme code for application.
I have just run your code and it works fine.
I am running it with the following theme in styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
In manifest I have android:theme="#style/AppTheme" in the application tag.
And my Activity extends AppCompatActivity
Also check you have put the menu xml in res/menu folder.
The only thing I see but it is regarding the item being selected is that the return statement is wrong, but it doesn't have to do with the icons not appearing.
In onOptionsItemSelected(MenuItem item) you should return true if you handle the request or false if not.
Change it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean rtn = false;
switch (item.getItemId()) {
case R.id.action_create: //run NoteActivity in new note mode
startActivity(new Intent(this, NoteActivity.class));
rtn = true;
break;
case R.id.action_settings:
//TODO show settings activity
rtn = true;
break;
default:
rtn = super.onOptionsItemSelected(item);
}
return rtn;
}
See this question: Should "android: onOptionsItemSelected" return true or false
EDIT I
I see in your styles.xml you are extending Theme.AppCompat.Light.NoActionBar, which doesn't have an ActionBar.
In this case you can switch to a theme with an ActionBar as I am using or otherwise you need to add your own action bar to the layout using a Toolbar.

Cannot resolve symbol 'R', & Can't resolve symbols in menu_main xml

I'm so frustrated here. I've searched around and tried a bunch of different things here. I've just started this project and am trying to add options to the 3dot menu first. Heres my 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"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#id/action_settings" android:orderInCategory="100" android:title="#string/action_settings" app:showAsAction="never" />
<item android:id="#id/opt1" android:title="#string/opt1" android:onClick="onOption1" android:showAsAction="ifRoom|withText" />
<item android:id="#id/opt2" android:title="#string/opt2" android:onClick="onOption2" android:showAsAction="ifRoom|withText" />
<item android:id="#id/opt3" android:title="#string/opt3" android:onClick="onOption3" android:showAsAction="never" />
<item android:id="#id/opt4" android:title="#string/opt4" android:onClick="onOption4" android:showAsAction="never" />
<item android:id="#id/opt5" android:title="#string/opt5" android:onClick="onOption5" android:showAsAction="never" />
<item android:id="#id/opt6" android:title="#string/opt6" android:onClick="onOption6" android:showAsAction="never" />
<item android:id="#id/opt7" android:title="#string/opt7" android:onClick="onOption7" android:showAsAction="never" />
<item android:id="#id/opt8" android:title="#string/opt8" android:onClick="onOption8" android:showAsAction="never" />
<item android:id="#id/opt9" android:title="#string/opt9" android:onClick="onOption9" android:showAsAction="never" />
<item android:id="#id/opt10" android:title="#string/opt10" android:onClick="onOption10" android:showAsAction="never" />
</menu>
Strings.xml:
<resources>
<string name="app_name">Watchproj</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="opt1">Display list forward</string>
<string name="opt2">Display list backwards</string>
<string name="opt3">Add new watch</string>
<string name="opt4">Show details of watch</string>
<string name="opt5">Delete watch</string>
<string name="opt6">Show average price</string>
<string name="opt7">Show number of automatic watches</string>
<string name="opt8">Show most expensive watch</string>
<string name="opt9">Load list from file</string>
<string name="opt10">Save list from file</string>
</resources>
Mainactivity.java
package com.example.annon.watchproj;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onOption1(MenuItem i){
}
public void onOption2 (MenuItem i){
}
public void onOption3 (MenuItem i){
}
public void onOption4 (MenuItem i){
}
public void onOption5 (MenuItem i){
}
public void onOption6 (MenuItem i){
}
public void onOption7 (MenuItem i){
}
public void onOption8 (MenuItem i){
}
public void onOption9 (MenuItem i){
}
public void onOption10 (MenuItem i){
}
}
I have no idea what to do, I'm just hoping I missed something stupid. Ive cleaned the project, and rebuild dozens of times. I'm just trying to run it and see if I get the options to show up.
The id should be like this
#+id/action_settings
yours is (without the + )
#id/action_settings

Toolbar cannot be customized as expected in android

I'm learning Material Design from video tutorial provided by slidenerd.In that they put one next icon on toolbar,everything is working in that tutorial but when i'm implemented it the icon is not appearing.
Here,i'm attaching my files please have a look and help me out at my learning stage.Please!!!
MainActivity.java
package materialtest.example.ritzipsy.mytoolbar5;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.id_settings) {
Toast.makeText(this, "You have Clicked Setting menu", Toast.LENGTH_SHORT).show();
}
if (id == R.id.id_navigate) {
Toast.makeText(this, "You have Clicked Next menu", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="materialtest.example.ritzipsy.mytoolbar5.MainActivity">
<include android:id="#+id/app_bar"
layout="#layout/app_bar"></include>
</RelativeLayout>
my_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/tools">
<item
android:id="#+id/id_settings"
android:title="Settings"
app:showAsAction="never">></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.co0m/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Got Output as
Expected output to be
Just change your menu xml as below. The problem is with this line
xmlns:app="http://schemas.android.com/apk/tools"
<?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/id_settings"
android:title="Settings"
app:showAsAction="never"></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>

Android Action Button not shown in action bar

I want to display an icon on the action bar. But it always comes in the overflow list. Could you please help me up with a solution. This is my code.
CreateScheduleActivity.java
package com.mpeers.ui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class CreateScheduleActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_schedule);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
action_bar.xml in menu folder
<?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/plusicon"
android:title="CS"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
If you are using the appcompat support library, you need to define
xmlns:app="http://schemas.android.com/apk/res-auto"
in the parent tag and then use app:showAsAction for controlling the visibility of the action items.
If you are not using the appcompat support library, you can use the android:showAsAction tag for controlling the visibility of the action items.
define xmlns:app="http://schemas.android.com/apk/res-auto" in parent tag.
then use app:showAsAction="ifRoom"
Following is the example code.
<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.taazi.android.MainActivity" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_right_drawer"
android:icon="#drawable/ic_action_play_over_video"
android:title="#string/action_right_drawer"
app:showAsAction="ifRoom"/>
</menu>

Android adding action buttons problems

I tried to add two action buttons on the action bar, for visibility one is defined as:
android:showAsAction="ifRoom"
another one is defined as:
android:showAsAction="never"
The problem is I can see the ic_action_search icon but I could not see the ic_action_overflow icon. This is the main_activity_actions.xml in menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyFirstApp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
MyFirstApp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:icon="#drawable/ic_action_overflow"
android:title="#string/action_settings"
MyFirstApp:showAsAction="never" />
</menu>
and this is the Java code that includes the buttons:
#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_actions, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So whats wrong with my code?
cheers
<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_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
it is work

Categories