Change android drop down menu to a settings button - java

Currently in my app there is a dropdown menu containing one option Settings using this icon in the upper right corner I want to swap it out with this icon and instead of causing a drop down just calling a new view on tap.
This is how i currently handle the settings 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();
Intent intent = new Intent(this, appSets.class);
startActivity(intent);
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

You just set android:showAsAction="always" in your res/menu/filename.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/action_settings"
android:showAsAction="always"
andoid:title="#string/action_settings"
android:icon="#drawable/ic_action_settings"/>
</menu>
and if you are using appcompatv7 library you just need to edit it a little bit
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
app:showAsAction="always"
andoid:title="#string/action_settings"
android:icon="#drawable/ic_action_settings"/>
</menu>

go to res/menu/main.xml and in item <item /> instead android:icon="#drawable/new_pic" use your drawable image

Related

Android Studio | Error "resource not found" for no longer called resource

fist off all the code:
Following lines are in my MainActivity.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();
View view = findViewById(R.id.action_item_two);
//noinspection SimplifiableIfStatement
if (id == R.id.action_item_one) {
// Do thing one
return true;
}
if (id == R.id.action_item_two) {
// Do thing two
return true;
}
return super.onOptionsItemSelected(item);
}
My menu has the file res/menu/main.xml and looks 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_item_one"
android:icon="#drawable/ic_help"
app:showAsAction="always"
android:title="Hilfe">
</item>
<item
android:id="#+id/action_item_two"
android:icon="#drawable/ic_forward_30_48px"
app:showAsAction="always"
android:title="Sekunden">
</item>
</menu>
In an old version of the main menu I used drawable/ic_baseline_pause_24 as icon-source to test the menu and it worked.
After this I changed the icon-sources to the final ones, but the icons where still the old one.
I deleted the old icon source (drawable/ic_baseline_pause_24 and from now on I have the following error when trying to start the app:
[datafile to the app]\app\src\main\res\menu\main.xml:4: AAPT: error: resource drawable/ic_baseline_pause_24 (aka [...]:drawable/ic_baseline_pause_24) not found.
In line 4 of old menu version stood the old icon-source but now it stands nowhere and I have no idea how to solve this error.
I tried to restart everything including (invalidate Caches and restarting Android Studio)
I hope somebody can help!
Thanks
Ok, I found the solution to my problem...
It was i syntax-error in main.xml
It has to be:
<item
android:id="#+id/action_item_one"
android:icon="#drawable/ic_help"
app:showAsAction="always"
android:title="Hilfe"/>
<item
android:id="#+id/action_item_two"
android:icon="#drawable/ic_forward_30_48px"
app:showAsAction="always"
android:title="Sekunden">
</item>
Cause this error the menu did not update

Android Clicks On Popup Menu Items Are Not Shown On The Popup Menu

I click an Icon on the action bar to display a popup menu, then I click a popup menu item and a toast message in my Java code shows that it changes state from false to true. The problem is that when the popup menu is opened again no items are clicked. And clicking a popup menu item always shows that it changes state from false to true.
Does anyone have a code solution for this problem?
Thank you for your help.
Java code:
#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_action, 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_popup) {
View menuItemView = findViewById(R.id.action_popup);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.opt1_name) {
if (item.isChecked()){
item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{
item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
else if (id == R.id.opt2_date) {
if (item.isChecked()){item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
return true;
}
});
popupMenu.show();
}
return super.onOptionsItemSelected(item);
}
menu_action.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=".PlayerActivity">
<item android:id="#+id/action_popup" android:title="Sort Popup" android:icon="#drawable/ic_sort"
android:showAsAction="always" />
<item android:id="#+id/action_settings" android:title="Settings"
app:showAsAction="never" />
<item android:id="#+id/action_help" android:title="Help"
app:showAsAction="never" />
</menu>
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/opt1_name"
android:title="Name" />
<item
android:id="#+id/opt2_date"
android:title="Date" />
</group>
</menu>
Check the error if you examine then you must have one listview in your mainlist.xml file with id as #android:id/list
<ListView
android:id="#android:id/list"
android:layout_height="wrap_content"
android:layout_height="fill_parent"/>

Create an option Menu on Action Bar

I have made an option menu in my application and for some reason it works on one of my smartphones and not the other. The smartphone the option menu works on is android lollipop 5.0 the smartphone it doesn't work on is jellybean 4.1. I'm not sure if this has anything to do with it but I need the option menu to work on both phones. ANyone any ideas on why this is happening
My Code is as fallows
MainActivity
#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_Menu) {
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="saveourcar.soc.MainActivity">
<item
android:id="#+id/action_Menu"
android:orderInCategory="100"
android:title="Menu"
app:showAsAction="never" >
<menu>
<item
android:id="#+id/instructions"
android:title="Instructions"
android:icon="#drawable/rench"/>
<item
android:id="#+id/hotels"
android:title="Hotels"
android:icon="#drawable/hotel"/>
</menu>
</item>
</menu>
Try this:
In onCreateView method:
setHasOptionsMenuEnabled(true);
or this on menu layout:
android:showAsAction="always"

onOptionsItemSelected is not working in android?

I have a menu_item and here is the code:
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.TextView"
android:title="In Stocks"
app:showAsAction="always" />
<item
android:id="#+id/action_checkbox"
app:actionViewClass="android.widget.CheckBox"
android:title="#string/action_check"
app:showAsAction="always"
/>
I am trying to catch Menu_item_clicked_event So, I write following code in Activity file
#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();
switch (id) {
case R.id.action_checkbox:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.action_search:
System.out.println("xas");
default:
return super.onOptionsItemSelected(item);
}
}
When I am pressing the action_search it's working but not for action_checkbox. Why this is happening? I am getting No exception in ADB logs. Is this is the right way to do check for Checkboxes?
app:actionViewClass should be android:actionViewClass="android.widget.CheckBox"
<item
android:id="#+id/action_checkbox"
android:actionViewClass="android.widget.CheckBox"
android:icon="#drawable/unchecked"
android:checkable="true"
android:checked="false"
android:title="#string/action_check"
app:showAsAction="always"
/>
However checkbox will not be visble. Referthis
you could have your own icon and set it android:icon=
And in class
case R.id.action_checkbox:
if(item.isChecked()){
item.setIcon(R.drawable.unchecked);
}
else {
item.setIcon(R.drawable.checked);
}
item.setChecked(!item.isChecked());
break;

Action items/icons not displaying on action bar in Android App

I am trying to have a search action on my action bar. I have set an icon and have showAsAction set to always but it is still always displayed in the overflow menu. How can I make it display on the action bar itself? Thanks!
Activity
public class MainActivity extends ActionBarActivity {
#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.
setTitle("Add");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings",
Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search",
Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML file
<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.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always"
android:title="#string/title_search"/>
</menu>
Either remove
android:actionViewClass="android.support.v7.widget.SearchView"
or delete your case statement for action_search in onOptionsItemSelected. Doing either of those will display the icon.
If you want to use SearchView the callback is handled differently than onOptionsItemSelected.
Documentation here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
Just change app:showAsAction instead of android:showAsAction
<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.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"`enter code here`
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
app:showAsAction="always"
android:title="#string/title_search"/>
</menu>

Categories

Resources