Menu action items goes to overflow menu - java

You will have to forgive me, because I am still pretty new to android development. For my android app, I am staying to create a custom menu and place on the tool bar with the java code in a base activity that is extended by the other activities. But for some reason, the menu buttons are hiding in the overflow menu even though there is more than enough room for them to be showing. I feel like my code is correct, but I need to add a reference to something somewhere else that I am missing. Thanks in advance!
Current toolbar appearance
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
private Toolbar toolBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
toolBar = (Toolbar) this.findViewById(R.id.toolbar);
if (toolBar != null) {
setSupportActionBar(toolBar);
if (getSupportActionBar() != null) {
setDisplayHomeEnabled(true);
}
}
}
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, menu);
return true;
}
protected abstract int getLayoutResource();
public void setDisplayHomeEnabled(boolean b) {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(b);
}
}
#Override
public void setTitle(CharSequence title) {
toolBar.setTitle(title);
}
#Override
public void setTitle(int titleId) {
toolBar.setTitle(titleId);
}
public Toolbar getToolBar() {
return toolBar;
}
}
toolbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</LinearLayout>
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="always"/>
<item
android:id="#+id/menu_search"
android:title="Search"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>

In your main_menu.xml file, add an 'app' tag for showAsAction as I have done below. It worked for me. See if that works for you too.
<?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/menu_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
app:showAsAction="always"/>
<item
android:id="#+id/menu_search"
android:title="Search"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>

Related

Android Toolbar Menu Not Displaying on All Activities

I created a Toolbar and a "3-dot menu" that goes on the Toolbar. In my HomeActivity, the menu displays. However, in other activities, the menu doesn't (but the Toolbar does). I'm not sure what's happening here.
Here is the toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/Theme.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Here is the settings_menu.xml that should show the 3-dot menu.
<?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/item2"
android:title="Item 2"
app:showAsAction="never" />
<item android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="never">
<menu>
<item android:id="#+id/subitem1"
android:title="Sub Item 1"/>
<item android:id="#+id/subitem2"
android:title="Sub Item 2"/>
</menu>
</item>
</menu>
Here is the onCreate() method of HomeActivity, where the menu DOES appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// deactivate the fullscreen mode used by the splash screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tvClients = findViewById(R.id.tvClients);
tvClientList = findViewById(R.id.tvClientList);
tvClientName = findViewById(R.id.tvClientName);
buttonSettings = findViewById(R.id.buttonSettings);
buttonLogout = findViewById(R.id.buttonLogout);
buttonCreateNewClient = findViewById(R.id.buttonCreateNewClient);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
And here is the onCreate() method of another activity, accessed through a button click in the HomeActivity, where the menu button does NOT appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_client);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Create New Client");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
etClientFirstName = findViewById(R.id.etClientFirstName);
etClientLastName = findViewById(R.id.etClientLastName);
etClientAge = findViewById(R.id.etClientAge);
buttonCreateClient = findViewById(R.id.buttonCreateClient);
buttonCreateClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createClient();
}
});
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}
you have to override the onCreateOptionsMenu in every activity you need menu to be shown within
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
// return true so that the menu pop up is opened
return true;
}
check this thread for full details

How to Set Icon to display on the far right in Java

getSupportActionBar().setLogo(R.drawable.addicon);
The code above seems to place my Icon in the Centre . I would like to place to the far right and make it clickable as well . At the moment it the image for the icon is in the project files as a drawable I have not included it in any xml files.
You can set android:layoutDirection="rtl" in your toolbar xml for place it to right and
set below code for clickable :
getSupportActionbar().setDisplayHomeAsUpEnabled(true);
and :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
There are 2 approaches for doing that.
Option Menus
Creating custom toolbar.
With Option Menus
create main.xml under res/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.ioa.MainActivity" >
<item
android:id="#+id/action_addition"
android:icon="#drawable/addicon"
android:orderInCategory="100"
android:title="Addition"
app:showAsAction="always"/>
</menu>
and inside your activity create menu like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
you can perform Action on particular menu item.
#Override
public boolean onOptionsItemSelected(MenuItem Item) {
if (Item.getItemId() == R.id.action_addition) {
// perform action
}
return super.onOptionsItemSelected(paramMenuItem);
}
With custom toolbar
Create your ToolBar like this,
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/add"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
and now you can perform click event of particular ImageView.
ImageView add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do work.
}
});
happy Coding.
You Need 3 Things:
A Custom Toolbar Layout
Include the toolbar in your activity
Set the Logo to the toolbar
Create new Resource Layout:
include_toolbar.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/colorPrimaryDark"
android:id="#+id/include_toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Change Your Activity File: MainActivity.java
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar)findViewById(R.id.include_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.mipmap_ic_launcher);
}
}
And here's the styles.xml:
<!-- language: xml-->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
</resources>

