I'm trying to create a Toolbar with the android.support.v7.widget.toolbar but when i try t add an item, it will not show on the toolbar:
Toolbar in activity_main.xml:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
main_activity:
on the onCreate:
Toolbar my_tbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(my_tbar);
out of onCreate:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
res/menu/main_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/apk/res-auto">
<item
android:title="RefreshButton"
android:id="#+id/refresh"
android:icon="#drawable/refresh_icon"
app:showAsAction="always"
/>
</menu>
the refresh_icon is created by me because in the #drawable/i didn't found the ic_menu_refresh
Why the button is not shown?
Thank you
It is not showing because your not infalting the menu layout. Before calling onOptionsItemSelected() you need to inflate the layout like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
just add this method in your activity, and it will work fine.
Related
I want to make it so that the search view inside my toolbar takes all the available space. As of now, it doesn't, instead, it leaves a slither of unused space right next to the back button. I want it to take up more of that space.
Here is a picture:
Here is my Menu XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ticker_searchview"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/tickersearch"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
</menu>
Here is my Toolbar XML code:
<?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"
android:background="#color/White"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Here is the relevant java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tickersearch_menu, menu);
SearchView tickerSearchView = (SearchView) menu.findItem(R.id.ticker_searchview).getActionView();
tickerSearchView.setQueryHint("Ticker Symbol or Name");
tickerSearchView.setIconifiedByDefault(false);
tickerSearchView.setMaxWidth(Integer.MAX_VALUE);
TextView tickerSearchViewTextView = tickerSearchView.findViewById(androidx.appcompat.R.id.search_src_text);
tickerSearchViewTextView.setTypeface(font);
tickerSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
final Intent toStockInfo = new Intent(Search.this, stockinfo.class);
#Override
public boolean onQueryTextSubmit(String query) {
toStockInfo.putExtra("STOCKTICKER", query.toUpperCase());
startActivity(toStockInfo);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
newSearchUpdateRecyclerContent(newText.toUpperCase());
return true;
}
});
return true;
}
What is your toolbar XML file?
Do this in your toolbar.xml
android:layout_width="match_parent"
This question has been asked a number of times, but I cannot seem to hide the 3 dots icon.
There is no action_settings menu item in menu_main.xml, and yet the settings icon appears none the less.
I can't hide it in onPrepareOptionsMenu because there is no id for the icon
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.menu_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
My main activity lay out begins:
<RelativeLayout 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">
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_default"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_actionbar">
<LinearLayout
....
and layout toolbar_default is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
style="#style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="#dimen/abc_action_bar_default_height_material"/>
and menu_main.xml is
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
</menu>
and ToolBarStyle
<style name="ToolBarStyle" parent="">
<item name="popupTheme">#style/ThemeOverlay.AppCompat</item>
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="CustomPrefTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:checkboxStyle">#style/AppTheme.Preference.Checkbox</item>
</style>
Set the menu dynamically to your toolbar
Toolbar yourToolBar = (Toolbar) findViewById(R.id.my_toobar);
setSupportActionBar(yourToolBar);
yourToolBar.inflateMenu(R.menu.search_menu);
yourToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.search) {
// handle search logic here
}
return true;
}
});
Instead of
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.menu_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
You should probably do
// This will hide the overflow menu icon
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
From here https://stackoverflow.com/a/12253892/4858147 try hiding R.id.action_settings
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.action_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
I am using this sample for my app. This sample doesn't have an action bar.
This is the layout of activity in which I want to add an action bar with options menu:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="bottom"
android:background="#000"
tools:context="com.example.android.camera2basic.CameraActivity" />
</LinearLayout>
So, LinearLayout and a FrameLayout inside of it as a container for fragment. In Main Activity I have implemented onCreateOptionsMenu and added setSupportActionBar((Toolbar)findViewById(R.id.my_toolbar)); but actionbar with the menu isn't appearing.
How can I add an ActionBar with menus to this activity?
Create a main_menu.xml in your menu folder
<item
android:id="#+id/menu1"
android:title="Option 1" />
<item
android:id="#+id/menu2"
android:title="Optiion 2" />
Add this in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
Toast.makeText(this, "Clicked Menu 1", Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(this, "Clicked Menu 2", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Set your Activity Theme to NoActionBar and add this 2 lines of Code in your Activity onCreate method.
Add ToolBar in your Layout.
ToolBar toolbar = findViewById ();
setSupportActionBar(toolbar);
I want to add 2 or 3 icons on action Bar in android app. I already took the empty activity and added the toolbar. I also set the Icon at left side. Now i want to add another two icons on it. But there is no Menu folder in my project directory structure. So any one tell me how i can do this all with proper guidelines?
My code is here :
My activity file
public class ActionBarActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_bar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.left_nav);
getSupportActionBar().setTitle("");
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
my .xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="#ffffff"
android:fitsSystemWindows="true"
tools:context="firstapp.vaibhav.com.firstapp.ActionBarActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.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" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Screen shot of my project directory structure
1. Create a menu folder in your existing resource res folder. (Ex. .../res/menu)
2. Create a main.xml file in menu folder. (Ex. .../res/menu/main.xml)
main.xml
<?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/action_item_one"
android:title="Camera"
android:icon="#drawable/ic_menu_camera"
app:showAsAction="always" />
<item
android:id="#+id/action_item_two"
android:title="Send"
android:icon="#drawable/ic_menu_send"
app:showAsAction="always" />
</menu>
3. In your activity, Override onCreateOptionsMenu() and onOptionsItemSelected() to work with option menus.
ActionBarActivity.java
#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_camera) {
// Do something
return true;
}
if (id == R.id.action_send) {
// Do something
return true;
}
return super.onOptionsItemSelected(item);
}
OUTPUT
Hope this will help~
create menu.xml with item 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/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />-->
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_autorenew"
android:title="Search"/>
<item
android:id="#+id/action_search"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_search"
android:title="Search"/>
</menu>
and use it in activity
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// MenuInflater inflater1 = getActivity().getMenuInflater();
inflater.inflate(R.menu.cartmenu, menu);
return ;
}
in your res/menu/menu_main.xml:
add
<?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/icon_id"
android:visible="true"
android:title="#string/icon_name"
android:icon="#drawable/your_image"
app:showAsAction="always">
</item>
</menu>
in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// return true so that the menu pop up is opened
return true;
}
To access your menu item in activity add:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.your_item_id) {
// your code
return true;
}
return super.onOptionsItemSelected(item);
}
You can use showAsAction option found at item in menu resource file.
1) If you want to add popup menu then write app:showAsAction="never"
2) If you want to add icons as an action (multiple icons in actionbar) then write app:showAsAction="always"
Using this Code,
Layout :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
/>
</RelativeLayout>
ActionBarActivity :
final Toolbar ToolB = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(ToolB);
ToolB.setTitle("ToolBar Test");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The Output is just a blue empty rectangle, it does not react to Touch Event, it's like a Picture. I also get a Menu by pressing The Menu Button (which is working), but there is no Three Dots at top right, nothing.
What am I missing or have got wrong ?
Thanks
If you want to go to the previous activity when press the home button you have to override the method "onOptionsItemSelected(MenuItem item)" on the activity. Example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
For the settings button (those three dots) you have to create a menu (file) like this:
<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">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
and inflate it on the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
I hope these examples to help you.