Android: I can't make collapseActionView - android

I want to make something like that:
I try to make the popup menu whit the collapseview, y the action bar. but i try all the thing that i found on internet and no solution found. i didnt need suppoart android api less than 15.
this the main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_dialog_email"
android:title="#string/hello_world"
android:showAsAction="always|collapseActionView"
/>
<item android:id="#+id/action_compose"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
<item android:id="#+id/action_compose2"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
</menu>
This my activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar a = getActionBar();
a.setTitle("Mariano");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
}

If I understood correctly you want to open an Android Popup Menu by click on a Button. Then all you have to do is to add this to your MainActivity:
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
if (i == R.id.id1) {
//do something
return true;
}
else if (i == R.id.id2){
//do something
}
else if (i == R.id.id3) {
//do something
return true;
}
else if (i == R.id.id4) {
//do something
return true;
}else {
return onMenuItemClick(item);
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}

Related

open a menu from OnClickListener

I want to open a menu from a OnClickListener
without using the method onCreateOptionsMenu
My code:
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
}
});
Thanks in advance!
I think you want to show/hide a menu item based on actions from users. To do that you must use onCreateOptionsMenu and whenever you want to show/hide the menu item, then call invalidateOptionsMenu (this method will call onCreateOptionsMenu again).
boolean mShowMenu = false;
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.your_menu, menu);
MenuItem item = menu.findItem(R.id.your_menu_item);
item.setVisible(showMenu);
return true;
}
And in your code, when you want to show menu item.
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
mShowMenu = true;
invalidateOptionsMenu();
}
});
And give it a try.
You should need to create menu Interface in xml File Like this
<item
android:id="#+id/settings"
android:title="Setting"
app:showAsAction="never" />
<item
android:id="#+id/my_activity"
android:title="My Activity"
app:showAsAction="always"
android:icon="#android:drawable/btn_radio"/>
After that in the Java code of a particular class You need to create the code like this;
#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();
if (id == R.id.my_activity) {
Intent intent1 = new Intent(this,MyActivity.class);
this.startActivity(intent1);
return true;
}
if (id == R.id.settings) {
Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Hopefully this may resolve your problem

Actionbar popup menu item not visible

I have implemented popup menu in button onClick, with AppCompact theme I can get the menu overflow with empty items in menu. please help me on this
OptionMenuBtn = (ImageButton) v.findViewById(R.id.three_dot);
OptionMenuBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated m// openOptionsMenu();
showPopup(v);
}
});
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.action_menu, popup.getMenu());
popup.show();
}
this should be the Xml file :
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_edit"
android:title="#string/menu_edit" />
<item
android:id="#+id/menu_block"
android:title="#string/menu_deactivate" /></menu>
and the code in activity on your onclick
PopupMenu popupMenu = new PopupMenu(YourActivity.this, view);
popupMenu.setOnMenuItemClickListener(YourActivity.this);
popupMenu.inflate(R.menu.menu_import_export);
popupMenu.show();
and implements :
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
//TODO
return true;
case R.id.menu_block:
//TODO
return true;
}
return false;
}

How to add search and checkbox icon with their functions in android action bar?

I want to add a searchview and checkbox into action bar menu. And this checkbox will be visible if searchview is opened. And in it's opposite case it will be hidden. How I can do this?
I do something below . But it doesnt work correctly. I want hide checkbox (In my notes) when searchview is closed.
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_button"
android:icon="#drawable/ic_icon_search"
android:title="Arama"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/search_in_my_notes_checkbox"
app:showAsAction="ifRoom"
android:title="#string/search_in_my_notes"
android:checkable="true"
android:visible="false"
/>
</menu>
HomeActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.search_button){
MenuItem searchInMyNotesCheckbox = (MenuItem)menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
I found an alternative solution. I will use two checkbox image with checked and unchecked. And I will override onCreateOptionsMenu like this :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = (MenuItem)menu.findItem(R.id.search_button2);
final MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
MenuItemCompat.setOnActionExpandListener(search,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(false);
return true;
}
});
searchInMyNotesCheckbox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() == R.drawable.checked_checkbox){
item.setIcon(R.drawable.unchecked_checkbox);
}
else {
item.setIcon(R.drawable.checked_checkbox);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}

How to set a default icon in menu bar according to the server response in android

