I need to get the id of the 3 dot menu on action bar.I need it to use
showcaseview on it to show a tutorial to the user.
If it is not possible,then is there any way to set showcaseView target
as 3dot menu image?
Assuming your Activity is AppCompatActivity:
Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
if (toolbar != null) {
// 0 - title
// 1 - options menu
View menuLayout = toolbar.getChildAt(1);
if (menuLayout instanceof ViewGroup) {
// index here depends on actual toolbar layout
View menu3dots = ((ViewGroup)menuLayout).getChildAt(0);
}
}
it has no id; you must inflate the view to create the three dots. You also need another xml file defining the menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
The 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=".My_Activity">
<item android:id="#+id/more_info" android:title="#string/moreInfo"
android:orderInCategory="100" app:showAsAction="never" />
Related
I develop android application and I want to create action placed on action bar that is visible as icon. I added a drawable with resolution of 18x18px and created menu with one item, which is my action, as shown below:
<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=".RelationsActivity">
<item
android:id="#+id/action_show_profile"
android:icon="#drawable/profile_icon_16"
android:title="#string/action_show_profile"
app:showAsAction="always" />
As you can see the showAsAction attribute is set to "always" and it is still not working. I tried with different AppThemes and nothing helped. There surely is enough space for the icon cuz action bar title is disabled:
getActionBar().setDisplayShowTitleEnabled(false);
The action is present in the overflow dropdown as if there is no space for the icon.
use this
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
And this method too
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.YOUR_MENU_FILE, menu);
return true;
}
implements AppCompatCallback
and use this code inside oncreate method
//let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
AppCompatDelegate delegate = AppCompatDelegate.create(this, this);
//we need to call the onCreate() of the AppCompatDelegate
delegate.onCreate(savedInstanceState);
//we use the delegate to inflate the layout
//delegate.setContentView(R.layout.activity_sync_contact);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu);// icon of navigation
toolbar.setTitle("Inbox");
delegate.setSupportActionBar(toolbar);
I have a Toolbar within my app which I would like to modify it contents dynamically, during the app execution (in other words, on run-time).
For example, the app is capable of taking and previewing photos; once that photos are previewed, the user is able to select some photos and perform a sending action to a server. I want also to make the user able to delete photos once that some of them are selected and for doing that I would like that the "delete" item on the action bar (identifiable via the trash icon) will be visible only when one or more photos are selected.
Is this possible to do?
If yes, how?
In other words and, more generically, I want to control items (visibility) in the toolbar, setting the code as they will be "visible" only when some conditions are "true" (in the example above, when photos are selected or, in a different example, when user is logged) and invisible when they are "false" (when user isn't logged).
For now I just need to "remove" (or make invisible) items in the Toolbar, but it will be useful also to know if is possible to add items in the toolbar on run-time.
I'm adding some code which can help to understand the problem.
app_bar.xml file in "/res/layout", which graphically defines the Toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="#style/ToolbarTheme" />
menu_resources.xml file, which defines the Toolbar items
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "User favourite function", should appear as action button if possible -->
<item
android:id="#+id/action_app_icon"
android:icon="#mipmap/ic_launcher"
android:title="#string/action_bar_app_icon"
app:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_delete"
android:icon="#drawable/trash"
android:title="#string/action_bar_delete"
app:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:icon="#drawable/settings"
android:title="#string/action_bar_settings"
app:showAsAction="never"/>
<!-- About, should always be in the overflow -->
<item
android:id="#+id/about"
android:icon="#android:drawable/ic_dialog_info"
app:showAsAction="never"
android:title="#string/action_bar_about"/>
Part of the activity in which the toolbar is
public class myClass extends AppCompatActivity implements View.OnClickListener {
// Instantiating a toolbar
private Toolbar toolbar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_class);
// Adding toolbar to the activity
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
// The method that creates an options menu
#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_resource, menu);
// This make the delete item invisible
menu.findItem(R.id.action_delete).setVisible(false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// Perform the settings action
return true;
case R.id.about:
// Perform the about
return true;
case R.id.action_delete:
deletePhotos();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public static void manageSelection(Boolean state, int position){
if (photoArray.getNumberSelected() == 0) {
// disable the trash icon and its functionality;
} else {
// enable the trash icon with its functionality;
}
}
// This method allows to deleteItems images to the array
public void deletePhotos() {
//code for deleting photos
}
}
Thanks for your time.
Try this code in your activity please:
public class ActivityClass extends AppCompatActivity {
MenuItem menuItem; // Make global toolbar's menuItem
.
.
.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
menuItem = menu.findItem(R.id.toolbar_action_button) // Your toolbar´s button ID and save it in your global menuItem
return super.onCreateOptionsMenu(menu);
}
public void showMenuItem(){
menuItem.setVisible(true); // Call this method in runtime when you need show it
}
public void hideMenuItem(){
menuItem.setVisible(false); // Call this method in runtime when you need hide it
}
[EDIT]
Thanks to Alessandro Iudicone´s comment we have an alternative way to get the toolbar´s menu too without the global MenuItem but only global Toolbar instance:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
return super.onCreateOptionsMenu(menu);
}
public void showMenuItem(){
toolbar.getMenu().findItem(R.id.toolbar_action_button).setVisible(true);
}
public void hideMenuItem(){
toolbar.getMenu().findItem(R.id.toolbar_action_button).setVisible(false);
}
Hope it helps :)
For hiding the icon:
toolbar.findViewById(R.id.menu_item_id).setVisibility(View.INVISIBLE);
For adding a view to the toolbar:
TextView textView = new TextView(MainActivity.this);
textView.setText("Hello World");
toolbar.addView(textView);
This only works for views on the toolbar itself, not for the overflow menu. You'll probably have to use code found in this stack overflow answer if you want to mess with the overflow menu: Android Toolbar overflow menu view id
By default android have an option to enable BACK ARROW on the left of the toolbar using
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
same like i need TICK MARK on the right of the toolbar, is there any method to enable TICK MARK in android by default.
First download the "done" icon as png from the materials icon library
Copy the icons to your projects drawable folder.
Then create a file in res/menu called menu_example.xml
<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:icon="#drawable/ic_done_white_24dp"
android:title="#string/action_done"
app:showAsAction="always" />
</menu>
Then add the following code to your Activity.
#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_example, menu);
return true;
}
To have a onClick listener for the tick you can implement this:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
toast.show();
return true;
}
I have an action bar with a drawer on the left. I want to add another menu on the right side of the action bar. It can be three dots or a button or anything else. Is there any way to do this?
Thanks :)
For achieving 3 dots menu in your actionBar, in your activity (which extends AppCompatActivity or ActionBarActivity), you override the creation of options menu like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
Where this R.menu.your_menuis a resource item present in your res/menu folder. One sample menu resource file content
<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="your_package.your_activity">
<item
android:id="#+id/action_edit"
android:orderInCategory="100"
android:title="#string/action_edit"
app:showAsAction="collapseActionView"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="101"
android:title="#string/action_settings"
app:showAsAction="collapseActionView"/>
</menu>
This will show a menu with both the options collapsed by default. To listen for clicks on these menu items, you override onOptionsItemSelected and perform the necessary action
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Do something
...
return true;
case R.id.action_edit:
//Do something else
...
return true;
}
return super.onOptionsItemSelected(item);
}
You can create a layout for your ActionBar and then use something like the following in your activity:
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
And in your R.layout.custom_action_bar create the buttons you need.
I hope you can give me a hand on this one.
I have an Action bar item with a default icon set.(android:icon="#drawable/ic_action_volume_on")
but the thing is that i need to change it somehow programmatically at my method "onOptionsItemSelected" and since I recently started with Android I'm a bit lost...
First you have to save a menu instance on the onCreateOptionsMenu:
private Menu menu;
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu
...
}
Then, you can create the following method:
private void setItemView(int itemId, int layoutId) {
if (menu != null) {
final Menuitem myItem = menu.findItem(itemId);
if (myItem != null) {
MenuItemCompat.setActionView(myItem, layoutId);
}
}
}
It replaces the current icon of your item by a view. For example if you want to display a ProgressBar you can create this layout (let's name it item_custom_view.xml):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="56dp"
android:layout_height="wrap_content"
android:minWidth="56dp" >
<ProgressBar
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center" />
</FrameLayout>
Finally, in your Activity, just call the setItemView method. Put in parameters the id of your item and the id of your layout (in this case R.layout.item_custom_view).
You can do this simply by defining
private Menu menu;
and in onCreateOptionMenu callback assign value to menu and get menu inflater like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu=menu;
getMenuInflater().inflate(R.menu.menu_bar, menu);
return true;
}
And finally you can write this line to change icon in your preferred method
menu.findItem(R.id.item_id).setIcon(R.drawable.your_icon);