Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 2, 2016

Android - Popup menu when list item view pressed?

Android - Popup menu when list item view pressed?


i would like to implement a popup menu similar to google's play store as shown below.

enter image description here

so basically from what i understand, i'll need an activity and a layout for this activity with a listview defined in it. i need to create my custom adapter. also, i need to create a list layout would contain the information and a view (with the 3 dots) that will serve as the button to launch the popup menu? the issue that i'm seeing here is that how do i create a listener for this view only and how do i reference the value for that specific list item in the list view.

i don't have any code available yet as i haven't started anything related to this. i'm currently getting info in theory for now but if required i will create a sample code.

thanks.

Answer by Biagio Cannistraro for Android - Popup menu when list item view pressed?


You have to set the Listener of the Button in the getView()-Method of your List-Adapter. In this getView()-Method you assign a Layout to one List-Item. if you have done this, you just have to set the Listener on this View (Button), and handle the onClick() Event.

Answer by Mario Lenci for Android - Popup menu when list item view pressed?


first of all you need to make your custom adapter with a view that has the 3 dots.

then in the getView() or newView() method you set the listener to the 3 dots image.

i think that PopupMenu is what you are looking for, it's is supported since API 11.

if you want to support also earlier version of the API you can use PopupMenu class provided by the support library v7.

the usage is pretty straight forward. you define it with the id of the view you want the menu to show next to, and then you can directly inflate a menu resource there as if it was a common menu.

Answer by sean for Android - Popup menu when list item view pressed?


Not sure if i understand you correctly but you can trigger this method to open a pop up dialog with a listview.

@Override  public Dialog onCreateDialog(Bundle savedInstanceState) {      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());      builder.setTitle("Title if Any");      builder.setItems(R.array.listoptions, new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int itemClicked) {                     String[] option_array = getResources().getStringArray(R.array.listoptions);                       String optionSelected = option_array[itemClicked];             }      });      return builder.create();  }  

See Adding a List

           Install      Add to listview      

Hope this helps.

Answer by MDroid for Android - Popup menu when list item view pressed?


Hiii... You can use like this..

public class MainActivity extends Activity {      ListView listView_Actions;      ArrayList actionsArrayList;      Button btn_ViewPopUp;      ArrayAdapter actionsAdapter;      static final int CUSTOM_DIALOG_ID1 = 1;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          btn_ViewPopUp=(Button) findViewById(R.id.btn_ViewPopUp);            actionsArrayList=new ArrayList();          actionsArrayList.add("Action 1");          actionsArrayList.add("Action 2");      }        @Override      protected void onStart() {          super.onStart();            btn_ViewPopUp.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  showDialog(CUSTOM_DIALOG_ID1);                  actionsAdapter=new MyCustomBaseAdapteradpt(getApplicationContext(),R.layout.list_actions,actionsArrayList);                  listView_Actions.setAdapter(actionsAdapter);              }          });      }        @Override      protected Dialog onCreateDialog(int id) {          Dialog dialog = null;          switch (id) {              case CUSTOM_DIALOG_ID1:                  dialog = new Dialog(MainActivity.this);                  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);                  dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));                  dialog.setContentView(R.layout.list_actions);                  listView_Actions = (ListView) dialog.findViewById(R.id.listView_Actions);                  break;          }          return dialog;      }        class MyCustomBaseAdapterAdapter extends ArrayAdapter       {          public MyCustomBaseAdapterAdapter(Context context, int textViewResourceId, ArrayList actionsArrayList) {              super(context, textViewResourceId,actionsArrayList);          }            @Override          public View getView(int position, View convertView, ViewGroup parent) {              LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);              View v = inflater.inflate(R.layout.action_list_cell, null);              final TextView lblContactAction;              lblContactAction = (TextView) v.findViewById(R.id.txtContactAction);                lblContactAction.append(actionsArrayList.get(position));              return v;          }      }  }  

Now XML files......

action_list_cell.xml

                

list_actions.xml

                                                                          

Answer by LOG_TAG for Android - Popup menu when list item view pressed?


ActionBarCompat List PopupMenu implementation is here (with back port available because it uses ABC)!

enter image description here

You can also get this sample from Github or from SDK (Mr.Morgan commented below)

/sdk/samples/android-19/ui/ActionBarCompat-ListPopupMenu. Make sure to install Samples for SDK under Android 4.4.2 (API 19)

Answer by Anthone for Android - Popup menu when list item view pressed?


Now showDialog is deprecated, use PopupMenu instead And AppCompat PopupMenu f you want to support version before V11

public class MainActivity extends Activity {    Button button1;               @Override             protected void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              setContentView(R.layout.activity_main);                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) {                  Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();                  return true;                 }                });                  popup.show();//showing popup menu               }              });//closing the setOnClickListener method             }        }    

Answer by razzak for Android - Popup menu when list item view pressed?


Using popup menu it's quite simple to create a menu with these three steps:

1 - Add a click listener to the menu button using OnClickListener or as i prefer from the layout xml:

  

2 - Create the menu layout menu_layout.xml:

                  

3 - Create a popup menu, inflate the xml layout and show it:

public void showMenu (View view)  {      PopupMenu menu = new PopupMenu (this, view);      menu.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener ()      {          @Override          public boolean onMenuItemClick (MenuItem item)          {              int id = item.getItemId();              switch (id)              {                  case R.id.item_settings: Log.i (Tag, "settings"); break;                  case R.id.item_about: Log.i (Tag, "about"); break;              }              return true;          }      });      menu.inflate (R.menu.menu_layout);      menu.show();  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.