Selecting text on TextView (android 2.2) - android

How to implement selecting text capability on Android 2.2? I searched Google but can't find a solution.

This is the only way I've found (from google) to support it for android 1.6+, it works but it's not ideal, I think in stock android you can't hold down a webview to highlight until v2.3, but I may be mistaken..
By the way this is for a webview, it might also work on textview, but I haven't tried it
(Note: this is currently what my shipped app is using, so it works with all the phones I've had it tested on, but the only reason I found this question was because I was searching and hoping that someone had come up with a better way by now)
I've just got a menu item called "Select text" which calls the function "selectnCopy()"
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//..
switch (item.getItemId()) {
case R.id.menu_select:
selectnCopy();
return true;
//..
}
}
Which looks like this:
public void selectnCopy() {
Toast.makeText(WebClass.this,getString(R.string.select_help),Toast.LENGTH_SHORT).show();
try {
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(wv);
} catch (Exception e) {
throw new AssertionError(e);
}
}
Notice I've put the select_help string there as a toast, that's just because it's not immediately clear to the user how it's supposed to work
<string name="select_help">Touch and drag to copy text.</string>

The Link #Stefan Hållén provided only worked after API Level 11.
And Android 2.2 is API Lv.8, that's the reason why you cannot get a resource identifier.

Have you set the text to be selectable? like this:
android:textIsSelectable="false" //OR true
See the Documentation for further reference.

After a long and time consuming search, I can't find a component that can select text in textview for android API level <=11. I have written this component that may be of help to you :
new Selectable TextView in android 3 (API <=11) component

An interesting workaround:
You could try displaying your text in a webview.
You just have to write the HTML tags and all of that into your string to display, and it should be selectable using the WebKit browser.
This should be fairly lightweight and transparent to the user, and I think it would solve your problem.
Let me know if you need a code example, it should be fairly simple. Just check out the WebView docs on http://developer.android.com/resources/tutorials/views/hello-webview.html
Best of luck!

Related

how to clear a text edit field? android python appium

how to clear text edit field? android python appium?
I tried that way:
el = driver.find_element_by_id('text edit field id').clear()
el.click()
el.clear()
however, it actually deletes only one symbol.
I think may by call an el.click() twice and send keycode to delete highlighted text area but how to highlight the text?
Interestingly, such problem reproduces in Sony Xperia z3 compact, android 5.0.1.
In lg, nexus android 4.4,5.1 accordingly, the above code works well
i had faced same issue with my android application but i am doing automation in java , so for the solution i am performing tap Action twice near the location of the element so it will select all the text which already written in text field and after that click on that "cut" symbol so it will clear text field.
i have written a method for sendkeys in android device.
public void typeForAndroid(AppiumDriver appiumDriver,WebElement ele,String text)
{
if(ele.getText().isEmpty())
{
ele.sendKeys(text);
}
else
{
TouchAction action=new TouchAction(appiumDriver);
action.tap(ele.getLocation().getX()+5, ele.getLocation().getY()+5);
action.tap(ele.getLocation().getX()+5, ele.getLocation().getY()+5);
appiumDriver.performTouchAction(action);
pause(2);
try{
appiumDriver.findElementById("android:id/cut").click();
}
catch(Exception e)
{
appiumDriver.sendKeyEvent(67);
}
pause(2);
ele.sendKeys(text);
}
}
this is working for me. I hope this might works for you.
in this i have used appiumdriver but you can also use androiddriver.
Thanks

Highlight Text feature like google play book

