I am developing an App in android, i want to disable copy paste buttons onLongClick,
I am using the following code:
edittext.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;
}
});
But it is showing error as
"The method setCustomSelectionActionModeCallback(ActionMode.Callback) in the type TextView is not applicable for the arguments (new ActionMode.Callback(){})".
I am searching for hours to get the solution. Please provide me solution.
Add
import android.view.ActionMode.Callback;
to your imports
OR
if you have another Callback class already imported change
edittext.setCustomSelectionActionModeCallback(new Callback() {
to
edittext.setCustomSelectionActionModeCallback(new android.view.ActionMode.Callback() {
You are getting error in above method because the method is included in API Level 13+ and your current compilation version may be set to lower.
Right Click on project -> properties -> "Android Tab" -> select api level 13 or greater.
If you wants backward support, you can try these tricks.
1)
OnLongClickListener mOnLongClickListener = new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//since nothing is in here, nothing will happen.
return true;
}
};
2)
edtPassword.setLongClickable(false);
3) IN XML
android:longClickable="false"
NOTE :
The method you are trying will only works with API level 13+. But these tricks will work with lower aloso
I found one hack. It's working charm:
OnLongClickListener mOnLongClickListener = new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//since nothing is in here, nothing will happen.
setEnabled(false);
setEnabled(true);
return false;
}
};
Do same for onClick event of OnClickListener.
Related
I want to disable copy/paste option of my Entry control in Xamarin forms application. I am using custom renderer for that. The current solution is working in all the other devices apart from Redmi Note 8. This is my renderer code.
class MyEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.CustomSelectionActionModeCallback = new Callback();
Control.CustomInsertionActionModeCallback = new Callback();
Console.WriteLine("CustomSelectionActionModeCallback");
Control.SetTextIsSelectable(false);
Control.LongClickable = false;
}
}
}
public class Callback : Java.Lang.Object, ActionMode.ICallback
{
public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
{
Console.WriteLine("OnActionItemClicked");
return true;
}
public bool OnCreateActionMode(ActionMode mode, IMenu menu)
{
Console.WriteLine("OnCreateActionMode");
menu.Clear();
return false;
}
public void OnDestroyActionMode(ActionMode mode) {
Console.WriteLine("OnDestroyActionMode");
}
public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
{
Console.WriteLine("OnPrepareActionMode");
menu.Clear();
menu.Close();
return true;
}
}
}
So long click is getting disabled in Redmi Note 8 but still there is blue color dot which appears. On click of that it still shows me copy/paste option. It is happening only in Redmi note 8. And in my code no other callback is getting hit other than OnDestroyActionMode and I'm not able to execute menu.Clear. In other devices that option is getting disabled by using this code
Control.SetTextIsSelectable(false);
This is how it is getting shown in Redmi Note 8 device.
I have referred these links because this issue is quite similar to mine but it didn't help.
Disable EditText context menu
EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event
As you can see in the image the paste option is getting shown in the device. I have no clue how to fix this bug any suggestions?
I have found two solutions for this. One is clearing the clipboard and other is setting the variation of input as visible password. Both these solutions serves my purpose. For clearing the clipboard you can use the following code in your renderer
var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
clipboardManager.ClearPrimaryClip();
And for setting the variation of input as visible password you can use the following code
Control.InputType = Android.Text.InputTypes.ClassText | Android.Text.InputTypes.TextVariationVisiblePassword;
So these are the solutions which was useful for me.
Everybody,
I want to implement search on my app. I've been doing research here and there and some of it I can't really grasp. Android Search Listview using Filter is one of the very helpful example.
I want to implement a search functionality that give out a hint when I start searching. Also, when I clicked on the hint, it will move to the intended page.
For example, the hint is 'apple'. When I clicked on the hint it will directed me to the page of apple.
Is that possible?
Any site or tutorial that you can recommend for me?
Thanks!
I suggest you to use Material SearchView it worked for me.
MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do some magic
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//Do some magic
return false;
}
});
searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
#Override
public void onSearchViewShown() {
//Do some magic
}
#Override
public void onSearchViewClosed() {
//Do some magic
}
});
https://github.com/Shahroz16/material-searchview
I am adding some custom menu items in the Contextual Action Menu. I need to give a web search feature with the words selected in the WebView.
I override the ActionMode using this code.
#Override
public void onActionModeStarted(ActionMode mode) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
mode.getMenuInflater().inflate(R.menu.menu_search, menu);
}
}
super.onActionModeStarted(mode);
}
public void onContextualMenuItemClicked(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
//HERE I WANT TO GET THE TEXT: HOW CAN I?
break;
}
if (mActionMode != null) {
mActionMode.finish();
}
}
I want to search my site using the word selected by the user in the webview, but I couln't get the way to get the selected text. How could i get that, any one please help.
Thanks in advance.
The only way to get text selection from a WebView is based on javascript. This is not specific to the action mode, this is how WebView text selection is supposed to be retrieved according to WebView developers' point of view. They deliberately decided to not provide an API to access text selection from Java.
The solution comprise 2 approaches.
With Android API >= 19 you can use evaluateJavascript:
webview.evaluateJavascript("(function(){return window.getSelection().toString()})()",
new ValueCallback<String>()
{
#Override
public void onReceiveValue(String value)
{
Log.v(TAG, "SELECTION:" + value);
}
});
On older builds your only resort is a custom javascript interface with a single method accepting String, which you should call via webview.loadUrl passing the same thing:
webview.loadUrl("javascript:js.callback(window.getSelection().toString())");
where js is the attached javascript interface:
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new WebAppInterface(), "js");
and
public class WebAppInterface
{
#JavascriptInterface
public void callback(String value)
{
Log.v(TAG, "SELECTION:" + value);
}
}
I have an application that is connected and disconnected. These two states are handled by a single button. However, that was clear to the user, when you click Connect, the app must change the name of the button to disconnect, and unlike too. I tried to do it like this:
#Override
public boolean onMenuItemSelected(int panel, MenuItem item) {
showToast(item.getTitle().toString(), Toast.LENGTH_LONG);
if(item.getTitle().equals("Conectar") &&
item.getItemId() == (R.id.connectionButton))
{
ConnectProcess con = new ConnectProcess(Configuration.this);
con.execute();
item.setTitle(getResources().getString(R.string.disconnect));
}
else if(item.getTitle().equals("Desconectar") &&
item.getItemId() == (R.id.connectionButton))
{
LoadCompany loadCompany = new LoadCompany(Configuration.this);
loadCompany.execute();
item.setTitle(getResources().getString(R.string.connect));
}
return true;
}
When I run the command, the following exception is being thrown. Anyone know how to fix this problem?
06-09 15:08:26.140: E/AndroidRuntime(1219): FATAL EXCEPTION: main
06-09 15:08:26.140: E/AndroidRuntime(1219): Process: com.sisteplantbrasil.prisma3mobilev2, PID: 1219
06-09 15:08:26.140: E/AndroidRuntime(1219): java.lang.ClassCastException: com.android.internal.view.menu.ActionMenuItemView cannot be cast to android.view.MenuItem
06-09 15:08:26.140: E/AndroidRuntime(1219): at com.sisteplantbrasil.prisma3mobilev2.Configuration.setConnectionButtonText(Configuration.java:105)
Can you show me more of your logCat.Normally the line wich create the error is write on it.
But if i understand your question,you can use "seText()".
Here a code example:
//
button1=(Button) findViewById(R.id.yourButton);
button1.setOnClickListener(yourListener);
private OnClickListener yourListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
button1.setText("Your New Txt");
};
To change the actionbar menus in the support actionbar. call the super method supportInvalidateOptionsMenu(); onCreateOptionsMenu will then be called where you can change the menu. There is also invalidateOptionsMenu() method witch does the same thing on the non support version of the actionbar.
public class Whatever extends ActionBarActivity {
boolean action_connect = false;
public boolean onOptionsItemSelected(MenuItem item) {
int x = item.getItemId();
switch (x) {
case R.id.action_connect:
action_connect = true;
super.supportInvalidateOptionsMenu();
return true;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (action_connect) {
}
The Problem
On Android versions < 4.1, the alpha value of the MenuItem is getting reset after an orientation change, BUT it remains disabled.
The code I'm using
DetailsFragment.java
public class DetailsFragment extends SherlockFragment {
private MenuItem miEmail;
...
#Override
public void onPrepareOptionsMenu(Menu menu) {
miEmail= menu.findItem(R.id.menu_email);
super.onPrepareOptionsMenu(menu);
}
private void populateDetails(final Detail oDetail) {
//disable email button if dealer doesn't have option
if(!oDetail.bShowSAM){
miEmail.setEnabled(false);
miEmail.getIcon().setAlpha(50);
}
...
}
}
MyManifest.xml
<activity
android:name=".activities.DetailsActivity"
android:uiOptions="splitActionBarWhenNarrow"
android:configChanges="keyboardHidden|screenSize|orientation">
</activity>
What I expect to happen
When the orientation changes, miEmail is still disabled and the alpha value is still at 50.
What is actually happening
When testing on older devices(2.3,4.0), the MenuItem is remaining disabled but the alpha value is getting reset to the default value. When testing with my devices that are >4.1, it is working as expected.
What I've tried
Googling the problem.......
I've tried to avoid using the android:configChanges="..." and handling the data through savedInstanceState, but I've learned you can't make the MenuItem serializable/parciable, thus not allowing me to pass it through outState bundle object.
I'm fairly new to Android development and I feel as though there is a trivial way of handling this MenuItem, but I cannot figure how else to handle it.
What do you think is the issue?
Any feedback will be greatly appreciated.
Dont set the icon alpha on your custom function, instead, set it on OnPrepareOptionsMenu (with a suitable conditional). You can pass a boolean on savedinstancestate saying whether it should be grayed or not.
In your populateDetails function, you would call invalidateOptionsMenu() to make android remake the action bar icons. Example:
private boolean buttonEnabled;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem miEmail= menu.findItem(R.id.menu_email);
if (buttonEnabled) {
miEmail.setEnabled(true);
miEmail.getIcon().setAlpha(255);
}else{
miEmail.setEnabled(true);
miEmail.getIcon().setAlpha(50);
}
return super.onPrepareOptionsMenu(menu);
}
private void populateDetails(final Detail oDetail) {
//disable email button if dealer doesn't have option
if(!oDetail.bShowSAM){
buttonEnabled = false;
InvalidateOptionsMenu();
}
...
}
}
If you are using the support library for compatibility, use supportInvalidateOptionsMenu instead.
By the way, never use the orientation tag to "fix" the problem, the issue will still appear if you quit the app for a long time and then try to open it. (android pauses the activity initially and will stop it after a while)