Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of long-press photo pop-up right-click menu in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to implement the right-click menu function of long-press photos in Android. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

achieve

Change the layout to LinearLayout and set it to vertical via android:orientation="vertical">.

Then add an ImageView and set the id property and image source.

Then go to Activity, first override onCreateContextMenu method in activity, this method can add new menu and add menu items

//Overwrite onCreateContextMenu menu in activity, add option value to menu @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add("Favorite"); menu. add("Report"); }

Then register the long press event in the onCreate method to the menu and open the menu.

@Override protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_long_click); //Register the long press event in the menu and open the menu ImageView imageView =(ImageView) findViewById (R.id.image); imageView.setOnLongClickListener (new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { //Register Menu registerForContextMenu(v); //Open Menu openContextMenu(v); return true; } }); }

Complete sample code

package com.badao.relativelayouttest;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.ContextMenu;import android.view.View;import android.widget.ImageView;public class LongClickActivity extends AppCompatActivity { @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_long_click); //Register the long press event in the menu and open the menu ImageView imageView =(ImageView) findViewById (R.id.image); imageView.setOnLongClickListener (new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { //Register Menu registerForContextMenu(v); //Open Menu openContextMenu(v); return true; } });} //Overwrite onCreateContextMenu menu in activity, add option value to menu @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add("Favorite"); menu. add("Report"); }}

Thank you for reading! About "Android how to achieve long press photo pop-up right-click menu function" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report