Toolbar menu does not work anymore - java

I changed from a normal ActionBar to the new Toolbar. My problem is that my old menu doesn't work. When I click on the overflow icon nothing happens. I'm using appcompat v7 but I could not find any solution.
Can someone help me?
Menufunctions:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(R.menu.chatmenu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.onlineliste:
getOnlineList();
return true;
case R.id.settings:
showSettings();
return true;
case R.id.logout:
logout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Chatmenu:
<?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/onlineliste"
android:title="#string/userliste"
app:showAsAction="always">
</item>
<item
android:id="#+id/settings"
android:title="#string/settings"
app:showAsAction="never">
</item>
<item
android:id="#+id/logout"
android:title="#string/logout"
app:showAsAction="never">
</item>
</menu>
Theme
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#428bca</item>
<item name="colorPrimaryDark">#428bca</item>
<item name="android:windowNoTitle">true</item>
<item name="theme">#style/ThemeOverlay.AppCompat.Dark</item>
<item name="windowActionBar">false</item>
</style>
Toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark" />

Oh my godness I got it. Thank you for all your help guys!
The problem was that I had a RelativeLayout which was overlapping the toolbar but I did not see this directly. THe solution was to add
android:layout_below="#+id/toolbar" to the relativ layout and now everything works fine.
Again thanks for all your help guys and sorry!

try this one:
<?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/onlineliste"
android:visible="true"
android:title="#string/userliste"
app:showAsAction="never">
</item>
<item
android:id="#+id/settings"
android:visible="true"
android:title="#string/settings"
app:showAsAction="never">
</item>
<item
android:id="#+id/logout"
android:visible="true"
android:title="#string/logout"
app:showAsAction="never">
</item>
also your theme must have a parent of Theme.AppCompat and your Activity must extends from ActionBarActivity

Probably you need to set your Toolbar as the activity ActionBar, using:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.your_toolbar);
setSupportActionBar(toolbar);

You are not returning true from the onCreateOptionMenu().
The documentation says that
You must return true for the menu to be displayed; if you return false it will not be shown.
Insead of
return super.onCreateOptionsMenu(menu);
which always return false. Return true from onCreateOptionsMenu(menu);
Check out the documentation here.
Source Link

Related

Android action bar options menu item custom selectable background

I'm trying to use the default android action bar with a custom view:
<style name="ActionBar" parent="android:Widget.Material.ActionBar.Solid">
<item name="android:displayOptions">showCustom</item>
<item name="android:customNavigationLayout">#layout/customNavigationLayout</item>
</style>
The options menu contains a single item which should always show with text:
<item
android:id="#+id/action_cancel"
android:showAsAction="always|withText"
android:title="#string/action_cancel" />
Now I'm having the issue with the selectable background, which is still the size of an action icon:
How can I setup the action bar to apply a selectable background which fills the whole box of the item?
You can try setting the android:actionBarItemBackground attribute in styles, like so:
<style name="AppTheme" parent="android:Theme.Material">
...
<item name="android:actionBarItemBackground">?android:selectableItemBackground</item>
</style>
Use one of the following solutions:
Solution 1:
Add <item name="actionButtonStyle">#style/ActionButtonStyle</item> to your base application theme with for exmaple:
<style name="ActionButtonStyle" parent="android:Widget.Material.Button">
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#drawable/button_background</item>
</style>
and
button_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/colorAccent"/>
<item android:state_focused="true"
android:drawable="#color/colorPrimaryDark"/>
<item android:drawable="#color/colorPrimary"/>
</selector>
Solution 2:
Use menuItem.setActionView to apply a custom layout to your menu item as follows:
layout_action_cancel (custom menu item layout):
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/b_action_cancel"
android:gravity="center"
android:background="#drawable/button_background">
</Button>
Then in your onCreateOptionsMenu apply the custom Layout to the menu
item:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
LayoutInflater layoutInflater = getLayoutInflater();
View layout = layoutInflater.inflate(R.layout.layout_action_cancel, null, false);
menu.getItem(0).setActionView(layout);
return super.onCreateOptionsMenu(menu);
}
and in onPrepareOptionsMenu set an OnClick Listener (replacing the
onOptionsItemSelected)
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
View layout = menu.getItem(0).getActionView();
if(layout instanceof Button){
Button b_action_cancel = layout.findViewById(R.id.b_action_cancel);
b_action_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your code
}
});
}
return super.onPrepareOptionsMenu(menu);
}
You can try applying drawable withe selected and normal state at menu item property :
<item
android:id="#+id/action_cancel"
android:showAsAction="always|withText"
android:title="#string/action_cancel"
android:icon="#drawable/mybuttonbackground" />

