4

I want to display a popup whenever a user selects a text in an EditText.

Here is what I want to do: (Screenshot from the Google Docs app)

I would also like to add custom actions in that popup.

Any idea on how to achieve this?

Edit: I specifically want a floating popup just like the one shown in the screenshot, not an ActionMode for there are valuable information that are displayed in the appbar.

I know that I can just make the actionmode push the contents of the screen below it. But I really need a text selection popup.

zepolyerf
  • 1,098
  • 3
  • 16
  • 35

1 Answers1

0

lets look at the default behavior you would get if you set textIsSelectable attribute true for a given textView.

onLongPress on the TextView you will get this enter image description here

Things may look different depending on the API-level of the android,

Now lets customize things a little,

I am assuming you will be using support library v7, so made things for ToolBar.

Do make sure your applications theme has style which has this elements in it.

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeCloseDrawable">@drawable/ic_done_white_24dp</item> // this drawable can be changed depending on what you want

contextual_action_mode_test_one_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/ContextualActionModeTestOneActivity_add"
        android:icon="@android:drawable/ic_input_add"
        android:title="Add"
        android:titleCondensed="Add"
        app:showAsAction="ifRoom|withText" />

    <item
        android:id="@+id/ContextualActionModeTestOneActivity_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        android:titleCondensed="Search"
        app:showAsAction="ifRoom|withText" />

    <item
        android:id="@+id/ContextualActionModeTestOneActivity_sort"
        android:icon="@android:drawable/ic_menu_sort_by_size"
        android:title="Sort"
        android:titleCondensed="Sort"
        app:showAsAction="ifRoom|withText" />
    <item
        android:id="@+id/ContextualActionModeTestOneActivity_help"
        android:icon="@android:drawable/ic_menu_help"
        android:title="Help"
        android:titleCondensed="Help"
        app:showAsAction="ifRoom|withText" />
</menu>

ContextualActionModeTestOneActivity.java

public class ContextualActionModeTestOneActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contextual_action_mode_test_one);
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_custom_toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initializeUI();
    }

    private void initializeUI() {
        textView = (TextView) findViewById(R.id.ContextualActionModeTestOneActivity_textView);
        textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                startActionMode(new ActionBarCallBack());
                return true;
            }
        });


    }


    class ActionBarCallBack implements android.view.ActionMode.Callback{

        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
            mode.setTitle("Do it");
            getMenuInflater().inflate(R.menu.contextual_action_mode_test_one_activity, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
            switch(item.getItemId()){
                case R.id.ContextualActionModeTestOneActivity_add:
                    Toast.makeText(getBaseContext(), "add this text somewhere ", Toast.LENGTH_LONG).show();
                    mode.finish();    // Automatically exists the action mode, when the user selects this action
                    break;
                case R.id.ContextualActionModeTestOneActivity_search:
                    Toast.makeText(getBaseContext(), "search this text ", Toast.LENGTH_LONG).show();
                    break;
                case R.id.ContextualActionModeTestOneActivity_sort:
                    Toast.makeText(getBaseContext(), "sort", Toast.LENGTH_LONG).show();
                    break;
                case R.id.ContextualActionModeTestOneActivity_help:
                    Toast.makeText(getBaseContext(), "help with this", Toast.LENGTH_LONG).show();
                    break;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(android.view.ActionMode mode) {

        }
    }
}

long press on the textView this is what you will see

enter image description here

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30