this is my app strategy:
there is a text view that contains texts.
for editing them, i define Action mode items(e.g. EDIT- this will
texts of text view into edit text)although there are some default
actions like select all/copy/.. as selecting text, so i don not want
to define them.
i use this code for enabling the default selecting/copy buttons.
but as i clicked the buttons, nothing happens. why???
xml code:
<TextView
android:id="#+id/speech"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:textSize="25sp" android:textAlignment="inherit"
android:selectAllOnFocus="true"
android:background="#drawable/curved_background"
android:layout_centerInParent="true" android:textIsSelectable="true"
android:padding="10dp" />
initializing TextView in class:
speech_text = (TextView) findViewById(R.id.speech);
registerForContextMenu(speech_text);
speech_text.setTextIsSelectable(true);
speech_text.setCustomSelectionActionModeCallback(new SelectText());
SelectingText java class:
public class SelectText implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.text_select, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Log.d(Log, String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
switch (item.getItemId()) {
case ... //other actions
return true;
}
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
Note:
as i control my code. the problem is in defining SelectText class in this line: speech_text.setCustomSelectionActionModeCallback(new SelectText()); because as i delete thiscode, every thing worksgood!
You can make your text in app enable for copy/paste in clipboard by adding below line :
android:textIsSelectable
For more information, Please check below reference,
http://developer.android.com/reference/android/widget/TextView.html
Thanks.
This should work for you.
In your xml jus make sure that....
android:focusable="true"
android:textIsSelectable="true"
Related
Whenever I have text in an EditText, the user can do a longpress over the text and the "Copy/Cut/Share" options hover over it.
It is OK for me to let Copy/Cut (Paste also) options appear (although not really needed), but I really can't let those text trigger the "share" option appear.
Is this feasible without reinventing the wheel ?
The XML code to define the EditText is quite basic, currently testing against API 30:
<EditText
android:id="#+id/settings_doctor_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/dr_name"
android:inputType="textCapCharacters"
android:singleLine="true"
android:textSize="26sp" />
Having had a review on EditText and TextView documentation, but I still haven't found any idea on do to deal with this.
Did I miss anything in the docs ? Thanks for reading !
I share with you the solution derived from your comments that has been implemented and tested:
I created a my own TextEdit:
public class EditText extends android.support.v7.widget.AppCompatEditText {
public EditText(Context context) {
super(context);
this.setCustomSelectionActionModeCallback(removeShareActionMode);
}
ActionMode.Callback removeShareActionMode = new ActionMode.Callback() {
#Override
#TargetApi(26)
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (menu != null) {
menu.removeItem(android.R.id.shareText);
}
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
}
And used it everywhere. Works as expected. Thanks to all for contributing. I'm sorry I don't have enough reputation to vote your answers.
I have a TextView where a user is able to select a text. By default the following options appear: "Copy", "Share" and "Select All".
I need to override them with custom options. But I can't find how to do that. I went through the documentation and this nice article but no lack. The article explains how to extend the menu when a user presses three-dots-button which is not what I need.
Question: How can I override default "Copy", "Share" and "Select All" options in text section menu?
Here is how my view looks like:
<TextView
android:id="#+id/transcript"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
And in java code I have:
transcript.setTextIsSelectable(true);
transcript.setFocusable(true);
transcript.setFocusableInTouchMode(true);
You can use TextView.setCustomSelectionActionModeCallback() to do this.
Documentation: https://developer.android.com/reference/android/widget/TextView.html#setCustomSelectionActionModeCallback(android.view.ActionMode.Callback)
I put together a very simple app to demonstrate how to use this feature.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
CustomActionModeCallback callback = new CustomActionModeCallback(this);
text.setCustomSelectionActionModeCallback(callback);
}
}
activity_main.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:text="#string/lorem_ipsum"
android:textIsSelectable="true"/>
</FrameLayout>
CustomActionModeCallback.java
public class CustomActionModeCallback implements ActionMode.Callback {
private final Context context;
public CustomActionModeCallback(Context context) {
this.context = context;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
mode.getMenuInflater().inflate(R.menu.menu_custom, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.custom_one) {
Toast.makeText(context, "One!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
else if (item.getItemId() == R.id.custom_two) {
Toast.makeText(context, "Two!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
else if (item.getItemId() == R.id.custom_three) {
Toast.makeText(context, "Three!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
menu_custom.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/custom_one"
android:title="One"
app:showAsAction="never"/>
<item
android:id="#+id/custom_two"
android:title="Two"
app:showAsAction="never"/>
<item
android:id="#+id/custom_three"
android:title="Three"
app:showAsAction="never"/>
</menu>
Nothing much to comment on in MainActivity or either xml file. All the magic happens in CustomActionModeCallback.
Both onCreateActionMode() and onPrepareActionMode() can be used to add your custom menu items to the menu. If you use onCreateActionMode(), the system will add some extra options into an overflow menu, like this:
If you use onPrepareActionMode(), the extra items won't be added.
Note that you must return true from onCreateActionMode() no matter what (returning false causes the menu to not be displayed), but you only have to return true from onPrepareActionMode() if you've actually modified the menu.
You can handle the user's clicks on your custom items inside onActionItemClicked(). In my example, I simply show a Toast and then close the contextual menu (using ActionMode.finish()). In this method, you should return true only on menu items that you handle yourself; returning false allows the system default action to happen (such as if you want to give the user the option to select all text).
Finally, onDestroyActionMode() is called when the menu is closed. Perhaps you have some use for this; I did not.
I am using following code but it is not working when cursor blink and I click on that paste button display.
edittext.setCustomSelectionActionModeCallback(new ActionMode.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;
}
});
I don't want to hide Cursor.
How do I solve this issue?
Try this:
edittext.setLongClickable(false);
edittext.setTextIsSelectable(false);
You can use longClickable in xml file like:
<EditText
... your edittext
android:longClickable = "false"/>
or you can try in java like
youredittext.setLongClickable(false);
you can try this just make your editext LongClickable false like below code
from XML
android:longClickable="false"
from JAVA
edittext.setLongClickable(false);
I trying to create a note apps that have an ability to format the selected text in EditText like the one I usually use in Onenote. But everytime I click my menu icon, the selected text in EditText is resetted and the menu is closed itself.
This is my code in my custom Edit Text
public void init()
{
this.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.edit_text_menu, 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) {
}
});
}
Menu Layout
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_bold"
android:title="Bold"/>
<item android:id="#+id/action_underline"
android:title="Underline"/>
<item android:id="#+id/action_italic"
android:title="Italic"/>
</menu>
the screenshot of onenote menu
http://i57.tinypic.com/zlriu9.png
and screenshot of my apps
http://i58.tinypic.com/2yjwlys.jpg
What is the right way to make a menu for EditText to format just the selected text?
Update! Already found a solution that I think work this far
I just need to override the onWindowFocusChanged function, so when I click another menu, it won't lose its selection.
i'am beginner and i'am trying to make a simple calculator for android but he give me a syntax error
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
final EditText e = (EditText)findViewById(R.id.value);
final EditText e2 = (EditText)findViewById(R.id.value2);
getMenuInflater().inflate(R.menu.activity_main, menu);
Toast.makeText(this,"Welcome",Toast.LENGTH_LONG).show();
Button welcome = (Button)findViewById(R.id.x);
welcome.setText("push ");
welcome.setonclickListener(new View.onclickListener() {
#Override
public void onclick(View v) {
int v1 = Integer.parseInt(e.getText().toString());
int v2 = Integer.parseInt(e2.getText().toString());
int v3 = v1 + v2 ;
Toast.makeText(MainActivity.this,"= " + v3, Toast.LENGTH_LONG).show();
return true;
}
});
}
}
http://imageshack.us/photo/my-images/24/problem00.png/
Try: View.OnClickListener() with a capital O and C. And the same thing with setOnClickListener. That should do the trick.
Use OnClickListener(). You don't have to specify View. Also, make sure you use a capital 'C' in the overridden onClick() method. Make sure you include import android.view.View.OnClickListener; in your imports section. Eclipse should this for you, but if it's not or you are using Eclipse, add it manually to the top of the class. If you simply do the following, that should work for you:
welcome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int v1 = Integer.parseInt(e.getText().toString());
int v2 = Integer.parseInt(e2.getText().toString());
int v3 = v1 + v2;
Toast.makeText(MainActivity.this, "= " + v3, Toast.LENGTH_LONG).show();
return true;
}
});
--------EDITED FOR FULL EXAMPLE--------
In your project, you have your main activity. That activity should use a layout from an XML resource. That XML resource likely looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
In your activity class, you have to set that resource as the content view for the activity, using setContentView(resID). Say your XML file is called helloworld.xml, you would execute setContentView(R.layout.helloworld) in the activity's onCreate(Bundle s) method right after calling super.onCreate(s).
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.helloworld);
}
Once you have set the activity's view, you can access that layout's elements (EditTexts, Buttons, etc). To do this, you'll have to create EditText and Button objects (which you were already doing in your posted code, we just have to do them elsewhere instead). Continuing with my example, you could do the following in your onCreate(Bundle s) function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.helloworld);
EditText et1 = (EditText) findViewById(R.id.edittext1);
EditText et2 = (EditText) findViewById(R.id.edittext2);
Button but = (Button) findViewById(R.id.button1);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
et2.setText(et1.getText().toString());
// this will set the second EditText's text to whatever is in
// the first EditText, but you could do anything with the value.
}
}
}
If you want to change what the button does based on a menu item selection, you have to override onOptionsItemSelected(MenuItem item) in addition to onCreateOptionsMenu(Menu menu). onCreateOptionsMenu(Menu menu) simply creates the menu for the menu button to open. onOptionsItemSelected(MenuItem item) actually decides what to do when you select a menu item.
See the tutorial at this page for full rundown http://developer.android.com/guide/topics/ui/menus.html, but here are their examples with some explanation. These examples are not from my example app above but from the Android Developer API pages. I strongly recommend you go over their tutorial.
All you have to do in onCreateOptionsMenu(Menu menu) is tell Android where to get the menu and to inflate that resource. This means there is an XML file called game_menu in the menu folder of the res folder in your project's directory.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
onOptionsItemSelected(MenuItem item) is where the meat of the menu logic goes, letting you perform different tasks based on which menu item get's selected. In this example, the game_menu XML file mentioned in the above function has menu items called new_game and help.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}