Add options in an option menu of main toolbar in Android studio - android

I am new at android and am trying to create a customized toolbar. I was wondering if there is any way to add options to the settings menu(3 dots) when it is clicked.

First you need to add an item in menu_main.xml(res>menu) file like.
<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="in.amzbizsol.vts.MainActivity">
<item
android:id="#+id/action_changepassword"
android:orderInCategory="1"
android:title="Change Password"
app:showAsAction="never" />
<item
android:id="#+id/action_logout"
android:orderInCategory="2"
android:title="Logout"
app:showAsAction="never" />
then in your MainActivity create something like
#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_changepassword) {
Intent intent=new Intent(getApplicationContext(),ChangePassword.class);
startActivity(intent);
return true;
}else if(id==R.id.action_logout){
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}

Please Check this documentation - https://developer.android.com/guide/topics/ui/menus.html

Related

Settings in actionbar

(1) I want to remove the icon of settings which was created after when i make a new project with a Navigation Drawer Activity. (2) And also i want change the title of item to red color i an activity_main_drawer.xml. (3) The last one, i want also remove title of action bar.
The screenshot of action bar:
The code of menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item android:title="Asosiy menyu"
>
<menu>
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_star_black_24dp"
android:title="Yoqtirganlar" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_format_list_bulleted_black_24dp"
android:title="So'ngi ko'rilganlar" />
</menu>
</item>
</group>
</menu>
The title of navigation menu:
Your first question ans is remove below code from 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.main2, 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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
then after not show setting..
In string.xml in remove
<string name="app_name">RafDemo</string> // here give empty no show title in actionbar.

Creating Material Design SearchView which is always collapsed

I am working on a material design app and i want to implement a ToolBar which needs a collapsed SearchView and a settings button. I've successfully implemented the SearchView and settings menu items. But the problem is that I want the SearchView to be collapsed by default.Now it is collapsed on icon click only.
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.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_settings) {
return true;
}
else if (id == R.id.action_search) {
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
return super.onOptionsItemSelected(item);
}
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="com.example.navigationdrawer.MainActivity" >
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
You have to use
app:showAsAction="always|collapseActionView"
If you want it to always show, but be expandable as explained in the action views training and the Providing Search with SearchView video

Menu is not showing in custom toolbar

I am trying to use custom menu in my android app. I want to add some menu items.
For the purpose, I add following in my 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=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="always" />
<item
android:id="#+id/contact"
android:icon="#drawable/ic_star"
android:orderInCategory="2000"
android:title="#string/Rate"
app:showAsAction="always" />
</menu>
And in MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onPrepareOptionsMenu(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)
{
Toast.makeText(this,"Hello from settings",Toast.LENGTH_LONG).show();
return true;
}
if ( id == R.id.contact)
{
startActivity(new Intent(this,ContactUs.class));
return true;
}
return super.onOptionsItemSelected(item);
}
But, it is not working at all.
I tried some solutions on SO, but none of them worked.
e.g. this
Please help me to solve this.
You have to set your toolbar like
setActionBar(toolbar);
in onCreate()

Action bar Icon isnt working in API 21

i used to use the action bar icon but when i do someting on it on API 21 nothing is display no icons i mean only the 3 dot not the icon i want whereas i the api 14 it's working :
menu :
<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.gweltaz.idee.Historique">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100"
android:icon="#drawable/sup"
app:showAsAction="ifRoom" />
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_historique, 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_settings) {
db.execSQL("DELETE FROM story2");
return true;
}
return super.onOptionsItemSelected(item);
}
Try change:
app:showAsAction="ifRoom" />
to
app:showAsAction="always"/>

how to show icon on action bar in android

I want to implement action bar in my application i write some code in activity and add xml file .
but icon on action bar is not show . I try much but i cant know
that what is wrong with this code
Java code is here:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.testmenu, menu);
return true;
}
/*public boolean onOptionsItemSelected1(MenuItem item)
{
switch(item.getItemId()){
case R.id.register:
finish();
}
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_settings) {
}
*/
switch (item.getItemId()) {
case R.id.register:
finish();
break;
default:
break;
}
return true;
// return super.onOptionsItemSelected1(item);
}
and xml code is
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/register"
android:title="#string/action_settings"
android:showAsAction="alway"
android:orderInCategory="100"
android:icon="#drawable/register"
/>
</menu>
There's a typo in your testmenu.xml
android:showAsAction="alway"
change to:
android:showAsAction="always"
android:showAsAction="alway"
alway -> always
UPDATE
try this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/register"
android:title="#string/action_settings"
android:showAsAction="always"
myapp:showAsAction="always"
android:visible="true"
android:icon="#drawable/register" />
</menu>

Categories

Resources