How to create the mini dialogs (with pictures) - java

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();
}

Related

Pop up shows at the top

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.

Make Static menu to dynamic from arraylist in android

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);
}

Unable to hide activity's options menu on some fragments

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.

My app has an action bar, but no menu.xml. How do I modify my action bar?

Hello and thanks for reading,
When I first made my project, I was prompted by Android Studio to chose a boiler plate. I chose empty activity (the one without FAB and others). Still, my app has an ActionBar, but it just shows the name. Now, I want to modify that action bar and add a menu. My java extends AppCompatActivity, so there is an action bar. However, unlike my prior experiences in eclipse, there is no menu xml to that I can locate.
How can I add one or modify my action by through other means? Can I add one manually?
Thanks!
1) You need to create (or modify if it exist) your menu resources file, /res/menu/main_menu.xml to create the actions.
eg:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:icon="#drawable/ic_action_setting"
android:title="Settings">
</item>
</menu>
2) Override onCreateOptionsMenu() in your activity to allows to inflate actions defined in your XML:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
3) Override onOptionsItemSelected() to react the actions selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
break;
}
return true;
}

Unable to add action bar items in Android Studio

I've been searching for half an hour and none of the solutions for other people are working for me.
Here is what is shows in the preview to the right:
Here is the java code in my activity (I removed the other stuff):
public class PlayGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_game);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_playgame);
myToolbar.setTitle("ChessClock");
myToolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(myToolbar);
}
#Override
public void onBackPressed() { } //Back Button Disabled
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbaroverflow, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pause:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
All the instructions said to make an XML in my 'menu' directory but I didn't have one so I made it (called 'menu' inside of 'res'). The XML 'actionbaroverflow' has this code:
<?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/action_pause"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
And the item is simply not showing up when I run the app. I had an icon but someone said it could have been too big, so I just made it text and it's still not showing up.
Tou need to add to your R.menu.actionbaroverflow
android:icon="#drawable/your_icon"
Will be something like this:
<item
android:id="#+id/action_pause"
android:icon="#drawable/your_icon"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
This works for me fine:
<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:activity=".ProductsNewOrder">
<item android:id="#+id/action_pause"
android:title="Pause"
android:visible="true"
app:showAsAction="always"
android:icon="#drawable/ic_pause"/>
</menu>
and in the activity:
setSupportActionBar(myToolbar);
if (myToolbar!= null) {
myToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_pause: {
//do something
break;
}
}
return true;
}
});
}
and where i inflate the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbaroverflow, menu);
return true;
}
Hope it helps!!!

Categories