Android: overriding an intent's option menu - android

Suppose i wrote a camera app by using and intent(MediaStore.ACTION_IMAGE_CAPTURE) and i notice that when i run the app, the option menu displays "Edit shortcuts", how can i add on to this option menu or completely override the option menu to my own?
Below are snippet of my code:
public class GPSCam extends Activity implements LocationListener,
GpsStatus.Listener {
private Intent camIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpscam);
...
...
this.startCamera();
}
protected void startCamera() {
...
// create camera intent
camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// define where to keep the photo
camIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
// this method will call onActivityResult() upon finishing an activity
// i.e. image captured
startActivityForResult(camIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
...
...
...
}
}

You cannot modify the options menus of other applications. If you do not want to use another application for taking photos, you are welcome to use the Camera object and take photos yourself.

Related

onActivityResult never called from widget launched activity

I'm new here. I discover how StackOverflow works.
I'm creating a new android homescreen widget. My widget has a button. When pressed, it starts an activity (it's not a configure Activity, just a standard activity). In this activity, I have a test button. My purpose is to create a text file after pressing this test button.
In onCreate function, I have this code to handle the button :
final Button testButton = findViewById(R.id.button_test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ActivityCompat.requestPermissions(WidgetActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
Utils.MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
String fileName = "test.txt";
Intent exportIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
exportIntent.addCategory(Intent.CATEGORY_OPENABLE);
exportIntent.setType("text/plain");
exportIntent.putExtra(Intent.EXTRA_TITLE, fileName);
startActivityForResult(exportIntent, FILE_EXPORT_REQUEST_CODE);
}
});
And I have this function :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case FILE_EXPORT_REQUEST_CODE:
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Context c = WidgetActivity.this;
ParcelFileDescriptor pfd = null;
try {
pfd = c.getContentResolver().openFileDescriptor(uri, "w");
Preferences.export(mAppWidgetId, pfd.getFileDescriptor(), WidgetActivity.this);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
break;
}
}
onActivityResult is never called.
I put this code in my MainActivity, and it works very well.
I don't know how to achieve this...
It's seems to be impossible.
The way I achieve this :
My widget starts its activity (not the MainActivity), as usual, and one feature offer the possibility to create a file.
The button in this activity create a new intent with action code, and launch MainActivity.
MainActivity use action code to decide what to do : execute code to create the file.
MainActivity have a onActivityResult called when user choose a file name and a directory.

How to open new activity when a qr code is scanned?

hi i am currently working on QR code scanner. I want to ask if it is possible to open another activity after the QR code have been scanned rather than getting it displayed as a toast?
below is my scanner file code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtexthello=(TextView)findViewById(R.id.textview_hello);
scanbtn=(Button)findViewById(R.id.btn1) ;
final Activity activity=this;
scanbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator l=new IntentIntegrator(activity);
l.setDesiredBarcodeFormats(l.QR_CODE_TYPES);
l.setPrompt("scan");
l.setCameraId(0);
l.setBeepEnabled(false);
l.setBarcodeImageEnabled(false);
l.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult res= IntentIntegrator.parseActivityResult(requestCode, resultCode,data);
if(res!=null)
if(res.getContents()==null)
{
Toast.makeText(this,"u cancelled scanning",Toast.LENGTH_LONG).show();
}
else
{
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
// Toast.makeText(this,res.getContents(),Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
At a first reading it sounds as if the issue is how you're attempting to start the next activity, if not the activity itself.
Start a new app, and use a simple Button as the trigger for the new activity. Once you have the syntax and requirements correct there, apply what you've learned to the QR app.
If you're able to get the Toast to display from your existing code, then your difficulty has nothing to do with the QR code.
Maybe what you really need is to look at something like this (Start an Activity with a parameter) where you pass a parameter to the second activity.

Loading Images from Dropbox in my app

I am making a activity whereby i ask the user to load image from various sources and showing the selected image on a imageview in a different activity.
I am able to show the image from the camera and gallery but from dropbox it is showing error.
For Camera and gallery i can even query the uri obtained from
intent.getData()
in the onactivityresult method and obtain the Filepath and accordingly even obtain the bitmap and resize it .
But the same is not working for Dropbox. Kindly update what code to use for Dropbox so that all options start working.
thanks
For Dropbox you will need to use their Android Chooser to choose files from the user's Dropbox account. You will need a Dropbox API key for this. Once you have grabbed the chooser library and your API key it should be fairly easy to implement;
private Button mChooserButton;
private DbxChooser mChooser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChooser = new DbxChooser(APP_KEY);
mChooserButton = (Button) findViewById(R.id.chooser_button);
mChooserButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mChooser.forResultType(DbxChooser.ResultType.PREVIEW_LINK)
.launch(MainActivity.this, DBX_CHOOSER_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DBX_CHOOSER_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
DbxChooser.Result result = new DbxChooser.Result(data);
Log.d("main", "Link to selected file: " + result.getLink());
// Handle the result
} else {
// Failed or was cancelled by the user.
}
} else if (requestCode == GALLERY) {
// If your request was from the user gallery
Log.d("main", "Link to selected file: " + data.getData());
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

how to add button or Menu in Contacts list?

I am using following code to open Android Contacts
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnContacts = (Button) findViewById(R.id.btn_contacts);
txtContacts = (TextView) findViewById(R.id.txt_contacts);
btnContacts.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
txtContacts.setText("");
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
//display picked contact data.
}
}
}
Now I want to put Button at the top of this Contact activity when opened or add my own Menu in this Activity
Can any one guide me? Is this possible or not? If yes then please tell how to achieve this?
I don't believe that is possible as each Activity in Android is working on its own and by starting the Intent, you are basically giving the new Activity the focus (and control).
One way to do something like this would be to build a custom contact list activity that uses the public data providers to access the contacts and simply lists them then. Then you could add as many custom functions as you like or even add Intents for original actions (like viewing a contact's details).

setting picture captured by built-in camera into an ImageView

in my app i m calling built in camera for capturing picture but i want to set that picture into an image view following is the code . so what code should i add to it to set the picture into imageview.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
// ...
}
}
Thanks in advance
Add a button and an ImageView to your main layout. When you click the button, first intent.putExtra to specify where the image will be stored, then start the camera intent. In your onActivityResult, if the resultCode comes back 0 that means the user accepted the picture. In this case, go grab the image from the path you specified as an extra of the intent. Create a bitmap from the file specified by the path, then set the image bitmap in your ImageView.

Categories

Resources