I want my android application to have text highlighting within a WebView. Similar to the functionality found in Google play book. Does any one have an idea how to achieve this?
I'm using a WebView because my content is in html form.
basically talking about this effect:
Starting from API level 11, you can use TextView's textIsSelectable flag.
Edit: Even though the question has now been edited to specifically mention WebView, OP #Suhail's comment "my content is in html form" does not fully disqualify TextView as it can also render some basic HTML.
The blue selection you see is part of the standard android environment when selecting text. So that should work on your standard webview without need of any custom code. => I'm no longer convinced this is true. Looks like it's not.
The green (yellow, orange, red, ...) selection however is custom.
You could read out the selected text from your selection event and use that information to update the html content, wrap the text in a span with a background color set.
Alternative approach is using javascript and enabling javascript in your webview. Not sure however how to get that done.
Some sources to check for that approach are https://github.com/btate/BTAndroidWebViewSelection and Android: How to select text from WebView, and highlight it onclick
Text selection from WebView details
To get the text selection working on a WebView you can use the following snippet (from this question). Trigger this on a button click (or other event) from your (context) menu.
private void emulateShiftHeld(WebView view)
{
try
{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(view);
Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.e("dd", "Exception in emulateShiftHeld()", e);
}
}
If your using WebView Try to integrate Mozilla PDF.JS where you can render PDF . which can contain Images also .

Accessibility and Android WebView

Is Accessibility enabled for Android WebView? Can anyone tell how to make a WebView accessible?
The Android webview accessibility is enabled via javascript injection in Honeycomb and above (as pointed out by alanv). The user must enable it by:
Turning on Accessibility mode, including 'Explore by Touch' in 4.0+.
Enabling 'Enhanced Web Accessibility', or, on older devices, 'Inject Web Scripts'.
This works by injecting javascript into each loaded pages via <script> tags. Note that you will have to be careful, as not all content will play nice with this javascript, and the injection will fail in some edge cases.
You can extend the WebView class to implement Accessibility API methods.
http://developer.android.com/guide/topics/ui/accessibility/apps.html#custom-views
I also ran into this issue on 4.0 devices. I ended up placing a TextView on top of the WebView, making the TextView background and text transparent. The text content of the TextView being set to the content of WebView with all HTML tags stripped out.
A most inelegant solution, I know, but hey it works....
No, the WebView is not accessible from the built-in TalkBack service, at least as of Android version 4.0. Instead, blind users are instructed to use something like Mobile Accessibility for Android, which provides a spoken browsing interface. I would suggest making your content available to other processes, allowing blind users to view the content in their browser of choice. I did this by starting an intent to view the content:
public static void loadUrlInBrowser(Context c, Uri uri) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(uri, "text/html");
c.startActivity(i);
}
Alternatively, you can set the content description of the WebView. Note that this only seems to work in pre 4.0 versions of android. I've found that 4.0 devices keep either say WebView or nothing at all.
I know how to access web view content by Accessibility API, when AccessibilityNodeInfo.getText() is null or "null", call AccessibilityNodeInfo.getContentDescription().
simple code:
private String getTextOrDescription(AccessibilityNodeInfo info) {
String text = String.valueOf(info.getText());
if (isEmptyOrNullString(text)) {
text = String.valueOf(info.getContentDescription());
}
return text;
}
private boolean isEmptyOrNullString(String text) {
if (TextUtils.isEmpty(text) || text.equalsIgnoreCase("null")) {
return true;
}
return false;
}
getContentDescription() can get the web view content.
sorry for my poor english.

Android:cannot make a selection after WebView search with highlight

Hello I have a bug in my app and I cannot figure it out.
I want to search for text in my WebView and get the found Text highlighted
for Android 1.5-2.3 this works quite well
public void onClick(View v){
webView1.findNext(true);
int i = webView1.findAll(findBox.getText().toString());
try{
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(webView1, true);
}catch(Exception ignored){}
}
}
for Android 3.0+ I have to use the JavaScript workaround from here, because Google doesn't support the highlighting of searched text for incomprehensible reasons
And now my Bug: After the search on my WebView I get the highlighted Text, and I can't select the Text anymore. The only fix I could use is the JavaScript workaround in older Android versions, too. But the function runs very slow and it takes about 10 seconds until the text gets highlighted. I Hope someone has a better solution/fix :)
Thank you
I use the same for 3.x then it did not work on 4.0.x.
Yesterday I updated to 4.0.4 and now highlight works again.
So the solution can be found in the 4.0.4 sources.
OK I have found a quite good solution.
Here is a JavaScript code for Highlighting, that runs really fast :) http://4umi.com/web/javascript/hilite.php#thescript
Anyway I don't understand, why I can't selecting text after the of the official Webview search
For Android 3.x I used webview.showFindDialog(stringtofind, true);
use findAllAsync() instead , finAll() is deprecated in API 16;

