2

Attention: This question is different and is not an exact duplicate. This states that the image needs to pop up without a dialog. No one even bothered going through this question to see if it was a duplicate which it is not.

I am trying to open an Image View from my context menu, however, I do not know how to do this. When an item in the Context Menu is clicked, I need an Image View to appear. Is it even possible to open an Image View from a Context Menu? People have recommended using a dialog instead, but I want to know if this specific way is possible. Thank you in advance. Here is my code:

package com.carouseldemo.main;

import android.app.Activity;


import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;

import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselAdapter.AdapterContextMenuInfo;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.controls.CarouselItem;

public class MainActivity extends Activity {


    @Override
      public boolean onContextItemSelected(MenuItem item) {
            int id = item.getItemId(); // you know which menu is clicked
            ImageView popup = new ImageView(this);
        //set the image 
            popup.setImageResource(R.drawable.cat); // or setImageBitmap ,
                                                            // or setImageUti etc
        // this will make the ImageView fill the entire content area. If you don't want this wrap it with another layout
            ((FrameLayout) findViewById(android.R.id.content)).addView(popup);
            return super.onContextItemSelected(item);
        }


    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.testmenu1, menu);
    // your code...
    return true;} // true if you want the menu to be displayed; false otherwise

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // Just prepare ourself for unexpected arguments...

        if (menuInfo == null) {
            return;
        }
        if (!(menuInfo instanceof AdapterContextMenuInfo)) {
            return;
        }


        AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo;

        switch (mi.position) {
        case 0:

            menu.add("Product 1");

            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            break;
        case 1:
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            break;
        case 2:
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            break;
        case 3:
            menu.add("mouse menu");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            break;
        case 4:
            menu.add("panda menu");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            break;
        case 5:
            menu.add("rabbit menu");
            menu.add("Product 1");
            menu.add("Product 1");
            menu.add("Product 1");
            break;
        default:
            break;
        }
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        final Carousel carousel = (Carousel) findViewById(R.id.carousel);
        carousel.setOnItemClickListener(new OnItemClickListener() {



            public void onItemClick(CarouselAdapter<?> parent, View view,
                    int position, long id) {
                CarouselItem item = (CarouselItem) parent.getChildAt(position);

                // HERE: Force context menu
                carousel.showContextMenuForChild(item);
                Toast.makeText(
                        MainActivity.this,
                        String.format("%s has been clicked",
                                ((CarouselItem) parent.getChildAt(position))
                                        .getName()), Toast.LENGTH_SHORT).show();







            }

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {

                // TODO Auto-generated method stub





            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onContextItemSelected(MenuItem item) {
                // TODO Auto-generated method stub
                return false;
            }});



        carousel.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(CarouselAdapter<?> parent, View view,
                    int position, long id) {

                final TextView txt = (TextView) (findViewById(R.id.selected_item));

                switch (position) {
                case 0:
                    txt.setText("The cat (Felis catus), also known as the domestic cat or housecat to distinguish it from other felids and felines, is a small, usually furry, domesticated, carnivorous mammal that is valued by humans for its companionship and for its ability to hunt vermin and household pests. Cats have been associated with humans for at least 9,500 years, and are currently the most popular pet in the world. Owing to their close association with humans, cats are now found almost everywhere in the world.");
                    break;
                case 1:
                    txt.setText("The hippopotamus (Hippopotamus amphibius), or hippo, from the ancient Greek for \"river horse\" (), is a large, mostly herbivorous mammal in sub-Saharan Africa, and one of only two extant species in the family Hippopotamidae (the other is the Pygmy Hippopotamus.) After the elephant, the hippopotamus is the third largest land mammal and the heaviest extant artiodactyl.");
                    break;
                case 2:
                    txt.setText("A monkey is a primate, either an Old World monkey or a New World monkey. There are about 260 known living species of monkey. Many are arboreal, although there are species that live primarily on the ground, such as baboons. Monkeys are generally considered to be intelligent. Unlike apes, monkeys usually have tails. Tailless monkeys may be called \"apes\", incorrectly according to modern usage; thus the tailless Barbary macaque is called the \"Barbary ape\".");
                    break;
                case 3:
                    txt.setText("A mouse (plural: mice) is a small mammal belonging to the order of rodents. The best known mouse species is the common house mouse (Mus musculus). It is also a popular pet. In some places, certain kinds of field mice are also common. This rodent is eaten by large birds such as hawks and eagles. They are known to invade homes for food and occasionally shelter.");
                    break;
                case 4:
                    txt.setText("The giant panda, or panda (Ailuropoda melanoleuca, literally meaning \"black and white cat-foot\") is a bear native to central-western and south western China.[4] It is easily recognized by its large, distinctive black patches around the eyes, over the ears, and across its round body. Though it belongs to the order Carnivora, the panda's diet is 99% bamboo.");
                    break;
                case 5:
                    txt.setText("Rabbits (or, colloquially, bunnies) are small mammals in the family Leporidae of the order Lagomorpha, found in several parts of the world. There are eight different genera in the family classified as rabbits, including the European rabbit (Oryctolagus cuniculus), cottontail rabbits (genus Sylvilagus; 13 species), and the Amami rabbit (Pentalagus furnessi, an endangered species on Amami, Japan)");

                    break;
                }

            }

            public void onNothingSelected(CarouselAdapter<?> parent) {
            }

        });

        registerForContextMenu(carousel);
    }

}
Lulu R
  • 143
  • 1
  • 3
  • 11
  • Open an `ImageView` where? – user Feb 06 '13 at 09:25
  • I want an image to pop up after clicking on an item in the Context Menu. – Lulu R Feb 06 '13 at 16:37
  • No. I said in this question I want to know how to do it without a dialog. I want to know if it is possible to open an image from a context menu. The question before hand was not answered at all, it just recommended a different way. I need to know if this specific way is possible and how. – Lulu R Feb 07 '13 at 04:24
  • 1
    This is not a duplicate. Wow, I can't believe some people don't even bother reading here. – Lulu R Feb 07 '13 at 14:03
  • Your question is a duplicate. In this one you ask about hot to popup an `ImageView` without using a dialog, and in the initial question you ask the same, about how to popup the ImageView(and when you receive an answer to use a dialog you say that isn't the way you wanted, so in that question you were already looking for a solution without the use of a dialog). Not to mention that half of the question's text is identical + the code posted. In `onContextItemSelected` create the `ImageView`, assign its image and add it to the view with the id `android.R.id.content`. – user Feb 07 '13 at 14:31
  • The question was not answered properly before. So I asked again because I need to get this done, there were plenty of views but no answers. Do you mean something like this? public void onContextItemSelected (ContextMenu menu, View v, ContextMenuInfo menuInfo){ ImageView imageView = new ImageView(context); ImageView.setImage(R.drawable.cat); imageView.setVisibility(View.VISIBLE); } – Lulu R Feb 07 '13 at 14:55
  • Even though (context) cannot be resolved to a variable and the method setImage is undefined or the type ImageView. – Lulu R Feb 07 '13 at 14:57
  • Something like this https://gist.github.com/luksprog/4731481 . You should have a reference to the Context as you are in an activity. – user Feb 07 '13 at 15:07
  • It says FrameLayout cannot be resolved to a type. Thank you for helping me so far. I have updated the code in my question with the code you have given me. Am I not placing the code in the right spot? – Lulu R Feb 07 '13 at 15:23
  • 1
    It's in the right spot. Did you imported the class From the `FrameLayout`(import android.widget.FrameLayout;) like you did for the TextView, ImageView etc? – user Feb 07 '13 at 15:46
  • Thank you. Now there's just two more things please. How can I make different images pop up when I select other items in the context menu? Right now, one picture shows up when I select one item in the context menu. How can I get a different picture to show up when I click different item in the context menu? The second thing is how to close the image view, as the picture is stuck on the screen and won't go away. – Lulu R Feb 07 '13 at 15:54

1 Answers1

1

How to popup the ImageView I've shown you in the comment posted.

How can I make different images pop up when I select other items in the context menu? Right now, one picture shows up when I select one item in the context menu. How can I get a different picture to show up when I click different item in the context menu?

Don't add your menu options using this menu.add("Product 1");. Instead use this option:

menu.add(Menu.NONE, 1, Menu.None, "menu title");

Instead of the 1 value, the second parameter, pass a unique value for each menu item that you add to the ContextMenu. In the onContextItemSelected method you can then use item.getItemId(); to get the id of the menu that was clicked and based on that you'll know which item was clicked. Assign the image based on that item's id.

The second thing is how to close the image view, as the picture is stuck on the screen and won't go away.

Assign to the ImageView that you create an OnClickListener or OnTouchListener and in the listener's method remove the ImageView:

ImageView popup = new ImageView(this);    
popup.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
         ((FrameLayout) findViewById(android.R.id.content)).removeView(v);
     } 
});
user
  • 86,916
  • 18
  • 197
  • 190
  • I have the idea of what you're saying but cannot implement it right. https://gist.github.com/nixjust/c64c0ecc6d49506dde93 That is the code I have now. I cannot assign the id of the menu properly, and I'm not sure if everything is in the right place. The second image I am trying to open is rabbit, from menu id 2. Can you take a look and show me what I'm doing wrong? – Lulu R Feb 07 '13 at 16:46
  • The original code before I changed it can always be viewed in my question, just for reference if it helps. – Lulu R Feb 07 '13 at 16:49
  • Update: The image does disappear when clicked. So that is working. But I cannot assign the menu id properly. – Lulu R Feb 07 '13 at 16:53
  • 1
    @LuluR I've edited the gist, check it out https://gist.github.com/luksprog/5a6dd3e7f901a5d32cb5 – user Feb 07 '13 at 17:52
  • Thank you! You've really helped me. I've been searching for a solution for over a week. Again, thank you for all your help. – Lulu R Feb 07 '13 at 18:48