Can't create action buttons - android

I am currently trying to learn android programming and have encountered a problem. When I try to complete the Action Bar tutorial it does not work. It runs, but on the Action Bar only the overflow button diplays and that contains the settings button. I have no idea why this does not work. Here is the code:
package com.example.usingui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.usingui.MESSAGE";
#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)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, 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_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.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/**
* 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;
}
}
}
and res/menu/main.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
I have no idea what I am doing wrong. Please help me!

#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Do something here
return true;
case R.id.action_search:
//do something here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That is my code for my action bar

I see that in your XML for the menu, you have android:showAsAction="never" change it to always for each menu item that you want to see in the action bar so it would be this:
<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="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="always" />
</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_settings) {
return true; ->you can handle every item of action bar and you can do any interaction in here!
}
return super.onOptionsItemSelected(item);
}
I am agree with Cybergei you should set visible of every item of action bar by writing android:showAsAction="always" and as I wrote above,you can do anything what you want by handling id of every item inside of the onOptionsItemSelected method

Related

Can not add two buttons in action bar Android

I am very new. I want to show two button and change their icons but I failed.
I don't understand the problem.
You can also use "always" to declare that an item always appear as an action button. I read it and used always but it didn' work.
menu_mainxml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_toggle_setting"
myapp:showAsAction="always"/>
<item android:id="#+id/action_about"
android:title="#string/action_about"
android:icon="#drawable/ic_action_toggle_about"
myapp:showAsAction="always"/>
MainActivityjava:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private MenuItem item1;
#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);
item1= menu.findItem(R.id.action_about);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
//some code
return true;
}
if (id == R.id.action_about) {
//some code
return true;
}
return super.onOptionsItemSelected(item);
}}
My result:
Change the following in your activity:
private MenuItem item1;
#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);
item1= menu.findItem(R.id.action_about);
return true;
}
Instead of Activity, try using AppCompatActivity.
MainActivity
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);
}
}
menu_main
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_toggle_setting"
myapp:showAsAction="always"/>
<item android:id="#+id/action_about"
android:title="#string/action_about"
android:icon="#drawable/ic_action_toggle_about"
myapp:showAsAction="always"/>
you should use below code and remove myapp prefrex instead used android like below
<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"
android:showAsAction="always"
android:visible="true" />
</menu>

Android Action Bar Button does not show up