I am having list of colleges. If I click one of the college it has go to detail page which has a menu bar with favourite_icon.If I click the favourite_icon that college is stored as favourite in server and that favourite_icon changed as favourite_icon1.If we chech the college detail again after some times that menu bar favourite_icon should be favourite_icon1 if that college is already favoiurited.I am having the API to check whether the college is favourited or not if it favourited the response is like "status=fav" otherwise "status=not_fav".
Here I have added the code for your reference
menu_clg.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
icon_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/vijay"
/>
<item
android:state_selected="false"
android:drawable="#drawable/favourite_icon"
/>
</selector>
activity 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.menu_clg, menu);
mMenu = menu;
return true;
}
// delete the selected event from event list added here
boolean canAddItem = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
if(item.getItemId() == R.id.action_favourite){
invalidateOptionsMenu();
favouriteClg();
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(canAddItem){
menu.getItem(1).setIcon(R.drawable.vijay);
canAddItem = false;
favouriteClg();
}
else{
menu.getItem(1).setIcon(R.drawable.favourite_icon);
canAddItem = true;
favouriteClg();
}
return super.onPrepareOptionsMenu(menu);
}
favourtieclg() method
public void favouriteClg() {
final CollegeMO collegeMO = (CollegeMO) getIntent().getSerializableExtra("CollegeMO");
DatabaseHelper db = new DatabaseHelper(context);
userMO = db.getUserData(1);
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... arg0) {
return favouriteDelegates.addFavourite(userMO, collegeMO, context);
}
#Override
protected void onPostExecute(String userData) {
if (!userData.equals("0") && null != userData) {
UserMO userMO = gson.fromJson(userData, new TypeToken<UserMO>() {
}.getType());
if (userMO.getStatus().equals("success")) {
Toast.makeText(getApplicationContext(), userMO.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}.execute(null, null, null);
}
This is how i achieved it, hope this help. You can also use supportInvalidateOptionsMenu() or InvalidateOptionsMenu() to update changes.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear(); //If you want to clear the menu
inflater.inflate(R.menu.activity_menu, menu);
updateMenuIcon(menu);
}
private void updateMenuIcon(Menu menu) {
if (menu == null) return;
if (condition) {//Check for some condition
changeMenuIcon(menu, R.drawable.fav);
} else {
changeMenuIcon(menu, R.drawable.not_fav);
}
}
private void changeMenuIcon(Menu menu, int resource) {
Drawable newIcon = getResources().getDrawable(resource);
menu.findItem(R.id.menu_fav).setIcon(newIcon);
}

Why opens popupmenu inside action bar

I am working on an timetable app, but I have a strange problem, I created a popupmenu that opens upon clicking an action-bar item.
The popup works, but it opens inside the action bar, I want that it opens in the view below.
My code..
#Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.lists_choice_mode_mulitplue, menu);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.inverse:
showPopupMenu(this.getView());
return true;
}
return false;
}
private void showPopupMenu(View v){
final Activity activity = getSupportActivity();
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(activity,
item.toString(),
Toast.LENGTH_LONG).show();
return true;
}
});
popupMenu.show();
}
My .xml layout files
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/group_popupmenu">
<item android:id="#+id/menu1"
android:title="Popup menu item 1"/>
<item android:id="#+id/menu2"
android:title="Popup menu item 2"/>
<item android:id="#+id/menu3"
android:title="Popup menu item 3"/>
</group>
</menu>
My action bar button .xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/inverse"
android:showAsAction="always|withText"
android:title="Week"
android:titleCondensed="Week" />
</menu>
Yeah it's fixed now!
Its wrong to to showPopupMenu(this.getView());
It should be the id of icon in action bar.. as following..
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.inverse:
showPopupMenu(R.id.inverse);
return true;
}
return false;
}
change showpopupmenu(view v) in
private void showPopupMenu(int id){
final Activity activity = getSupportActivity();
View v = activity.findViewById(id);
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(activity,
item.toString(),
Toast.LENGTH_LONG).show();
return true;
}
});
popupMenu.show();
}
It works now! Thanks for the answers it didn't help me but it is appreciated!
You are inflating your menu layout, and I think that you want to inflate your activity layout.
check this previous question that shows how to inflate a layout:
How to inflate one view with a layout

Categories

Resources