Change Menuitems when Sliding Tab - java

I followed this tutorial to build up a Sliding Tab application :
http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
Now my Question:
How can I change my Menu icons in my actionbar when I change to another Tab ?For example in Tab1 you can see a Search Icon and when you change to Tab2 this gets invisible but there comes a Add icon.
My code till now :
#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);
menu.add("menu_search").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("menu_contact").setIcon(R.drawable.ic_add_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}

You can define menu in Fragment instead of Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_1:
// handling menu item click
break;
}
return super.onOptionsItemSelected(item);
}
Of course each Fragment will have its own menu with corresponding icons.
This is an example (menu_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item_1"
android:showAsAction="ifRoom"
android:title="#string/ic_menu_label"
android:icon="#drawable/ic_menu_icon"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>

Related

Implement Search Bar on Activity Load

I want to load an activity and have the search bar visible right away with a magnifying glass available to click/search based on the term entered. Here is what I want to see when I load the activity:
Code:
public class FlickRActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flick_r);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getApplicationContext(),newText,Toast.LENGTH_SHORT).show();
return false;
}
});
return true;
}
}
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:id="#+id/search"
android:title="#string/hint_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Just call searchItem.expandActionView();

checkbox menu item doesn't work

I have this in my menu_check.xml But when I click on the check nothing happens so the Toast never show... what's the problem? Thanks a lot
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_selecciontodo"
android:title="#string/check"
android:checkable="true"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="always" />
</menu>
and this in my java class
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_check, menu);
checkBox = (CheckBox) menu.findItem(R.id.action_selecciontodo).getActionView();
checkBox.setText("Select all");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_selecciontodo:
CheckBox checkBox= (CheckBox) item.getActionView();
if (checkBox.isChecked())
Toast.makeText(getApplicationContext(), "You selected all", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
No need to app:actionViewClass="android.widget.CheckBox" just do like this
in oncreate options menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
and in onOptionsItemSelected write this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_bookmark:
if (item.isChecked()) {
item.setChecked(false);
Toast.makeText(MainActivity.this, "Un Checked", Toast.LENGTH_SHORT).show();
} else {
item.setChecked(true);
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
and in your menu.xml in menu folder declare menu like this
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/menu_bookmark"
android:checkable="true"
android:title="Bookmark"/>
so when you check uncheck the menu item it show toast and show checkbox checked or unchecked
Try this,
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.stackoverflow.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_check"
android:title="YOUR_TITLE"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
</menu>
MainActivity.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.menu_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
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) {
return true;
}
if (id == R.id.action_check) {
item.setChecked(!item.isChecked());
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
Log.i("cheeck status" , "" + item.isChecked());
return true;
}
return super.onOptionsItemSelected(item);
}
this should work.
Happy coding.!!!

Menus not showing up on older apis

I have an application which works fine on apis 4.0+ but on older apis menus are not showing up. I use the last version of the appcompat library.
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.main, menu);
return true;
}
Fragments :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.interval_timer, menu);
super.onCreateOptionsMenu(menu, inflater);
}
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" >
<item
android:id="#+id/action_interval_training"
android:title="#string/action_interval_training"
app:showAsAction="never"/>
<item
android:id="#+id/action_advanced_session"
android:title="#string/action_advanced_session"
app:showAsAction="never"/>
</menu>
Thank you
EDIT : it seems that it doesnt work only if i set showAsAction="never" ...
On devices with a hardware menu key (such as all Gingerbread devices), that hardware button is still used to trigger the overflow menu - where all items with showAsAction="never" appear.

Android: ActionBarCompat (Support library)

i have a problem.
I'm using support library for my app.
I add some Action items on my ActionBar by XML-file.
From docs:
https://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|collapseActionView" />
...
</menu>
It works. But the item stays on all fragments.
I need use different action items in ActionBar.
I created a few menu-files and tried to use them on different frŠ°gments, but it doesn't work.
I see only the first Action item from class extends ActionBarActivity.
From fragment:
public class Foods extends Fragment implements View.OnClickListener, SearchView.OnQueryTextListener
...
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.showinfo_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
searchView.setQueryHint(getResources().getString(R.string.search));
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
I have just found my mistake.
If you want ActionBar items of fragmets to work, you should add it:
http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
...

Menu inside fragment not getting called

#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
inflater.inflate(R.menu.menuitem, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Below is my onCreateView method, where i am calling
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.layout1, container, false);
}
I don't get the log statements or the menu getting called in my action-bar.
Update: I tried calling this from onCreate method of fragment, yet the menu is not shown.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/section" android:title="#string/section"
android:icon="#drawable/ic_section"
android:showAsAction="always" />
<item android:id="#+id/refresh" android:title="#string/refresh"
android:icon="#drawable/ic_refresh"
android:showAsAction="always" />
<item android:id="#+id/edit_patient" android:title="#string/edit_patient"
android:icon="#drawable/ic_editpatient"
android:showAsAction="always" />
<item android:id="#+id/about" android:title="#string/about"
android:showAsAction="never" />
<item android:id="#+id/help" android:title="#string/help"
android:showAsAction="never" />
<item android:id="#+id/signout" android:title="#string/signout"
android:showAsAction="never" />
</menu>
You'll need to make a call of setHasOptionsMenu(true); from within one of the starting lifecycle methods of the Fragment. Preferably from within onCreate(...).
In a minimalistic case the onCreate method of your Fragment looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Also, calling super.onCreateOptionsMenu(menu, inflater); after you have inflated your custom menu will reset the menu you just have inflated to an empty menu.
So either call:
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menuitem, menu);
}
or:
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
//no super call
inflater.inflate(R.menu.menuitem, menu);
}
Also, if you're testing on a Gingerbread device, the menu might not be displayed if the hosting Activity does not contain a menu item of it's own.

Categories

Resources