Open Dialog via dynamic menu - android

How can I open a dialog with some html formatting and hypertext link support (open that link in default browser), when I click on item: "ABOUT" in my dynamically created menu?
Also, how I can make SHARE function, so that if anybody click on: "SHARE" item, it will either share link to that APK, or send it over bluetooth?
This is what I have in MainActivity:
private static final int NEW_MENU_ID=Menu.FIRST+1;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, NEW_MENU_ID, 0, "ABOUT");
menu.add(0, NEW_MENU_ID, 0, "SHARE");
return true;
}
And this is how it should look like:
Thanks for help!

Actually, there's two big and completely different questions and too little code presented.
First of all You should give different options id in onCreateOptionsMenu (let them be ID_ABOUT == 0 and ID_SHARE == 1) override onOptionsItemSelected() like this:
#Override
public boolean onOptionsItemSelected (MenuItem item) {
switch(item.getItemId()) {
case ID_ABOUT:
handleAbout();
break;
case ID_SHARE:
handleShare();
break;
}
}
No handleAbout() and handleShare() should be defined (that's your questions about):
ABOUT: probably, the easiest way is to create additional activity which would contain only one WebView. First activity would just start AboutActivity from handleAbout();
SHARE: it's quite common task. Please, refer to android documentation here and for example, to this question

1. how could I open dialog with some html formatting and hypertext link support (open that link in default browser), when I click on item: "ABOUT" in my dynamically created menu?
Look at this SO Question: Android hyperlinks on TextView in custom AlertDialog not clickable
2. how I can make SHARE function, so that if anybody click on: "SHARE" item, it will either share link to that APK, or send it over bluetooth?
Use Android Intent with Intent.ACTION_SEND. Which will share the link of .apk file on available application on device which handle SHARE Intent.
and to send APK via Bluetooth .. either use same Intent with ACTION_SEND action or You have to implement Bluetooth file transfer code..
Look at this SO Question: bluetooth file transfer in android

Related

How to open common incomplete products link with different toolbar in android studio

I have an eCommerce website and that has been running as an android app using WebView method. I have basic knowledge in android studio but not depth. My concern is wanna open Second Activity from Main Activity when user clicks on any product in app that contains link as "https://www.ecommerce.in/product/XXXXX". Here "https://www.ecommerce.in/product/" is common to all products but "XXXXX" will change to every product when user clicks on different products and this Second Activity should open using another toolbar without related to Main Activity toolbar and my code is like this:
webView.setWebViewClient(new ourViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.contains("https://www.ecommerce.in/product/XXXXX"))
{
Intent intent = new Intent(MainActivity.this, ProductsActivity.class);
startActivity(intent);
}
}
});
This code is not working at all. Hope, I get a solution for this and thank you for your help in advance.
First, it looks like you're trying to filter out links with a contains:
if(url.contains("https://www.ecommerce.in/product/XXXXX"))
But you've made it too restrictive. It is literally looking for "XXXXX" in the URL. Also, this doesn't really get you much in terms of security. WebViews are very dangerous. I'd suggest looking through this link for some basic suggestions.
Anyways, if you just want to open the ProductActivity it looks like all you have to do is update the test as follows:
if (url.startsWith("https://www.ecommerce.in/product/"))
Note if you need that URL then you have to pass it to the ProductActivity as part of the intent and grab it in the ProductActivity, but be sure to verify the URL before just loading it to make sure sit starts with the common part.
It might be that someone is trying to get your Activity to do things it shouldn't be.

How to fix the share icon in the support actionbar