Toolbar icons not appearing in AppCompat Activity

I am trying to add toolbar to my app but somehow it is not working. Neither title not action icons are appearing. Even " : " menu is also not appearing. Here are my codes:
Style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
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="com.example.vaibhav.thirdeye.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/next"
android:title="#string/next"
android:orderInCategory="200"
android:icon="#drawable/next_arrow"
app:showAsAction="always"/>
</menu>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back_splash"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.vaibhav.thirdeye.MainActivity"
tools:showIn="#layout/activity_main"
app:theme="#style/AppTheme.NoActionBar">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
MainActivity.java:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.vaibhav.thirdeye.MESSAGE";
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding Custom toolbar
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.next_arrow);
toolbar.setTitle("ThirdEye");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_settings:
Toast settings = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
settings.show();
case R.id.next:
Toast next = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
next.show();
}
return true;
}
}
app_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/icon_size"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
Output Screenshot:
Any helps????
Remove extra toolbars in your MainActivity's activity_main.xml layout, it will solve your problems.

Toolbar cannot be customized as expected in android

I'm learning Material Design from video tutorial provided by slidenerd.In that they put one next icon on toolbar,everything is working in that tutorial but when i'm implemented it the icon is not appearing.
Here,i'm attaching my files please have a look and help me out at my learning stage.Please!!!
MainActivity.java
package materialtest.example.ritzipsy.mytoolbar5;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
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);
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.id_settings) {
Toast.makeText(this, "You have Clicked Setting menu", Toast.LENGTH_SHORT).show();
}
if (id == R.id.id_navigate) {
Toast.makeText(this, "You have Clicked Next menu", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="materialtest.example.ritzipsy.mytoolbar5.MainActivity">
<include android:id="#+id/app_bar"
layout="#layout/app_bar"></include>
</RelativeLayout>
my_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/tools">
<item
android:id="#+id/id_settings"
android:title="Settings"
app:showAsAction="never">></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.co0m/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</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>
</resources>
Got Output as
Expected output to be
Just change your menu xml as below. The problem is with this line
xmlns:app="http://schemas.android.com/apk/tools"
<?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/id_settings"
android:title="Settings"
app:showAsAction="never"></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>

Android setting custom actionbar

In my project i would to set a custom actionBar (with a custom layout) after a specific action.
I have this simple Activity:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
#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_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//apriImpostazioni ();
return true;
case R.id.information_item:
//apriInformazioni ();
return true;
case R.id.search_item:
apriBarraRicerca ();
System.out.println ("IL BOTTONE RICERCA E' PREMUTO");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void apriBarraRicerca () {
getActionBar ().setCustomView(getLayoutInflater().inflate(R.layout.search_layout, null));
}
}
Where "menu_main_activity2" is this 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="zipangulu.myapplication.MainActivity2">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/information_item"
android:title="Info"
android:icon="#drawable/info"
android:orderInCategory="100"
app:showAsAction="always"/>
<item android:id="#+id/search_item"
android:title="Search"
android:icon="#drawable/search_icon"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
I would, pressing the search_item on the main actionbar, set a custom actionBar whith this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#4584d3">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/back_button"
android:background="#null"
android:src="#drawable/back_icon"/>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:id="#+id/campo_ricerca"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:spinnerMode="dropdown"
android:id="#+id/spinner_anno"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/search_icon"
android:id="#+id/avvia_ricerca"/>
</LinearLayout>
But at runtime I have a NullPointerException in the body of "apriBarraRicerca" method..why?How can I fix this problem?
EDIT: as suggested I replaced "getActionBar ()" with "getSupportActionBar ()", now I don't have any exception but nothing happens.
EDIT2: I added getSupportActionBar().setDisplayShowCustomEnabled(true); and now my custom actionbar is visible but not as i want, look at the following image:
http://bit.ly/1Dc2kGg
The bar is visible but cutted, and are visible also the items of the previous actionBar..
You can try do the same thing with the android.support.v7.widget.ToolBar
Your Layout which you can populate as you desire:
<?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:background="#688A08"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
You have to modify the values->styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
And the in the onCreate() method you do the following:
Toolbar tb = (Toolbar)findViewById(R.id.top_bar);
if(tb != null){
setSupportActionBar(tb);
}
Hope it helps!!!

Categories