PreferenceScreen change colors

I have an issue styling the (PreferenceScreen) in my project.
How can I change the colors of of (Title) & (Summary) texts.
No luck after trying for hours with different methods, I must be doing something wrong
SettingsFragment.Java
public class SettingsFragment extends Fragment {
public SettingsFragment() {
// Required empty public constructor
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().getFragmentManager().beginTransaction()
.replace(R.id.flContent, new SettingsPreference())
.commit();
}
public static class SettingsPreference extends PreferenceFragment {
public SettingsPreference() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
}
fragment_setings.xml
<FrameLayout 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:background="?attr/app_background"
android:clickable="true"
android:focusable="true"
tools:context="com.companyv.app.fragments.SettingsFragment">
</FrameLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="pref_key_storage_settings"
android:title="First">
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_key_auto_delete"
android:summary="Sub 1 summary"
android:title="Sub 1" />
<Preference
android:dependency="pref_key_auto_delete"
android:key="pref_key_sms_delete_limit"
android:summary="Sub 2 summary"
android:title="Sub 2" />
<Preference
android:dependency="pref_key_auto_delete"
android:key="pref_key_mms_delete_limit"
android:summary="Sub 3 summary"
android:title="Sub 3" />
</PreferenceCategory>
</PreferenceScreen>
I have done this before by using a custom preference theme and setting the
android:textColor(title color) and android:textColorSecondary(summary color) in the custom theme. For example, black title and white summary:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="preferenceTheme">#style/AppPreferenceTheme</item>
</style>
<!-- Custom Preference Theme -->
<style name="AppPreferenceTheme"
parent="#style/PreferenceThemeOverlay.v14.Material">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
You can rename "AppPreferenceTheme" to whatever name you like.

Android Activity Menu

I wanted to create an option menu which shows up similar to Whatsapp even if the device has its own "hardware" option button. The solution I found online worked just fine until I wanted to add another action to the Bar.
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title=""
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
android:showAsAction="always"
></item>
<item
android:title=""
android:id="#+id/menu_overflow"
android:icon="#mipmap/ic_more_vert_white_48dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
</item>
</menu>
And this in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
With this the app forces the addViewedMovie Action into an Overflow-Menu. Does anyone know how to prevent this?
Try this code and tell me if its work.. you should use app:showAsAction not android:showAsAction also you do not need to use submenu to show 3 dots.. just below code works fine also show 3 dots
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Movie"
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
app:showAsAction="always"/>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
Create a folder name Menu in your res folder.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:icon="#drawable/ic_launcher"
myapp:showAsAction="ifRoom" />
<item android:id="#+id/abc"
android:title="ABC" />
<item android:id="#+id/abc1"
android:title="ABC1" />
<item android:id="#+id/abc2"
android:title="ABC2" />
</menu>
In java file put this:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
return true;
case R.id.abc:
return true;
case R.id.abc1:
return true;
case R.id.abc2;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Simple.
use app instead of android as app is for design support library.
And so, in Gradle build in dependencies, you must have: compile com.android.support:design..... like that. But, I have seen that you already used it below. So,
Just use: app:showAsAction="never" instead of android:showAsAction="always" in your addViewedMovie item.
Remember: never means it will be on overflow menu item. always means is will show in action bar.
If you are using in a fragment, add below line in the onCreate
setHasOptionsMenu(true);
And two more functions
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
Inflate your views in the above method. Listen to the action in the below one.
#Override public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

Implementing multiple selection accross multiple groups in Navigation Drawer

I want to have this kind of selection in my Navigation Drawer. This picture shows the selection which are made initially (by default not by user).
I have implemented this by the following code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Category">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/coaching"
android:icon="#drawable/coaching"
android:title="Coaching"
android:checked="true"/>
<item
android:id="#+id/training"
android:icon="#drawable/training"
android:title="Training"/>
</group>
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="#+id/new_registrations"
android:icon="#drawable/new_registrations"
android:title="New Registrations"
android:checked="true"/>
<item
android:id="#+id/ready_certificates"
android:icon="#drawable/ready_certificates"
android:title="Certificates Ready To Collect"/>
<item
android:id="#+id/allotted_certificates"
android:icon="#drawable/allotted_certificates"
android:title="Certificates Allotted So Far"/>
</group>
But the thing is when I select any item manually then all the previously selected items from both groups get deselected. So I want to validate one selection from group one and one selection from group two. Looking for missing attributes.
Remove android:checkableBehavior="single" from both groups.
Remove android:checked="true" from both "coaching" and "new_registrations" items.
Set android:checkable="true" for each item individually.
Set unique ids for both groups ("#+id/first_group" and "#+id/second_group").
Your activity_main_drawer.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Category">
<menu>
<group android:id="#+id/first_group" >
<item
android:id="#+id/coaching"
android:icon="#drawable/coaching"
android:title="Coaching"
android:checkable="true" />
<item
android:id="#+id/training"
android:icon="#drawable/training"
android:title="Training"
android:checkable="true" />
</group>
</menu>
</item>
<group android:id="#+id/second_group" >
<item
android:id="#+id/new_registrations"
android:icon="#drawable/new_registrations"
android:title="New Registrations"
android:checkable="true" />
<item
android:id="#+id/ready_certificates"
android:icon="#drawable/ready_certificates"
android:title="Certificates Ready To Collect"
android:checkable="true" />
<item
android:id="#+id/allotted_certificates"
android:icon="#drawable/allotted_certificates"
android:title="Certificates Allotted So Far"
android:checkable="true" />
</group>
</menu>
Declare two MenuItem objects in the MainActivity.java and make them checked.
Add the following logic to the onNavigationItemSelected.
Your MainActivity.java should look like this:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
MenuItem prevItemOfFirstGroup;
MenuItem prevItemOfSecondGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ... some code
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
prevItemOfFirstGroup=navigationView.getMenu().findItem(R.id.coaching);
prevItemOfFirstGroup.setChecked(true);
prevItemOfSecondGroup=navigationView.getMenu().findItem(R.id.new_registrations);
prevItemOfSecondGroup.setChecked(true);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int groupId = item.getGroupId();
if (groupId == R.id.first_group) {
if (prevItemOfFirstGroup != null) {
prevItemOfFirstGroup.setChecked(false);
}
prevItemOfFirstGroup = item;
} else if (groupId == R.id.second_group) {
if (prevItemOfSecondGroup != null) {
prevItemOfSecondGroup.setChecked(false);
}
prevItemOfSecondGroup = item;
}
item.setChecked(true);
// Handle navigation view item clicks here.
int id = item.getItemId();
// ... some code
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return false; // IMPORTANT! NOT TRUE!
}
}

