0

After Seeing here Disable EditText context menu . I am able to Hide the context menu but unfortunately i am unable to select the text from the edittext. how do i do that i mean select text? Here is my code

import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText=findViewById(R.id.myid);
    editText.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            return true;
        }
    });
    editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            menu.clear();
            MenuInflater inflater=mode.getMenuInflater();
            inflater.inflate(R.menu.mymenu,menu);
            return true;
        }

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

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });

}
}

Here is the xml file

   <?xml version="1.0" encoding="utf-8"?>
   <android.support.constraint.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="pallob.example.com.customedit.MainActivity">

   <EditText
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/myid"
   android:gravity="top|left"
   />
 </android.support.constraint.ConstraintLayout>
Pallob
  • 31
  • 5

2 Answers2

0

change return true to False in onLongClick. it will work mate

Fazan Cheng
  • 866
  • 1
  • 8
  • 13
  • can you post the xml code where you define the edittext? – Fazan Cheng May 02 '18 at 00:14
  • Check the xml file. also i want to mention if i remove MenuInflater inflater=mode.getMenuInflater(); inflater.inflate(R.menu.mymenu,menu); these two lines also the context menu doesn't show – Pallob May 02 '18 at 00:27
  • i need to look at the code where you define menu item as well – Fazan Cheng May 02 '18 at 00:37
  • well i haven't add any item on the menu that's why the context menu is not showing but when i am adding a single or more item on the menu then the text is selected but the context menu is appearing . I want to select text without appearing the context menu hope you understand – Pallob May 02 '18 at 09:19
0

I have found a solution of this problem both hiding the context menu and selecting the text . I have already answered it Here

Pallob
  • 31
  • 5