How to send highlighted text from default browser to Activity? - android

I am new in Android Application Development.
Research: Before asking this question, I have read topics related with my question (How to send string from one activity to another?). Unfortunately they are explaining how to copy string from one activity to another activity.
My intention: User opens default browser of mobile phone, highlights text and clicks the button which will be added by me on menu and needed activity is opened with highlighted (copied) text
Tried: I have added intent filter to activity, then added action (android.intent.action.SEARCH) and category (android.intent.category.DEFAULT) into intent-filter. I thought, link for activity will be in Search section after Google, Google Translate, Wikipedia, Dictionary. Also, tried android.intent.action.ACTION_SEARCH_LONG_PRESS. Also, android.intent.action.SEND. Why it is not working, I have no idea.
Tried(2): Found answer for my question partially from here. But link for activity will be in Share->Share as Text->Activity. I think it is too long way.
<activity android:name="ActivityDate">
<intent-filter android:label="Lesson">
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.SEND"/>
</intent-filter>
</activity>
Questions (I have many questions. I will try make them short and ask the ones that are most important for me)
Question 1: Is it possible to add a button within menu (which opens when
text is highlighted. It includes copy, search, share)?
Question 2: If it is possible, how to do that? How to make the button so
that when it is clicked, highlighted text should be send to my
activity?
Question 3: If I could do this function for the default browser, can I implement to
other applications (3rd party applications)?

Related

What is the name of this on app development?

We are doing an app for a client an he needs some similar functionality to this (see picture attached) offered by the iTranslate App on iOS.
With this functionality, when you are in any other App (for instance reading anything on the Medium App) and you select a word, a menu appears and you can select to open this word with the app of my client. But instead of opening the whole App and closing the one were are using, a kind of pop up appears:
I have a few questions about this:
- Does this have a name?
- Can something like this be done with Ionic or you need to code the app in Native?
- Is this possible only on iOS or also in Android?
I am really lost about this issue and would appreciate some guidance.
Thanks
The example in the first picture would be called a "Floating Context Menu," according to the Android Developers website. The example in the second picture would be called a "Popup Menu."
Hope this helps! Good Luck!
You can use a text selection toolbar https://material.io/design/platform-guidance/android-text-selection-toolbar.html# for this, which was added in Android 6.0. Note that you can only use this on Android 6.0 and later.
There is a nice article here, which provides some examples on how to create this:
https://medium.com/androiddevelopers/custom-text-selection-actions-with-action-process-text-191f792d2999
From the article, the basic implementation is the following:
AndroidManifest.xml
<activity
android:name=".ProcessTextActivity"
android:label="#string/process_text_action_name">
<intent-filter>
<action android:name="android.intent.action.PROCESS_TEXT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
ProcessTextActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.process_text_main);
CharSequence text = getIntent()
.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
// process the text
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_PROCESS_TEXT, result);
setResult(RESULT_OK, intent);
}
So, I am pretty sure you can implement this in ionic. I never wrote anything in ionic, but you may be able to call something like this Java code from it.
This is a customized PopupWindow(Android Documentation) which should not be a major issue to cook up with something like PopoverController (Iconic Documentation).
the real problem you will face is the text selection. you can look into this stackoverflow link for pointers.
Solutions like these may work in one or the other platform you have to muck the code unless there is and api for text selection in iconic.
if iconic does not give you the API you will have to roll up your sleeves. At this point you are on a slippery slope looking over the webview(s).
Update:
All the above juggling is needed to implement this within your app. Android and iOS will not allow you to add items to system context menu as you see in an PC based(Windows/MacOS/...) OS(s).
If you check Google Translator app in Android. it listens clipboard copy event and pops up a transient icon over other apps. in iOS drawing over other app is not possible.
So if you want your feature to show up in Medium App then they have to add the UI and they have to call your 'API'.

How to use the new Android M feature of "Text Selection" to be offered from outside of your app?