Spinner works in 2.3 but not 3.2

I have been successfully running an app under android 2.2 (api8) using spinners with OnItemSelectedListeners. It was built with tarketSdkVersion = 8 and minSdkVersion = 8. I am now trying to run it on a 3.2 device, but the spinners can not be selected. They do populate with the default array value however, so the adapter appears to be working. Clicking on the spinners results in no reaction. I tried building with tarketSdkVersion = 13 and minSdkVersion = 13, but the spinners are still dead. I am using slightly customized spinner versions to achieve "wrap_content" in a multiline_spinner_dropdown_item.xml file. Is there a compatibility issue with spinners since 2.2?
I tried the .setEnabled(true) but it didn't work. In frustration, I started to remove sections of code from main.java and layout.xml until a single spinner was left and working. As I added the code and controls back, I found that a ScrollView nestled inside a TabHost/LinearLayout/TabWidget/FrameLayout prevented the Spinners from responding. By removing the Scrollview, the Spinners worked in 3.2. For some reason the Scrollview worked in 2.2, but not 3.2.
I had the same problem, I used the spinner in 2.2 ,it was working but the same do not work in 3.2, The problem was with the default theme of 3.2, its holo. because of that the spinner is not shown properly,
Just create a theme in values/style, and apply it to your activity in android.manifest file.
Are you by any chance changing the visibility of the spinner?
I had a similar problem, and it was necessary to call setAdapter() every time the spinner was re-shown, otherwise it became immune to clicks.
You can see the change I made to my project that fixed this problem at https://github.com/nikclayton/android-squeezer/commit/7a148edf5f1b3eaca7718161de18254970290ce0.
Perhaps some visibility issues ?
Are you stacking the ScrollView on top of the area where the spinners' items show up ?
Try to use different states for the spinners by changing their icon or color for example and see if they get the click event. If they do, then it is probably something related with visibility. If they don't, it might something else.
Some code sample could help :)
Have you tried setting the spinner as enabled?
spinner.setEnabled(true);
Although it would be weird that it was disabled on default. Your symptoms do describe a disabled spinner though
Oddly, I tried another app of my that works great on 2.2. When I installed it on a 3.1 tablet or emulator, the onClick events won't fire. No spinners this time, but the listeners don't work either! Here's some of the code:
//In onCreate:
//setup listeners
rbSlab = (RadioButton)findViewById(R.id.rbSlab);
rbBeam = (RadioButton)findViewById(R.id.rbBeam);
rbSlab.setOnClickListener(radio_listener);
rbBeam.setOnClickListener(radio_listener);
etFpc.setOnEditorActionListener(this);
etFy.setOnEditorActionListener(this);
etBw.setOnEditorActionListener(this);
etDp.setOnEditorActionListener(this);
etMu.setOnEditorActionListener(this);
...
}//end onCreate
//In Main body of app:
//for radio buttons
private OnClickListener radio_listener = new View.OnClickListener()
{ //#Override
public void onClick(View v) {
DoCalcs();
}//onClick
};
//for edittext
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if ((actionId == EditorInfo.IME_ACTION_DONE) || //if DONE button pushed
((event.getAction()==KeyEvent.ACTION_DOWN) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))) //if ENTER button pushed
{
//do calcs
return(true);
}
else return(false);
}
I've built this with target API 8 and min API 8, and also with target API 14, min 8, and when installed on a API 8 the listeners work fine, but not with API 14!
I had a similar problem. It's because of the default theme of Android 3.2 and above versions.
The simpler solution is to use a custom spinner, or to make any background colour or picture for the same, like:
android:background="#drawable/spinner"
This link will help you for custom spinner.

Categories

Resources