Android adding action buttons problems - java

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

Related

I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click

I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click. I don't know why and searched for information but it doesn't help me.
This my XML code profile__detail_menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:icon="#drawable/ic_baseline_more_vert_24"
android:title="">
<menu>
<item
android:id="#+id/change_name"
android:title="Change name"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
</item>
</menu>
This my Java code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile__detail_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
Toast.makeText(Profile.this,"Something", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.change_name:
Toast.makeText(this, "change_name selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.new_photo:
Toast.makeText(this, "new_photo selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.log_out:
Toast.makeText(this, "log_out selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing which I feel is wrong is your menu XML file. For some reason you have a menu tag inside a menu tag. So there is sort of a sub menu if you get my point? But you're not inflating that actually.
Modify the XML as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/change_name"
android:title="Change name"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
I have updated your menu as per you code. If you need, add more items to it.

How do i use my items in my Menu?

I have create a menu in the action bar but I don't know how to use the items inside of it as a button.
That's my menu 'xml' code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="#drawable/ic_add_circle_outline_black_24dp"
android:title=""
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</menu>
</item>
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
That's what i have in java:
public class MyMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
}
// "Creating" my menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
How I use the items I just declare?
For example the "Exit" option
I think it is the most basic thing although I don't know how to reach the item as a button.. Or its already define it self as a button ?
I would like to get an example and explanation.
you are using menu item inside the item, which is not the correct way to group item in menu.
Try this xml to generate your menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/my_menu" android:checkableBehavior="single">
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</group >
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
For receiving events of the click on menu:
#override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
// action needed
return true;
case R.id.item2:
// action needed
return true;
}
}
Use this
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exitMenu:
//Your Logic
return true;
}
}
Use onOptionsItemSelected Method.
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.exitMenu:
//code here
return true;
}
return(super.onOptionsItemSelected(item));
}
Use switch case to identify the ids of menu items.

Android menuItem setChecked doesn't work

I have this menu filters.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">
<group android:checkableBehavior="single" android:id="#+id/menuGroup_1" >
<item android:id="#+id/a_1" android:title="a1" />
<item android:id="#+id/a_2" android:title="a2" />
</group>
<group android:checkableBehavior="single" android:id="#+id/menuGroup_2" >
<item android:id="#+id/b_1" android:title="b1" />
<item android:id="#+id/b_2" android:title="b2" />
</group>
</menu>
and here are the methods in the .java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.filters, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
item.setChecked(!item.isChecked());
return true;
}
This works fine checkableBehavior="single", but the item doesn't uncheck when I press. Still checked. (item.isChecked()->true)
I try to edit the .xml:
<item android:id="#+id/..." android:checkable = "true" />
Now when a select any item they check/uncheck correcltry,
but this checkableBehavior="single" doesn't work. In other words, I can check two items of the same group.
you should let onOptionsItemSelected return true;

Adding icons to the ActionBar on Android

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.

how to force 3 dots in actionBar menu?

I have the following menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_totalCerdit"
android:showAsAction="always"
android:title="-100"/>
<item
android:id="#+id/actions_saved"
android:icon="#android:drawable/btn_star"
android:showAsAction="always"
android:title="#string/action_settings"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_full_text"
android:showAsAction="never"
android:title="#string/action_full_list"/>
<item
android:id="#+id/action_saved_text"
android:showAsAction="never"
android:title="#string/action_favorites_list"/>
<item
android:id="#+id/logout"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/log_out"/>
</menu>
When deploying on Samsung Note2 The action Bar shows all items that don't have android:showAsAction="never". The ones with that attributes are show when pressing samsung built-in left bottom menu button.
When deploring on Nexus4 I see all items with android:showAsAction="never" as sub menus of "3 dots" item.
how can I make these 3 dots appear and behave in note2 the same as in nexus4?
In Compatibility Definition Document, Google said that Android 4.4+ device SHOULD NOT implement a dedicated physical button for the Menu function. So later device would be fine.
But it seems like NoteII makes a built in menu button for Overflow in Action bar (3 dots menu button). If you really want to do it on NoteII, there is an old hack that I found:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
// presumably, not relevant
}
Try this:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return true;
}
Add this to all your Activity classes. Now you will see the same thing on both the Note 2 and the Nexus 4.
Just implement this method
#Override
public boolean onCreateOptionsMenu(Menu menu) {present.
getMenuInflater().inflate(R.menu.yourfile.xml, menu);
return true;
}

Categories