Search code examples
androidiconspopupwindowpopupmenu

Add text and emoticon (characters) that are selected from a popup window/menu with emoticons


In my app I would like to type a text with regular characters and emoticon short-cuts.

While typing characters from a soft keyboard, I would like to launch a popup menu or popup window with a series of icons/emoticon. Pressing on an icon/emoticon will insert emoticon short-cut characters at the cursor.

It's ok to have text with normal characters and emoticon-short-cut characters. Showing the emoticons in the text is one step further.


Solution

  • The solution I like is:

    1 - Upon clicking a smiley button I start a popup:

    @Override
    public void onClick(View v) {
        switch( v.getId()) {
        case R.id.fragment_log_popup_smileys:
            PopupIcons popup = new PopupIcons( myActivity, new PopupIconResultHandler() {
                @Override
                public void iconClicked( String iconResult) {
                    logTextV.getText().insert( logTextV.getSelectionStart(), iconResult);
                }
             });
            popup.show();
            break;
    

    Any smiley characters are inserted in the text. They could be shown as the same smileys. Easy to do so.

    2 - The popup

    public class PopupIcons implements Serializable {
        Activity myActivity; 
        PopupIconResultHandler myClickHandler; 
        public PopupIcons( final Activity activityContext, PopupIconResultHandler clickHandler) { 
        myActivity = activityContext;
        myClickHandler = clickHandler;
    }
    public void show() {
        Rect rectgle= new Rect();
        Window window= myActivity.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame( rectgle);
        int StatusBarHeight= rectgle.top;
        int contentViewTop= window.findViewById( Window.ID_ANDROID_CONTENT).getTop();
        int TitleBarHeight= contentViewTop - StatusBarHeight;
        Display display = ((WindowManager) myActivity.getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay();
        LinearLayout viewGroup = (LinearLayout) myActivity.findViewById( R.id.popupIconsLinearLayout);
        LayoutInflater layoutInflater = (LayoutInflater) myActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate( R.layout.popup_icons, viewGroup);
    
        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow( myActivity);
        popup.setContentView(layout);
        popup.setFocusable(true);
        popup.setAnimationStyle( R.style.PopupMenu);
        popup.setWidth( FrameLayout.LayoutParams.WRAP_CONTENT);  
        popup.setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);
    
        View.OnClickListener handler = new View.OnClickListener() {
            @Override
            public void onClick( View v) {
                if( myClickHandler != null) {
                    myClickHandler.iconClicked( (String) v.getTag()); 
                }
                popup.dismiss(); 
            }
          };
    
        ImageButton but = (ImageButton) layout.findViewById( R.id.popup_icon_smile); but.setOnClickListener( handler);
        but = (ImageButton) layout.findViewById( R.id.popup_icon_smile_big); but.setOnClickListener( handler);
        but = (ImageButton) layout.findViewById( R.id.popup_icon_smile_cool); but.setOnClickListener( handler);
        // etc ... for all buttons
    
        popup.showAtLocation( layout, Gravity.BOTTOM | Gravity.LEFT, 30 , 30);   
    }
    

    }

    I enjoy this method. Do you have suggestions for improvement?