Built-in grid bottom sheet? - java

Is there a built-in way for a grid bottom sheet, usually seen for sharing in some apps, or do I have to create a custom BottomSheet?
//1 column bottom sheet
String message = "Text I want to share.";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share, "Share through:"));

The intent chooser is a property of the OS and you can't really change it. As you might have noticed, this chooser has changed its appearance in the different releases of the OS. The apps in which you have seen a grid chooser might have implemented a custom bottom sheet or activity to look like a bottom sheet. You can achieve this by using the Android PackageManager. You can obtain the different apps that can handle your intent and show these in your custom chooser and then send the intent to the app which the user clicks on.

Related

Open image chooser with camera option

How can I open an image chooser and show an option to take a picture as well?
Here's my code:
private void openFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
The problem is that there's no open camera option. How do I add that?
And this is what I get:
You can use this library or show a normal alert dialog with two buttons, one for gallery and one for camera.
There is a similar question: Dialog to pick image from gallery or from camera
You have to specify an action for invoking a chooser/app via an intent. And since the action for selecting an image from gallery and taking a new image using the camera, are different, i don't think a chooser will have both option for uploading an image and taking a new image at the same time. For reference you could check profile picture changing procedure in facebook or whatsaapp. When clicking on the image to change, a custom dialog is shown with the option to either upload an image, take a new image, or select from a previously uploaded image. Based on what the user selects the app navigates the user to the particular app(camera/gallery etc)
What you can do is first create a custom dialog(or a layout based on your application's design) with two options
Take a new image.
Upload an Image from gallery.
Based on what the user clicks execute the code to navigate the user to, either the gallery or the camera application. If the user clicks on Upload an image from gallery, execute your above code and if the user clicks on Take a new image, then execute the code for opening the camera.
private void takeNewImage() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent,CAMERA_INTENT_REQ_CODE);
}

Android Programming - Always Restarts same Intent

I am using Android Studio for a simple communication app and this definitly might be a stupid question but I couldn't find the answer to it yet.
I am simply starting a new intent on a button click. However, the very first time the user does this, he is asked what kind of app he wants to use. Therefore, there are at least two applications with the same intent filter, namely "ACTION_VIEW"
My Problem:
After the initial click the button always "reuses" his initial choice so the user does not have a decision anymore which app he wants to use for this intent.
Unfortunately, this is excactly what I want. The user should get the chance to select the app of his favor each time he clicks on the button.
The relevant part of code is as simple as follows:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
Intent.FLAG_UPDATE_CURRENT could probably help you.
https://developer.android.com/reference/android/app/PendingIntent.html
You can use an App Chooser to show the dialog for the user to choose which app to use each time:
[I]f the action to be performed could be handled by multiple apps and the user might prefer a different app each time—such as a "share" action, for which users might have several apps through which they might share an item—you should explicitly show a chooser dialog.... The chooser dialog forces the user to select which app to use for the action every time (the user cannot select a default app for the action).
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}

starting another android application to open a photo?

I had an interview today for a junior android dev position and one of the questions I was asked was something like this:
You have an application that opens an email. The email has a photo attached to it. How could you give the user the choice of which application to open this photo with? I.e. give them a list of photo opening apps to choose from and then open that application with the photo. You don't know ahead of time what apps are available other than android's default photo viewer.
How the heck do you solve this?
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/foo.jpg");
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);
In a nutshell, you are telling Android, in an abstract way that you'd like to view a photo. Apps that are able to view photos register their ability to do so in their manifest (they respond to ACTION_VIEW, for the given content type image/*).
If you wanted to view an HTML file,
intent.setDataAndType(Uri.parse("http://www.google.com", "text/html");
ACTION_VIEW is just one example. There's also an ACTION_EDIT, and others.

How do I programmatically write a shortcut to a specific page on the homescreen of the Android launcher?

I am currently working on an Android application whereupon I programmatically write a shortcut to the home screen. Something like this:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(ai.packageName, ai.name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
BitmapDrawable bd=(BitmapDrawable)(res.get(app_id).activityInfo.loadIcon(p).getCurrent());
Bitmap newbit;
newbit=bd.getBitmap();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newbit);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);
(it was actually a StackOverflow post that pointed me to this solution).
Anyway, when I run the app in the emulator, the shortcut is written to the fifth page from the left(on 2.3 home screen ). A subsequent run writes the shortcut to the 4th page from the left on the home screen.
My question is this: is there a way to write the shortcut to the center page or the first page (like passing indexes for the 2-D array that seems to hold the app shortcuts)? am I missing something in the API?
Thanks!
You cannot decide where the shortcut will go.

Debugging Intents

I asked a question previously about what shows up in the IntentChooser when I send an Intent with ACTION_SEND and MIME type "image/png". The problem is that some phones come with a default messaging app that is not showing up in the list, mine included (myTouch 4G) as well as a user that I speak with over email (using an HTC EVO). If I choose a Send or Share option from the built in gallery app or another application on the same image I'm saving and attempting to send directly from my app, Messages shows up in the list. From my app it does not. Other phones don't have this problem, so it's clearly a platform specific thing. But that doesn't mean I should just ignore the problem.
So, I go to troubleshooting the issue. I register one of the activities in my app to receive the the same type of intent, and then hit a breakpoint to analyze the Intent object being sent from the two different ways of sending it.
The problem is, the intent I'm sending and the intent being sent from Gallery or AndroZip (where Messages does show up in the chooser) seem to be the same. They both have the same action, same categories, same flags, same mime type. What else can I inspect on the Intent from Gallery or AndroZip to tell if there's some more information I can add to my Intent to get the default messaging app to show up in the chooser in cases where it is not?
The problem is specific to HTC Sense phones, and it arises because their Gallery and Messaging apps are different to the stock ones.
Specifically the Intent sent from Gallery to Messaging has the action android.intent.action.SEND_MSG which is different to android.intent.action.SEND. The Sense messaging app doesn't handle SEND, unlike the stock messaging app.
So the question becomes, how is the Sense Gallery app creating an activity chooser dialog which combines both SEND and SEND_MSG ?
I've done some research and got mostway there... the code below works, but the "Messages" entry in the dialog appears at the top rather than in alphabetical order as per Gallery. Doubtless some more research into intents would correct that, but at least this works:
// Create a chooser for things that can ACTION_SEND images
Intent intent = new Intent(Intent.ACTION_SEND);
Uri data = Uri.parse("content://media/external/images/media/98");
intent.putExtra(Intent.EXTRA_STREAM, data);
intent.setType("image/jpeg");
Intent chooser = Intent.createChooser(intent, "Blah");
// Add the stupid HTC-Sense-specific secondary intent
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.putExtra(Intent.EXTRA_STREAM, data);
htcIntent.setType("image/jpeg");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
// Show the chooser
startActivity(chooser);
First of all, +1 to Reuben, he is the genius, not me. But I had to modify his code a bit to get it to work. Basically I had to putExtra() on the htcIntent or the image never got stuck to the Intent.
Tested and validated on a Droid X and HTC Incredible (which had the same problem until now thanks to Reuben).
Uri uri = Uri.fromFile(new File(mFile));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.setType("image/png");
htcIntent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(intent, "Send Method");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
startActivity(chooser);
Edit: I realize I'm putting the image on two Intents now, but I couldn't get it to work any other way.
Instead of debugging the intents, why not try to compare how your starting the chooser with how the gallery is doing it. It is open source after all, so instead of trying to guess at the issue with the result, you can debug from the cause.
https://android.googlesource.com/platform/packages/apps/Gallery3D

Categories

Resources