I have set the theme of my application as NoActionBar and have implemented a appcompatv7 toolbar.
But, the toolbar doesn't show any icon for the items explicitly mentioned in the menu_main.xml file.
Even the overflow icon that is shown by default doesn't appear on the toolbar.
menu_main.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.codesters.materialdesign.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_navigate"
android:orderInCategory="200"
android:title="#string/next"
app:showAsAction="always"
android:icon="#drawable/ic_action_next_item"/>
</menu>
styles.xml file:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="CustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">#FFFF</item>
<item name="android:textColorSecondary">#FFFF</item>
</style>
</resources>
MainActivity file:
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
#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, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
if (id == R.id.action_navigate) {
Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
return super.onOptionsItemSelected(item);
}
}
Write onCreateOptionsMenu(Menu menu) method and inflate the menu you've created in xml.
Then your code should look like.
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(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, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
if (id == R.id.action_navigate) {
Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
return super.onOptionsItemSelected(item);
}
Related
(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.
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
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()
I implemented a Material Design nav drawer using this example. I customize it according to my theme. Everything is working perfectly. Just the last thing I want to customize it's menu icon. I googled it, but have not find any good solution. I want to change its color if possible , or add a custom drawable. Any solution on idea. Thanks in advance
This is my Application theme which i set in menifest
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
And this is my toolbar style
<
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>
My Main activity code
public class Activity_Home extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = Activity_Home.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity__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;
}
//
// if(id == R.id.action_search){
// Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
// return true;
// }
return super.onOptionsItemSelected(item);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
}
}
Add:
getSupportActionBar().setHomeAsUpIndicator(resId);
or
getSupportActionBar().setHomeAsUpIndicator(drawable);
to your Activity's onCreate
Found a sloution myself. It was pretty easy indeed. All you have to do is set a toolbar style. In your style primary color act as a toolbar title and textScondary color act as a menu icon color.
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>
I have a button in my actionbar that hides all the buttons and should show another three but it shows only two of them. The number of items is 7. The buttons's (that have to be showed) showAsAction is ifRoom.
Here is how I hide/unhide my buttons:
private void hideUnhideActionButtons(boolean hide) {
MenuItem item;
Log.d("Item numbers", Integer.toString(audioMenu.size()));
for(int i = 0; i < audioMenu.size(); ++i ) {
if(i < 4)
audioMenu.getItem(i).setVisible(!hide);
else
audioMenu.getItem(i).setVisible(hide);
}
audioMenu.getItem(6).setVisible(true);
}
Here is where I call this method:
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_camera:
...
return true;
case R.id.action_audio:
hideUnhideActionButtons(true);
return true;
case R.id.action_record:
...
return true;
case R.id.action_play:
...
return true;
case R.id.action_return:
hideUnhideActionButtons(false);
return true;
case R.id.action_delete:
...
return true;
case R.id.action_save:
...
return true;
}
return super.onOptionsItemSelected(item);
}
My 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.victwo.NewNoteActivity" >
<item
android:id="#+id/action_save"
android:icon="#drawable/ic_action_save"
android:orderInCategory="100"
android:title="#+string/action_save"
app:showAsAction="always"/>
<item
android:id="#+id/action_delete"
android:icon="#drawable/ic_action_remove"
android:orderInCategory="100"
android:title="#+string/action_delete"
app:showAsAction="always"/>
<item
android:id="#+id/action_camera"
android:icon="#drawable/ic_action_camera"
android:orderInCategory="100"
android:title="#+string/action_camera"
app:showAsAction="always"/>
<item
android:id="#+id/action_audio"
android:icon="#drawable/ic_action_volume_on"
android:orderInCategory="100"
android:title="#+string/action_record"
app:showAsAction="always"/>
<item
android:id="#+id/action_record"
android:icon="#drawable/ic_action_mic"
android:orderInCategory="100"
android:title="#+string/action_record"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_play"
android:icon="#drawable/ic_action_play"
android:orderInCategory="100"
android:title="#+string/action_play"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_return"
android:icon="#drawable/ic_action_back"
android:orderInCategory="100"
android:title="#+string/action_return"
app:showAsAction="ifRoom"/>
</menu>
Can anybody tell me what is the problem?
I see you have a reference to your menu, which is audioMenu. So you can access to your menu items using findItem method and giving their id's instead of indexes.
Example :
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_camera:
...
return true;
case R.id.action_audio:
//hide
actionMode.findItem(R.id.id_1).setVisible(false);
actionMode.findItem(R.id.id_2).setVisible(false);
//show
actionMode.findItem(R.id.id_3).setVisible(true);
actionMode.findItem(R.id.id_4).setVisible(true);
return true;
case R.id.action_record:
...
return true;
}
return super.onOptionsItemSelected(item);
}