Background
Android M presents a new way to handle selected text (link here), even from outside of your app . Text selection can be handled as such:
I know it's possible to handle the selected text from outside the app, because if I go to the web browser (or any other place that allows text selection), I can see that I can use the "API demos" app to handle the selected text.
The problem
I can't see a lot of information about how to do it.
The question
What should be added in code (and manifest) to be able to handle the selected text from outside the app ?
Is it possible to limit the selection to certain types of texts ? For example, offer to show the app only if the text type is a valid phone number ?
First, to clarify the question: On an M emulator, if you highlight text, you will see the new floating action mode. If you click the overflow icon, you will see "API DEMOS" show up:
Clicking that brings up an activity from the API Demos app, showing the highlighted text:
Replacing the value in the field and clicking the button puts your replacement text in as a replacement for whatever you had highlighted.
WARNING: The following explanation is from inspecting the API Demos code and the M Developer Preview documentation. It is very possible that this will change before M ships for realz. YMMV, unless you use the metric system, in which case YKMV.
The activity in question, that is receiving the text, supports ACTION_PROCESS_TEXT as the Intent action. EXTRA_PROCESS_TEXT will hold some text, or EXTRA_PROCESS_TEXT_READONLY will hold it if the text is read-only. The activity will be invoked via startActivityForResult(). The result Intent can have its own EXTRA_PROCESS_TEXT value, which will be the replacement text.
So, to the specific questions:
What should be added in code (and manifest) to be able to handle the selected text from outside the app ?
See above. Note that the API Demos activity (ProcessText) has this <intent-filter>:
<intent-filter >
<action android:name="android.intent.action.PROCESS_TEXT"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
The documentation does not discuss a MIME type. I have not run any experiments to determine if the MIME type is required, and what else we might get (text/html for stuff that has spans?).
Is it possible to limit the selection to certain types of texts ? For example, offer to show the app only if the text type is a valid phone number ?
That wouldn't seem to be possible given the documentation. That being said, it's certainly a reasonable idea (e.g., advertise a regex, or multiple regexes, via metadata in the manifest that the text must match).
This article on Android Developers Blog may be relevant, it describes how Google Translate option can be added to overflow text selection menu.
Android apps that use Android text selection behavior will already
have this feature enabled, so no extra steps need to be taken.
Developers who created custom text selection behavior for their apps
can easily implement this feature by following the below steps:
Scan via the PackageManager through all packages that have the
PROCESS_TEXT intent filter (for example:
com.google.android.apps.translate - if it installed) and add them as
MenuItems into TextView selections for your app
To query the package manager, first build an intent with the action
Intent.ACTION_PROCESS_TEXT, then retrieve the supported activities
and add an item for each retrieved activity and attach an intent to it
to launch the action
public void onInitializeMenu(Menu menu) {
// Start with a menu Item order value that is high enough
// so that your "PROCESS_TEXT" menu items appear after the
// standard selection menu items like Cut, Copy, Paste.
int menuItemOrder = 100;
for (ResolveInfo resolveInfo : getSupportedActivities()) {
menu.add(Menu.NONE, Menu.NONE,
menuItemOrder++,
getLabel(resolveInfo))
.setIntent(createProcessTextIntentForResolveInfo(resolveInfo))
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}

Launch Home choice from an app

Hello my question is a bit difficult let me explain and try to understood
I have a custom Home who is set as default when user press home. Know i need a way to ask wich home use when i press on a setting key, or a way to launch Setting activity for delete home default action.
In fact i wan't a way to go back to home app installed by google instead of mine just in case.
I need this because my custom home is still in dev and don't have all feature yet.
I need to keep my home as a default action when user press home buton but i need to add a home selector in special case.
I have try to use finish() when i need to go back to old home but mine as default is automaticly relaunch because he is the default one.
Any idea?
EDIT
I try to reformul a bit.
I'm making a home apps. I set it as default on my tablet. My home apps don't have all feature yet. And when i will launch something or go to setting i need to relaunch default home app.
So i need a way inside my app to reset default home action or launch a home apps, or ask user to choice wich app he will use has home apps.
Hope this is clear for all.
Probably you should use this one to implement your idea. keep the below lines in your manifest file.
<activity android:name="Home"
android:theme="#style/Theme"
android:launchMode="singleInstance"
android:stateNotNeeded="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
For more info about the sample project. just go the below path in your Android SDK
\android-sdk\samples\android-8\Home
EDIT :
Hi Please check this to pro grammatically unset the default application.
How do I use PackageManager.addPreferredActivity()?
for more info go through this also to clear the clearPackagePreferredActivities
http://developer.android.com/reference/android/content/pm/PackageManager.html#clearPackagePreferredActivities%28java.lang.String%29
hope this helps you.

Wikitude personalize view on Android

I am creating an app using Wikitude API, but I haven't been able to customize the view.
I have asked the developers and I know I can't add buttons to the main view in the current release version (for Android), but I am wondering if I can add more buttons to the options menu. Right now when I press it I get just one button that says "Ignore Altitude" can I modify that button and/or add more buttons to that menu?
I have checked other posts but there aren't any answers. The posts are a little bit old so that is why I am asking again.
I haven't found any useful documentation.
Any help is greatly appreciated
if I understood your question correctly, then please try the following: in the method prepareIntent() of your main Activity, you can add up to 3 menu items:
intent.setMenuItem1("Menu 1", YourActivity.CALLBACK_INTENT);
intent.setMenuItem2("Menu 2", YourActivity.ANOTHERCALLBACK_INTENT);
Then you define the callback function as another activity (with dialog, list and stuffs). It works fine this way for me.
I am also playing around a bit with Wikitude, but hard to find something well documented!
Yes , You can Add upto three menu button as if you read doc properly
intent.setMenuItem1("Menu Name", YourActivity.LOCATION_INTENT);
intent.setMenuItem2("Menu Name", YourActivity.LOCATION_THREATS);
intent.setMenuItem3("Menu Name", YourActivity.MAP_INTENT);
With making Intent variable as
public static final String LOCATION_INTENT = "wikitudeapi.mylocationactivity";
Also declare action in manifest as,
<activity android:name=".activities.Your Activity Name"
android:theme="#*android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="wikitudeapi.mylocationactivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

What does "category" in the manifest mean?

The documentation says you can specify a custom category.
When, why and how would you do it?
What would be the use of it?
The way I understand it, categories are public directives to the android operating system(and other apps) that represent different categories that your app should be a part of.
Example
When the launcher icon is tapped on the home screen, the home application looks through every installed app's manifest for the HOME category -- and if so it displays it in the app drawer.
However, there's more. You can specify categories in your applications manifest that lets the system know that you application can handle the intent category. For example, by putting a ALTERNATIVE category, other apps in the system know that your app can handle that category without specifically knowing the action name! In the following example, custom intent categories are passed through this intent, which is filtered and the corresponding object gets edited(taken from the Notes example app):
<intent-filter android:label="#string/resolve_title">
<action android:name="com.android.notepad.action.EDIT_TITLE" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
By registering this intent filter in an <activity /> tag, you can edit a "note". The intent data would contain the note, and the intent would get routed to the activity that this filter is registered in.
In Conclusion:
There isn't really a reason you'd use a custom category. They are for Android, and thus don't really make sense in application use. But, if you choose to use them, they can be used in the methods described above. "They provide some specific semantic rules, and if those rules are useful to you then feel free to use them"(Hackbod).
http://developer.android.com/guide/topics/intents/intents-filters.html
Scroll down a bit and you should see a section under "Intent Objects"
They basically describe certain special properties of an activity. for example, adding
<category android:name="android.intent.category.HOME" />
means that the app can be started on the phone's bootup
I'm kinda a noob to Android still, although I have programming experience otherwise.. It says a custom category in your own namespace. I'm guessing that if you are programming multiple apps and you want one app to run another app, you could use a custom category for your intent to force the phone to find your other app to catch the intent with?
When you do not want to use the default category then use the custom category.
Custom categories should use the package name as a prefix, to ensure that they are unique.
Some information is provided on below link:
http://developer.android.com/guide/topics/manifest/category-element.html
Check the below link it has somewhat same question:
Android custom categories

Categories

Resources