How to get a thinner font in options menu android

I've tried making a option menu in my activity, and I succeed. The problem now, is that the text style is bold, and I don't know how to make it normal. I tried setting textStyle, but it had no effect.
option_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/apk/res-auto">
<item android:id="#+id/AppointmentItem"
app:showAsAction="always"
android:icon="#drawable/ic_notifications_white_24dp"
android:title="Appointments"/>
<item android:id="#+id/WeekItem"
app:showAsAction="never"
android:icon="#drawable/ic_today_24dp"
android:title="Week vooruit/achteruit"/>
<item android:id="#+id/LeerlingItem"
app:showAsAction="never"
android:icon="#drawable/ic_person_24dp"
android:title="Leerling veranderen"/>
<item android:id="#+id/InstelllingenItem"
app:showAsAction="never"
android:icon="#drawable/ic_settings_24dp"
android:title="Instellingen"/>
<item android:id="#+id/OverItem"
app:showAsAction="never"
android:icon="#drawable/ic_work_white_24dp"
android:title="Over ons"/>
</menu>
How I create the options menu in my mainactivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu); //your file name
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.AppointmentItem:
return true;
case R.id.WeekItem:
getWeek();
return true;
case R.id.LeerlingItem:
getLeerling();
return true;
case R.id.InstelllingenItem:
getSettings();
return true;
case R.id.OverItem:
getAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How it looks (first) and how I want it (last):
Fixed it by changing "TextAppearance.AppCompat.Widget.ActionBar.Title" to "TextAppearance.AppCompat.Widget.ActionBar.Subtitle".
<style name="MyActionBar.MenuTextStyle" parent="style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/black</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">16dp</item>
</style>
Thanks to #Rotwang for advising me to check the styles.xml

Categories

Resources