I have just created simple pop up with list items but when I click on Grid item it show the pop up at the top of the page like this all I need I want to show this pop up below the item grid
this is my onClick
gridView1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(getActivity(), gridView1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
getActivity(),
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT
).show();
return true;
}
});
popup.show(); //showing popup menu
return true;
}
});
this is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/newCheck"
android:title="New Check"/>
<item
android:id="#+id/settleCheck"
android:title="Settle Check"/>
<item
android:id="#+id/newGuestCheck"
android:title="New guest Check"/>
<item
android:id="#+id/discountCheck"
android:title="Discount Check"/>
<item
android:id="#+id/printCheck"
android:title="Print Check"/>
</menu>
You should change this line
PopupMenu popup = new PopupMenu(getActivity(), gridView1);
to this
PopupMenu popup = new PopupMenu(getActivity(), view);
so it will set the anchor for the menu to the proper element.
Related
I have a popup_menu.xml which has 3 item tags as static, so those 3 item values display in pop up menu, But I have one arraylist which has few values that I want to show in that popup menu.
Just I want to show carnames which is available in markersArray instead static Item values which is available in popup_menu.xml
Array List array
for(int i=0; i<markersArray.size(); i++){
String caname = markersArray.get(i).getCarname();
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/two"
android:title="Two"/>
<item
android:id="#+id/three"
android:title="Three"/>
</menu>
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Click action
System.out.println("Float Icon Clicked");
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, fab);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});
}
Under this line of code
PopupMenu popup = new PopupMenu(MainActivity.this, fab);
Just itterate through your ArrayList and add it to the popup menu like this
for (String s : array) {
popup.getMenu().add(s);
}
I am using options menu in an activity, in that I have inflated menu and onItemSelected for the menu item is done. And in fragment I am hiding the menu items which are not required to show on the fragment.
This is done well, I can see the desired menu items on the fragments. But on some fragments I dont want to show any menu item. So for this I tried to clear the menu, also I tried to setVisible false to all the items, also tried to setHasOptionsMenu(false); , But none is working. I can see all three menu items on that fragment.
Also I tried this answer: Hide/Show Action Bar Option Menu Item for different fragments
From Search merchants fragment in onPreparedOptionsMenu I did menu.setGroupVisible(R.id.main_menu_group, false);
Still not working.
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">
<group
android:id="#+id/main_menu_group">
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="customer"
app:showAsAction="always"
android:icon="#drawable/ic_search"/>
<item
android:id="#+id/action_create"
android:orderInCategory="100"
android:title="customer"
app:showAsAction="always"
android:icon="#drawable/ic_create"/>
<item
android:id="#+id/action_edit"
android:orderInCategory="100"
android:title="customer"
app:showAsAction="always"
android:icon="#drawable/ic_save"/>
</group>
</menu>
Home activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// menu.clear();
getMenuInflater().inflate(R.menu.menu_customer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create:
String favMerchantId = pref.getString("favMerchantId","");
String favKiranaName = pref.getString("favKiranaName","");
if(pref.getString("favMerchantId","").equals("null") && pref.getString("favKiranaName","").equals("null")){
CommonUtils.showAlert(HomeActivity.this,"There is no merchant yet.Please search for merchant.","No Merchant");
}
else {
FragmentManager fragmentManager = getSupportFragmentManager();
AddOrderFragment fragment1 = new AddOrderFragment();
Bundle bundle = new Bundle();
bundle.putString("kiranaName", favKiranaName);
bundle.putString("merchant_id", String.valueOf(favMerchantId));
bundle.putBoolean("ordersFrag", true);
fragment1.setArguments(bundle);
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment1, "RETRIEVE_ADDORDER_FRAGMENT").addToBackStack("B").commit();
}
break;
case R.id.action_edit:
if (CommonUtils.isConnectedToInternet(HomeActivity.this)) {
if(AddOrderFragment.mOrderItemsList.isEmpty())
{
CommonUtils.showAlert(HomeActivity.this,getResources().getString(R.string.listAlert),"List");
// showAlert(getString(R.string.listAlert));
}
else {
Log.e("itemList", String.valueOf(AddOrderFragment.mOrderItemsList.size()));
Gson gson = new Gson();
String toServer = gson.toJson(
Collections.singletonMap("items", AddOrderFragment.mOrderItemsList)
);
String date_time = String.valueOf(CommonUtils.getCurrentDateTime());
new AddOrderAsyncTask(HomeActivity.this,AddOrderFragment.ordersFrag).execute(toServer,pref.getString("api_key",""), pref.getString("sessionUserId",""), AddOrderFragment.mMerchantId,date_time);
}
}
else {
// showAlert(getString(R.string.check_network));
CommonUtils.showAlert(HomeActivity.this,getResources().getString(R.string.check_network),"Check network");
}
break;
case R.id.action_search:
// go to search fragment
FragmentManager fragmentManager = HomeActivity.this.getSupportFragmentManager();
SearchMerchantFragment fragment1 = new SearchMerchantFragment();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment1).addToBackStack("D").commit();
return true;
default:
break;
}
return true;
}
order's fragment to hide some menu items :
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem someMenuItem3 = menu.findItem(R.id.action_create);
someMenuItem3.setVisible(true);
// This does work
MenuItem someMenuItem = menu.findItem(R.id.action_search);
someMenuItem.setVisible(false);
MenuItem someMenuItem1 = menu.findItem(R.id.action_edit);
someMenuItem1.setVisible(false);
}
Search merchant fragment : In this I want to hide the menu, or all the menu items.
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem someMenuItem3 = menu.findItem(R.id.action_create);
someMenuItem3.setVisible(false);
// This does work
MenuItem someMenuItem = menu.findItem(R.id.action_search);
someMenuItem.setVisible(false);
MenuItem someMenuItem1 = menu.findItem(R.id.action_edit);
someMenuItem1.setVisible(false);
}
How can I hide this menu?
Please help.. Thank you..
you can try this,
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
}
and
setHasOptionsMenu(true); in onCreate of fragment.
I've seen these dialogs around certain apps but I haven't been able to figure out how to show / create them. Am I missing something obvious? Thanks for your help!
You can create PopupMenu.
First create popup_menu.xml file in res/menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity">
<item
android:id="#+id/action_copy"
android:orderInCategory="100"
android:title="#string/action_copy"/>
<item
android:id="#+id/action_forvard"
android:orderInCategory="110"
android:title="#string/action_forvard"/>
</menu>
Then implement PopupMenu inside onClick() method of onClickListener of your view:
#Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(MainActivity.this, view);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_copy:
//your code here
break;
case R.id.action_forvard:
//your code here
break;
}
return true;
}
});
popup.show();
}
I have a code sample got from the internet and the code goes like this
ListView lv1;
lv1 = (ListView) findViewById(R.id.custom_list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// print toast
}
});
Well I want to show context menu on list item click and I don't know how to do that.
You can use PopupMenu.
Create popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_action1"
android:icon="#drawable/menu_action1"
android:title="#string/menu_action1" />
...
</menu>
and add this to onItemClick:
PopupMenu popup = new PopupMenu(this, v);
// this = your activity
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
In addition your Activity should implements OnMenuItemClickListener so you can get menu click callback.
I'm trying to make the actionbar menu (onCreateOptionsMenu) open ONLY on a long-click. How would I achieve this? At the moment I have it working for just a single short press/click using the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// TODO: Only onlongclick should the menu be revealed
getMenuInflater().inflate(R.menu.my_menu_id, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
open_home();
return true;
case R.id.menu_how_to:
open_how_to();
return true;
case R.id.menu_rate:
open_rate();
return true;
case R.id.menu_about:
open_about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'd like the menu to ONLY open for a long click (sort of like a hidden/trick feature). I've looked into onLongClickListener but can't get it to work. After the menu is opened it can behave normally (no more longclicks needed). Thanks for your help! Really appreciate it.
This is not possible. LongClick on the ActionBar buttons is not overridable.
It will always show a toast with the name of the button you long pressed. This is a help and discovery feature.
Okay so there's 2 answers to this depending on whether you would like icons in the menu:
Create a context menu, however each row of the menu cannot have icons next to it. This is the simpler method.
Create an alert dialog with a menu. This is much more complex but you can add icons and make the menu much more customisable.
I personally prefer method 2 because of the icons. I'll run through these methods below. First I'll just setup the action bar and button for the menu.
Sp first create an image button to open the menu somewhere. Also open up the custom action bar. This code is for BOTH methods:
// This goes somewhere in the activity's onCreate
ImageButton menu_home_button = (ImageButton) findViewById(R.id.action_bar_menu_home);
registerForContextMenu(menu_home_button); //ONLY USE THIS FOR METHOD1
menu_home_button.setLongClickable(true);
menu_home_button.setClickable(true);
// Custom action bar
actionBar = getActionBar();
actionBar.setCustomView(R.layout.my_custom_actionbar);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
My ImageButton was overlayed on top of the action bar on the right and looked like this in my_custom_actionbar.xml:
<ImageButton
android:background="?android:selectableItemBackground"
android:id="#+id/action_bar_menu_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menu_icon"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="4dp"
android:clickable="true"
android:focusable="true"
android:longClickable="true" />
--------------------------------------
METHOD 1:
Add this to onCreate in WebViewActivity.java:
// Do the long press stuff
menu_home_button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Long Press works...", Toast.LENGTH_SHORT).show();
// Do whatever you want on the longclick
}
});
The following code goes where onCreateOptionsMenu and onOptionsItemSelected would go (at the end of the java class -- mine was called WebViewActivity.java).
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu_id, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.menu_home:
open_home();
return true;
case R.id.menu_how_to:
open_how_to();
return true;
case R.id.menu_rate:
open_rate();
return true;
case R.id.menu_about:
open_about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then you create your menu in my_menu_id.xml. Including icons in the XML won't work so don't bother trying:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.yourapp.WebViewActivity" >
<item
android:id="#+id/menu_home"
android:title="#string/menu_home"
android:showAsAction="never"/>
<item
android:id="#+id/menu_how_to"
android:title="#string/menu_how_to"
android:showAsAction="never"/>
<item
android:id="#+id/menu_rate"
android:title="#string/menu_rate"
android:showAsAction="never"/>
<item
android:id="#+id/menu_about"
android:title="#string/menu_about"
android:showAsAction="never"/>
</menu>
-----------------------------------------
METHOD 2:
Add this to onCreate in WebViewActivity.java:
menu_home_button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Menu text
final String[] items = {"Home", "How To", "Rate", "About"};
// Menu Icons in Drawable
final Drawable[] item_icons = {
getResources().getDrawable(R.drawable.home_icon),
getResources().getDrawable(R.drawable.how_to_icon),
getResources().getDrawable(R.drawable.rate_icon),
getResources().getDrawable(R.drawable.about_icon),
};
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.custom_menu_dialog, items) {
ViewHolder holder;
class ViewHolder {
ImageView icon;
TextView title;
}
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.custom_menu_dialog, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
// view already defined, retrieve view holder
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(items[position]);
holder.icon.setImageDrawable(item_icons[position]);
return convertView;
}
};
Toast.makeText(getApplicationContext(), "Long Press works...", Toast.LENGTH_SHORT).show();
AlertDialog.Builder menu_dialog = new AlertDialog.Builder(WebViewActivity.this);
menu_dialog.setTitle(getResources().getString("My Menu Name");
menu_dialog.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
//Toast.makeText(WebViewActivity.this, "You selected: " + items[item], Toast.LENGTH_LONG).show();
switch (item) {
case 0:
open_home();
break;
case 1: // HOW TO
open_how_to();
break;
case 2:
open_rate();
break;
case 3: // ABOUT
open_about();
break;
default: // Do Case 0
open_home();
break;
}
dialog.dismiss();
}
});
AlertDialog alert = menu_dialog.create();
alert.show();
return true;
}
});
Finally here is the custom_menu_dialog.xml file with the base info for the text and icon of the menu:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:paddingLeft="16dp"/>
<TextView
android:id="#+id/title"
android:text=""
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff6f6f6"
android:textSize="18sp"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"/>
</LinearLayout>
Hopw this helps at least one other person out. It certainly works for me.