customizing android toolbar programmatically - android

I want to use android v7.toolbar in my project with fragments. So i did something like below in my main activity xml. Because i dont want to add all fragments xml to android.support.v7.widget.Toolbar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</LinearLayout>
i am adding fragments with programmatically. And in some fragments i have to change right side of the toolbar. For example in some fragments i have to use just 1 right menu icon and another fragments 2. How can i achieve it?
Also is there a way to change icon resource which is in right side of the toolbar from fragments?
Thanks,

You can create menu.xml and put all your menu items in it. Set all the items visibility to false. This hides everything.
Then in your fragment's onCreate set setHasOptionsMenu(true), this will allow you to override onCreateOptionsMenu(Menu m, MenuInflater inflater).
In this method you can do menu.findItem(id.of.item).setVisible(true/false).
Examples:
menu.xml:
<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"
tools:context="com.sample.app.MainActivity">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="101"
app:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:visible="false"/>
<item
android:id="#+id/action_edit_account"
android:icon="#drawable/ic_action_edit"
android:orderInCategory="102"
app:showAsAction="never"
android:title="#string/action_edit_account"
android:visible="false"/>
<item
android:id="#+id/action_enable_offline_token"
android:orderInCategory="105"
app:showAsAction="never"
android:title="#string/action_enable_offline_token"
android:visible="false"/>
<item
android:id="#+id/action_disable_offline_token"
android:orderInCategory="105"
app:showAsAction="never"
android:title="#string/action_disable_offline_token"
android:visible="false"/>
<item
android:id="#+id/action_save"
android:icon="#drawable/ic_action_save"
android:orderInCategory="106"
app:showAsAction="ifRoom"
android:title="#string/action_save"
android:visible="false"/>
</menu>
Fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.action_refresh).setVisible(false);
menu.findItem(R.id.action_save).setVisible(true);
}
To change icons you can just get the menu and do a findItem and setIcon.

Related

How to add an extra menu to android toolbar in Kotlin

The question is clear! I need to add another menu beside of the main menu in toolbar. I have created the status.xml menu file 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/online"
android:orderInCategory="10"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/busy"
android:orderInCategory="20"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/invisible"
android:orderInCategory="30"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
and added a clickable TextView to toolbar like this:
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
... />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
Now I need to add it separately to the toolbar, but in code below:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
menuInflater.inflate(R.menu.status, menu)
return true
}
It add all of items to main menu. Any help will be appreciated.
If this menus have to be separate that appear depending on some kind of condition you can use onPrepareOptionsMenu.
Check this question and developer guide on menus
Android: Multiple Option Menus in one Activity
https://developer.android.com/guide/topics/ui/menus

Get subview of loaded view (group inside a drawer)

I have a drawer with actions in a group, file a list of files I want to dynamically put in another group. My main activity has this layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
Then the loaded NavigationView is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/test_id">
<group android:checkableBehavior="single" android:id="#+id/group_file_list">
<item
android:id="#+id/file1"
android:icon="#drawable/ic_menu_gallery"
android:title="File 1"/>
...
</group>
<item android:title="Actions">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share"/>
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send"/>
</menu>
</item>
</menu>
When I try to get the group where the files are to later add the Items dynamically, all of these return null:
findViewById(R.id.group_file_list); // returns null
View navView = findViewById(R.id.nav_view); // works
navView.findViewById(R.id.group_file_list); // returns null
navView.findViewById(R.id.test_id); // returns null
And yes, I'm calling this after setContentView(). I also tried inside onStart()
Any ideas?
Use this to add items to your drawer:
NavigationView navView = findViewById(R.id.nav_view);
navView.getMenu().add(...);
Or, to add to a group, do this:
navView.getMenu().addSubMenu(Menu.NONE, Menu.NONE, Menu.NONE, groupName).add(...);
See NavigationView and Menu
Those are menu items and not views. You get them by using the options menu
Menu optionsMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// store the menu to var when creating options menu
optionsMenu = menu;
}
Then you can find the item you want
MenuItem item = optionsMenu.findItem(R.id.test_id);

Fragments and ActionBar

I have 4 fragments which have different Actionbar (some items) but they have the same dropdown navigation. I've tried to covered some items but the spinner has been moved to the left. I need it on the right.
screen
My code in one of the fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.action_gps).setVisible(false);
menu.findItem(R.id.action_filter).setVisible(false);
}
My menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/spinner"
android:layout_gravity="left"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="always" />
<item
android:id="#+id/action_search"
android:icon="#drawable/m_glass"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/funnel"
app:showAsAction="always|collapseActionView" />
<item
android:id="#+id/action_gps"
android:icon="#drawable/gps"
app:showAsAction="always|collapseActionView" />

cannot change menu background color

I have a problem with a background color of my action menu items. As you can see below I tried to set the same background color as for the toolbar. But they are still different. I use different attributes -- background and itemBackground -- but they both don't work.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/BackgroundToolbarGrey"
android:contentInsetEnd="0dp"
android:contentInsetStart="0dp"
android:itemBackground= "#color/BackgroundToolbarGrey">
<android.support.v7.widget.ActionMenuView
android:id="#+id/mainMenu"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="left"
android:background="#color/BackgroundToolbarGrey"
android:itemBackground= "#color/BackgroundToolbarGrey"/>
</android.support.v7.widget.Toolbar>
And this is my menu.xml file where I also tried to set color:
<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=".MyActivity"
android:background = "#color/BackgroundToolbarGrey">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:background = "#color/BackgroundToolbarGrey"
android:itemBackground= "#color/BackgroundToolbarGrey">
</item>
</menu>
I also tried to set it programmatically, but it doesn't help
mainMenu= (ActionMenuView) findViewById(R.id.mainMenu);
mainMenu.setBackgroundColor(getResources().getColor(R.color.BackgroundToolbarGrey));
What can I do wrong?
You should set a PopupTheme for your ActionMenuView.
java code
mActionMenuView = (ActionMenuView) findViewById(R.id.action_menu_view);
mActionMenuView.setPopupTheme(R.style.OverFlowMenuTheme);
style.xml
<style name="OverFlowMenuTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:itemBackground">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/black</item>
<item name="overlapAnchor">false</item>
</style>
Try setting the menu item's properties in onCreateOptionsMenu.
Something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
View mainMenu = menu.findViewById(R.id.mainMenu);
mainMenu.setBackgroundColor(getResources().getColor(R.color.BackgroundToolbarGrey));
return true;
}

showAsAction = "always" is ignored in Toolbar

After switching to toolbar there is a problem with menu icons. Although I set for a menu item android:showAsAction="always" it does not show the icon, I can only find it clicking on the popup icon.
This is myActivity
public class myActivity extends AppCompatActivity{
.........
public void onCreate(....){
.............
Toolbar toolbar = (Toolbar) findViewById(....);
setSupportActionBar(toolbar);
}
............
public boolean onCreateOptionsMenu(Menu menu{
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
.............
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/settings"
android:icon="#drawable/settings"
android:title="settings"
android:showAsAction="always"
/>
<item
android:id="#+id/help"
android:icon="#drawable/help"
android:title="help"
android:showAsAction="never"
/>
</menu>
Both settings and help icons are only in popup menu. So how to show settings icon on toolbar?
Replace android:showAsAction with app:showAsAction. You will also need to add xmlns:app="http://schemas.android.com/apk/res-auto" alongside your existing xmlns item in your root element.
With AppCompat there is a little change. If you are running lint it will complain about it. Type 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/someId"
android:title="#string/someText"
app:showAsAction="always"/>
</menu>
You need to declare the "app" namespace and reference it.

Categories

Resources