Search code examples
androidgoogle-glassgoogle-gdk

Show menu in Google Glass


Hi I am having a problem with creating a menu on regular cards in my immersion. This is what I have so far:

I have a CardScrollAdapter that implements OnItemClickListener like this:

private class ExampleCardScrollAdapter extends CardScrollAdapter implements OnItemClickListener {

    @Override
    public int findIdPosition(Object id) {
        return -1;
    }

    @Override
    public int findItemPosition(Object item) {
        return mCards.indexOf(item);
    }

    @Override
    public int getCount() {
        return mCards.size();
    }

    @Override
    public Object getItem(int position) {
        return mCards.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;
        ViewHolder holder;

        if(rowView==null) {
            rowView = mInflater.inflate(R.layout.card_layout, null);
            holder = new ViewHolder();
            holder.mainText = (TextView) rowView.findViewById(R.id.tvMainText);
            holder.fullImage = (ImageView) rowView.findViewById(R.id.fullImage);
            holder.image1=(ImageView) rowView.findViewById(R.id.ivImage1);
            holder.image2 = (ImageView) rowView.findViewById(R.id.ivImage2);
            holder.image3 = (ImageView) rowView.findViewById(R.id.ivImage3);
            holder.llImages = (LinearLayout) rowView.findViewById(R.id.llImages);
            holder.llSecondaryImages = (LinearLayout) rowView.findViewById(R.id.llSecondaryImages);
            rowView.setTag( holder );
        } else holder = (ViewHolder) convertView.getTag();

        CustomCard myObject = (CustomCard) getItem(position);

        if (myObject.getCompanyInfo()!=null) {

            System.out.println(myObject.getImageURL());
            System.out.println(myObject.getCompanyInfo());

            holder.mainText.setText(myObject.getCompanyInfo());
            imageLoader.DisplayImage(myObject.getImageURL(), holder.image1);

            holder.mainText.setVisibility(ImageView.VISIBLE);
            holder.llImages.setVisibility(ImageView.VISIBLE);
            holder.image1.setVisibility(ImageView.VISIBLE);

        } else {
            imageLoader.DisplayImage(myObject.getImageURL(), holder.fullImage);
            holder.llImages.setVisibility(ImageView.VISIBLE);
            holder.fullImage.setVisibility(ImageView.VISIBLE);
        }

        holder.image2.setVisibility(ImageView.GONE);
        holder.image3.setVisibility(ImageView.GONE);
        holder.llSecondaryImages.setVisibility(ImageView.GONE);

        return rowView;
    }

    public class ViewHolder {
        public TextView mainText;
        public ImageView fullImage;
        public ImageView image1;
        public ImageView image2;
        public ImageView image3;
        public LinearLayout llImages;
        public LinearLayout llSecondaryImages;
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        System.out.println("Click" + position);
        onCreateOptionsMenu(null);

    }

}

And when i tap my cards it prints "Click" and the position for the card so that works.

After that I want to run the onCreateOptionsMenu that looks like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.card_menu, menu);
    return true;
}

The problem I am having is understanding how to make onCreateOptionsMenu run? What is the Menu menu that I must supply it with from the onItemClick method instead of null?

// Joakim


Solution

  • FYI I made it like this in the end:

    I implemented a separate Activity:

    public class MenuActivity extends Activity {
    
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        openOptionsMenu();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.card_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection.
        switch (item.getItemId()) {
            case R.id.navigate:
                System.out.println("Click");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public void onOptionsMenuClosed(Menu menu) {
        // Nothing else to do, closing the activity.
        finish();
    }
    

    }

    And then I created the intent:

    checklistIntent = new Intent(MainActivity.this, MenuActivity.class);
    

    And in my OnItemClick i only do:

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startActivity(checklistIntent);         
        }
    

    All the other stuff is the same as before.

    Hope this helps someone in the future.