I'm trying to implement the share functionality within my app. So far it works fine and I can share text to all other apps. The problem is the way it's shown.
I want something like just the share icon visible and then when user taps on it, it opens the OS dialog and lets user choose the app they want to share content to.
var share_article = menu.FindItem (Resource.Id.action_share);
var share_article_provider = (Android.Support.V7.Widget.ShareActionProvider) Android.Support.V4.View.MenuItemCompat.GetActionProvider (share_article);
share_article_provider.SetShareIntent (CreateIntent ());
and the xml:
<item
android:id="#+id/action_share"
myapp:showAsAction="ifRoom"
android:title="share"
myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
My app currently looks like this:
There's also a white border around it that I don't like.
Is there any way to change the icon??
How do I fix it??
You just want to turn off your share history.There is no official API to do this, but you can make your own ShareActionProvider. Actually there are two similar question on SO:
How do you turn off share history when using ShareActionProvider?
How to hide the share action (which use most) icon near the share action provider?
Wish these could help you.
As mentioned here when using support library this can be fixed really easily. This method won't turn off the share history but will hide the icons from actionbar.
I just needed to subclass the Android.Support.V7.Widget.ShareActionProvider like the following: (C# using Xamarin)
public class MyShareActionProvider : Android.Support.V7.Widget.ShareActionProvider
{
public SingleArticleShareActionProvider (Context context) : base (context)
{}
public override View OnCreateActionView ()
{
return null;
}
}
and then inside OnCreateOptionsMenu use the MyShareActionProvider like:
var share_article = menu.FindItem (Resource.Id.action_share);
var share = new SingleArticleShareActionProvider (globalContext);
Android.Support.V4.View.MenuItemCompat.SetActionProvider (share_article, share);
share_article.SetIcon (Resource.Drawable.abc_ic_menu_share_mtrl_alpha);
share.SetShareIntent (CreateIntent ());
You can use any icon you like with the method SetIcon.

How do I make an action bar feature from the facebook app?

This is a picture of the facebook app from the play store. I would like to copy the part that says "New Stories 10+" in my app. How do I make that? Is that part of the action bar?
I read through the action bar documentation here but couldn't find anything about it. I'm having trouble doing a google search for it too because I don't know what it's called.
Also, if you open the facebook app when you're in airplane a very similar type of message comes up that says "No Internet Connection". How is that message created?
Looking a little more deeply at the Facebook app, the bar just appears to be a LinearLayout containing two TextViews. This layout is then just embedded in the news feed fragment and hidden/shown as needed. In other words, it's not a part of the action bar; it's just a normal view within the fragment.
Check out this library by Cyril Mottier. it was designed to do exactly what you are asking for.
Yes it is part of the ActionBar but only if the device or emulator is on API 3.0 and higher. if the device or emulator has a lower API level, the actions will appear on the menu button.
This is just a dummy but it will give you a rough idea.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
int groupid = 1;
menu.add(groupid, 1, 1, "Search").setIcon(R.drawable.embassy_default);
// menu.add(groupid, , ,"Back").setIcon(R.drawable.hospital_default);
menu.add(groupid, 1, 1, "Exit").setIcon(R.drawable.government_default);
return true;
}
Read more here
http://developer.android.com/guide/topics/ui/menus.html

Return from Android Gallery in jUnit?

I have an application in which if you click a button, it opens the Android Gallery application. The user can then select an image for my application. The thing is, if I click that button in JUnit, it opens up the Gallery and then just sits there. I have to manually choose a picture. I just want to be able to open the Gallery and then go back or if possible select an image.
I tried several options such as -
sendKeys(KeyEvent.KEYCODE_BACK)
Robotiums goBack(), goBackToActivity(), sendKey() functions
Robotiums clickOnScreen() function to click and select an image
but none of these seem to work. So what do I do?
I had the same problem. You can't do this, as the Android Gallery is another application running in its own sandbox which you cannot reach from your testing code.
However, I came up with the following solution. You can build your own dummy application that provides an Intent filter for opening images. In this application you simply return the intent with an ok.
Now you only have to select your application as the default application when first opening your image from your test application. Afterwards, everything should work automatically.
Once you're done with testing, you can reset the default intent filter in your Android device's settings.
The code for my solution can be found in this discussion: How to provide content for Intent.ACTION_GET_CONTENT
It is possible to simulate response returned from gallery.
The code snippet below do all the work.
final Intent data = new Intent();
data.setData(Uri.parse("content://media/external/images/media/458")); // put here URI that you want select in gallery
Runnable runnable = new Runnable(){
#Override
public void run() {
getActivity().onActivityResult(3333,-1, data);
synchronized(this) {
this.notify();
}
}
};
synchronized(runnable) {
getActivity().runOnUiThread(runnable);
runnable.wait();
}
This method has two drawbacks:
onActivityResult method of your activity should be public which brakes encapsulation.
Test will not actually click button which opens gallery and onClick handler won't be executed.
But the benefit of such approach is that you don't need to build mock application and change any defaults in android settings.

Selecting text on TextView (android 2.2)

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!

Categories

Resources