I'm implementing an action bar for my app which has 2 menu buttons : Add and Settings. In Designer view the 2 buttons still apprear like normal. However when I run the app 2 buttons does not show up at all. Please help
Below are my code:
activity_main.xml
<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=".MainActivity">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
MainActivity.Java
package com.example.sunny.mynote;
import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteDataSource;
import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteItem;
import java.util.List;
public class MainActivity extends ListActivity {
public static final int EDITOR_ACRIVITY_REQUEST = 1001;
public static final int SETTINGS_REQUEST = 1003;
private static final int MENU_DELETE_ID = 1002;
private int currentNoteId;
private NoteDataSource datasource;
List<NoteItem> notesList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
datasource = new NoteDataSource(this);
refreshDisplay();
}
private void refreshDisplay() {
notesList = datasource.findAll();
ArrayAdapter<NoteItem> adapter =
new ArrayAdapter<NoteItem>(this, R.layout.list_item_layout, notesList);
setListAdapter(adapter);
}
//Add the Actions to the Action Bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
//Respond to Action Buttons
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_create:
createNote();
return true;
case R.id.action_settings:
Settings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createNote()
{
NoteItem note = NoteItem.getNew();
Intent intent = new Intent(this, NoteEditorActivity.class);
intent.putExtra("key", note.getKey());
intent.putExtra("text", note.getText());
startActivityForResult(intent, EDITOR_ACRIVITY_REQUEST);
}
private void Settings()
{
Intent intent2 = new Intent(this, Settings.class);
startActivityForResult(intent2, SETTINGS_REQUEST);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
NoteItem note = notesList.get(position);
Intent intent = new Intent(this, NoteEditorActivity.class);
intent.putExtra("key", note.getKey());
intent.putExtra("text", note.getText());
startActivityForResult(intent, EDITOR_ACRIVITY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == EDITOR_ACRIVITY_REQUEST && resultCode == RESULT_OK)
{
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
NoteItem note = new NoteItem();
note.setKey(data.getStringExtra("key"));
note.setText(data.getStringExtra("text"));
datasource.update(note);
refreshDisplay();
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
currentNoteId = (int)info.id;
menu.add(0, MENU_DELETE_ID, 0, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == MENU_DELETE_ID)
{
NoteItem note = notesList.get(currentNoteId);
datasource.remove(note);
refreshDisplay();
}
return super.onContextItemSelected(item);
}
}
menu.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_create" android:title="#string/action_create"
android:icon="#drawable/create_note"
android:orderInCategory="100" app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="ifRoom" android:icon="#drawable/ic_settings"/>
</menu>
Use in Your Java File after setContentView
getSupportActionBar().show();
I would like to see you menu xml file for this Activity.
Please try this one, change icon and id.
Notice that there is android:showAsAction="always" NOT app:showAsAction="always":
<item android:id="#+id/Add_New_Note"
android:icon="#drawable/ic_action_new"
android:title="Add new note"
android:showAsAction="always"/>
I had the same problem, hope it will help.
Include this code in your MainActivity
public class MainActivity extends ActionBarActivity {
Inside your onCreate()
ActionBar actionBar = getSupportActionBar();
Give Add and Settings Item in res -> Menu folder.
It will display ActionBar.
Give your extends ListViewActivity in findViewById
add in menu.xml app:showAsAction="ifRoom" like this:
<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=".Main">
<item android:id="#+id/paste" android:title="Paste"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
</menu>
and in main:
//Add the Actions to the Action Bar
#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);
}
//Respond to Action Buttons
#Override
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);
}
}
see this: https://developer.android.com/training/basics/actionbar/adding-buttons.html

Can't add action buttons on action bar

I want to add an ActionButton on the ActionBar, but it is placed on the overflow. What i'm doing wrong?
below is a code file and the main class
///--main_activity_actions.xml--///
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action buton -->
<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>
//--MainActivity.java--//
package com.example.my_first;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
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 MainActivity extends ActionBarActivity {
static final String EXTRA_MESSAGE = "com.example.myfirstapp.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_actions, 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.
switch(item.getItemId()){
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSettings() {
// TODO Auto-generated method stub
}
private void openSearch() {
// TODO Auto-generated method stub
}
public void sendMessage(View view){
//Do something in response to button
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);
}
}
If you want an action to have its own icon on the ActionBar, try changing
android:showAsAction="ifRoom"
to
android:showAsAction="always"
Add this schema to your menu resource:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
Then change the showAsAction attribute:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
Took me days to figure that out. Answer was here.

Action bar icon working fine on one activity not on other

I am using following menu.xml for action bar menu and its working fine on my main activity
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_drawer"
android:orderInCategory="0"
android:title="navigation"
app:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:icon="#drawable/settings"
android:orderInCategory="0"
android:title="Settings"
app:showAsAction="always">
</item>
But when I use same menu.xml on other activity menu icon doesn't appear. Here is a snapshot of my main activity
Now here is a snapshot of my other activity.
Why am I facing this problem, Please let me know your feedback on it.
Here is my onCreateOptionsMenu code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test,menu);
return true;
}
I am using this code for both activity.
Here is the code of my SecondActivity
package com.example.actionbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, 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_refresh) {
Toast.makeText(this, "navigation selected", Toast.LENGTH_SHORT)
.show();
}
if(id == R.id.action_settings){
Toast.makeText(this, "settings selected", Toast.LENGTH_SHORT)
.show();
}
return super.onOptionsItemSelected(item);
}
}
have you used onCreateOptionsMenu and onOptionsItemSelected method in the other activity if no then try it...

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