I want to create an EditText with the following changes:
Clicking on it will not show the keyboard
After SINGLE, SHORT click it will get into selection mode (When I say selection mode I mean the mode where you can select a section of the text (with two pointers). You can get to this mode by long clicking on the text.)
After text is selected the copy/paste/cut toolbar will not be shown
For the first, I guess I can create an OnTouchListener and return true immediately, but then it will block me from doing the second thing (which I have no idea how to do).
I looked for a command that gets the EditText into selection mode, but all I could find was a way to get the selected text from it...
Thanks!
EDIT: I successfully made 1 and 2, but the toolbar still shows (tried unregisterForContextMenu)
you can use edittex.setCustomSelectionActionModeCallback
setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
this will be block to open contextmenu for edittex
Related
I am attempting to use a popup menu that uses the view of a dynamically added EditText box.
When I do not create the new popup menu inside the onTouch method, the popup closes as expected but, I could not figure out a way to use the view of the touched EditText this way. Instead the popup would show up in the view of the last added EditText.
View.OnTouchListener subjectListener(final EditText editText) {
return new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
popupMenu = new PopupMenu(MainActivity.this, editText);
popupMenu.getMenu().add("works");
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
popupMenu.getMenu().close();
list.get((Integer) editText.getTag()).setText(item.getTitle());
return true;
}
});
return false;
}
};
}
With this code the popup menu shows up exactly where I want it too except it does not close unless I touch another EditText a few times. Which makes me think I am creating multiple popup menu's behind each other or something.
Any ideas?
popupmenu.dismiss();
You can use this to close popup
When I included following code in my Activity file, I got the options menu on the right side with three vertical dots:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
//code
}
}
I need to do following things:
Shift the options menu to the left side
Show 3 horizontal lines instead of 3 dots for options menu
In short, I need options menu like PAYTM app.
http://images.indianexpress.com/2016/12/paytm_big_new.jpg
What is the way to shift the options menu to left and change its appearance?
What you need is a Navigation Drawer.
You can find how to implement it here: https://developer.android.com/training/implementing-navigation/nav-drawer.html
My problem is that I want to change the icon from a MenuItem from Visible to not being Visible, but this ofcourse needs to go via the onOptionsItemSelected. If I call on menu, it gets the MenuItem where is clicked on, while another one needs to be hidden. And I also checked on defining the MenuItem and findViewById, which didn't work because it's no view.
Let me show you a part of my code to make it more clear:
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menu_refresh:
// Stuff
case R.id.menu_settings:
(Somehow point to R.id.menu_refresh).setVisible(false);
}
return super.onOptionsItemSelected(menu);
}
Any ideas?
What I read from the docs, there is a special method for this case: onPrepareOptionsMenu(Menu menu).
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
So that makes it more like this:
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menu_refresh:
// Stuff
case R.id.menu_settings:
showRefresh = false;
//Stuff
}
return super.onOptionsItemSelected(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_refresh).setVisible(showRefresh);
return super.onPrepareOptionsMenu(menu);
}
If you're doing a 2.3 style menu, you don't change it in onOptionsItemSelected. You do it in onPrepareOptionsMenu the next time the menu is launched (by saving whatever data is appropriate to save).
If you have an actionbar, you'll need to call invalidateOptionsMenu on the activity to reload the menu, and do the disabling in the create function.
I have an issue with the action bar on Android 4.2 with ActionBarSherlock. I have added an action bar menu item and it uses setOnMenuItemClickListener to set the buttons action.
MenuItem mapButton = menu.findItem(R.id.show_map);
mapButton.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//Button implementation goes here
}
});
This works fine on Android 2.2 up to Android 4.1 however on Android 4.2 onMenuItemClick does not get called at all.
I have tried adding this
//Handles the map and route button in the options bar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.show_map:
return false;
case R.id.show_route:
return false;
default:
return super.onOptionsItemSelected(item);
}
}
Which does get called on button press but appears to have no effect. I could move the code the button implements into this function but it would require me to make drastic alterations to other parts of the program. Is there some way to get setOnMenuItemClickListener working on Android 4.2?
I'm used to working in J2ME. I wanted to do something on Android, but I just can't get how to replace the command class in the events Android scenario.
Do you want to add commands and want them appear in a built-in menu? When user selects one of them you want a predefined function to be called? If I understand your question right and your problem is the same as above here is the solution:
Android provides a built-in menu and let you create your own menu options. Just like in J2ME as you create some menu options(commands in J2ME) they appear in menu automatically.(The menu that appears when the user hits the menu key on the handset)
To add menu to your Activity override onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ITEM_ID, 0, "First Menu Option");
return true;
}
Then when user selects an item in the menu onMenuItemSelected(int featureId, MenuItem item) callback is invoked. So you can catch your menu option as follows:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case MENU_ITEM_ID:
yourFunction(); //your function for this particular item
return true;
}
return super.onMenuItemSelected(featureId, item);
}
If there were more items(options) in menu, you would catch them in switch by their ids.
hope that helps