I'm trying to get a class extending ItemizedOverlay to startActivity but there is a problem, it just won't compile.
I have a MapView that uses the ItemizedOverlay class to draw overlays but i want to start and activity when i tap on the screen.
Any idea how to solve this problem? Thanks
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
String split_items = item.getSnippet();
Intent intent = new Intent();
intent.setClass(mainmenu,poiview.class);
startActivity(intent);
return true;
}
I had this problem so I have tested the example below.
The solution relies on calling "startActivity" from the MapActivity context.
If your map is actually working with overlays then you have already passed the MapView Context to your custom ItemizedOverlay constructor and you probably assigned the MapView Context to a class variable called mContext (I'm making assumptions that you followed Google's MapView example). So in your custom Overlay's onTap function, do this:
#Override
protected boolean onTap(int index) {
Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
mContext.startActivity(intent);
return true;
}
But you probably want to pass something to the new Activity you are trying to launch so your new activity can do something useful with your selection. So...
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
//assumption: you decided to store an "id" in the snippet so you can associate this map location with your new Activity
long id = Long.parseLong(item.getSnippet()); //Snippet is a built-in String property of an Overlay object.
//pass an "id" to the class so you can query
Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
String action = Intent.ACTION_PICK; //You can substitute with any action that is relevant to the class you are calling
//I create a URI this way because I append the id to the end of the URI (lookup the NotePad example for help because there are many ways to build a URI)
Uri uri = ContentUris.withAppendedId(Your_CONTENT_URI, id);
//set the action and data for this Intent
intent.setAction(action);
intent.setData(uri);
//call the class
mContext.startActivity(intent);
return true;
}
Try this one , i have done it with setting a positive button in the alert dialog...
protected boolean onTap(int index) {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setIcon(R.drawable.info_icon);
dialog.setPositiveButton("Details", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent placeDetailsMapIntent = new Intent(mContext, PlaceDetailsActivity.class);
mContext.startActivity(placeDetailsMapIntent);
}
});
try to use following
Intent intent = new Intent(mainmenu.this, poview.class);
startActivity(intent);
Related
I am trying to fix up my app code and transition from Activities to Fragments, in order to transition to the navigation draw and eventually have some sliding tabs too.
Currently for navigation I am using the 3 dots drop down menu from the action bar which I know is not really right. Which is why I am trying to transition to fragments.
I want to keep my action bar menu so I can keep my search icon in the action bar but I want to add a navigation draw to it.
I currentley have a class called ActionBarMenu which extends activity. All my activities come form this because this class holds all the functions which open new activities for the action bar menu I have set up right now.
public class ActionbarMenu extends Activity {
public void goToSearch (MenuItem item) {
// go to search page
Intent i = new Intent(this,Search.class);
startActivity(i);
}
public void goToStatistics (MenuItem item) {
// go to search page
Intent i = new Intent(this,StatisticsPage.class);
startActivity(i);
}
public void goToFindBrewery (MenuItem item) {
// go to search page
Intent i = new Intent(this,FindBrewery.class);
startActivity(i);
}
public void goToContact (MenuItem item) {
// go to search page
Intent i = new Intent(this,ContactPage.class);
startActivity(i);
}
public void goToPortfolio (MenuItem item) {
// go to search page
Intent i = new Intent(this,Portfolio.class);
startActivity(i);
}
public void goToDiscover (MenuItem item) {
// go to search page
Intent i = new Intent(this,Discover.class);
startActivity(i);
}
public void scanBarcode (MenuItem item) {
//open scanner
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
//get result value
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
// we have a result
String scanContent = scanningResult.getContents();
// launch async task for results
String url = "http://api.brewerydb.com/v2/search/upc?code=" + scanContent + "&key=0cdb22f84315834cc807c0176a927060&format=json&withBreweries=y";
new GetBeerDataFromUPCJSON(this).execute(url);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
I tried to change it to a fragment by changing extends activity to extends Fragment and all intents I build to launch new activities all get the red squiggly underline. I am just experimenting and trying to learn what's going on and whats the best way to transition my app from activities to Fragments, without having to recode everything.
Your problem is probably because in a line such as
Intent i = new Intent(this, Discover.class);
this now refers to a Fragment instead of an Activity. Intent() expects a Context as the first argument; Activity extends Context, but Fragment does not. To fix your problem, you should change this to getActivity().
For future reference, mousing over the little red x that appears next to the error should tell you exactly what the problem is.
Instead of using intents, you'll use the fragment manager to load new fragments. Read this page on the android website about fragments.
The finished code for quick replacement would be:
Intent i = new Intent(getActivity(), Discover.class);
startActivity(i);
you just need to make it same like backpressed if the activity is just next from your fragment like... it should work ....
#Override
public void onClick(View view) {
onBackPressed();
}
Intent i = new Intent(this, Discover.class);
I'm new to android and trying to find out how to show a new screen when the user clicks something in the menu item.
I'm using ActionbarSherlock and looking at the sample github-android app.
When the user clicks on an item in the menu, I want to show them a new screen. Github code is doing that like so:
startActivityForResult(new Intent(getActivity(), CreateGistActivity.class), GIST_CREATE);
But I've seen some code samples do:
Intent i = new Intent(getApplicationContext(), SomeActivity.class);
My code looks like this:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create) {
//show createactivity class
return true;
}
return true;
}
What is the right way to do ?
You can do it just like that:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create) {
//show createactivity class
Intent i = new Intent(MainActivity.this, SomeActivity.class);
startActivity(i);
return true;
}
return true;
}
startActivityForResult is used when you have to return some value/data to the first screen like a user selection. More here
As far as the context to use getActivity() or getApplicationContext(), I prefer to use the context of current activity MainActivity.this its more straitforward similar to documentation example
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Inside a fragment use getSherlockActivity() instead of getActivity() as getActivity() can cause crashes to older devices.
Of course getApplicationContext() would always work and not crash but I feel that it may mess the garbage collector and do not let activities to be cleared (but not sure about it)
Just use startActivityForResult
There is no 'right' way. The Github code doesn't first declare the variable. The onther does. I believe for a menu, you normally need to declare the Intent as a local variable, if not a field.
Create an intent: Intent i = new Intent(MainActivity.this, CreateGistActivity.class);
where MainActivity is the activity you're in, and CreateGistActivity is the class you want to launch.
Then use startActivity(Intent) to launch the new activity: startActivity(i);
Or just combine them:
startActivity(new Intent(MainActivity.this, CreateGistActivity.class));
Full code:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create)
{
Intent i = new Intent(MainActivity.this, CreateGistActivity.class);
startActivity(i);
return true;
}
return true;
}
startActivityForResult probably isn't needed in your case, unless you're expecting to send values between the classes.
I have this scenario:
Main activity with one button. Once clicked it launchs 'mapActivity'
Map activity (extending MapActivity) uses an ItemizedOverlay (extending ItemizedOverlay) class to mark places over the map.
Now, I want to launch another activity from my itemized overlay. I achieved to launch one activity as standar intent this way:
#Override
public boolean onTap(GeoPoint point, MapView mapView) {
boolean tapped = super.onTap(point, mapView);
if (!tapped){
Intent intent = new Intent();
intent.setClass(this.context, EditarLugarActivity.class);
this.context.startActivity (intent);
return true;
}
EditarLugarActivity is an activity whichs obtain a text input from user. Now, I need to get that text from the Itemized Overlay activity. Context have the value from map activity context and is set in constructor in this way:
public MiItemizedOverlay(Context context, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
this.context = context;
populate();
}
To do this I've tried to use 'sartActivityForResult' instead 'this.content.startActivity(intent) in this way:
#Override
public boolean onTap(GeoPoint point, MapView mapView) {
boolean tapped = super.onTap(point, mapView);
if (!tapped){
Intent intent = new Intent();
intent.setClass(this.context, EditarLugarActivity.class);
this.context.startActivityForResult (intent,1);
return true;
}
but is not recognized by the compiler. 'this.context' is not working as it work for a simple 'startActivity'
Someone can help me with this? Thanks!
EDIT-
Adding my MiItemizedOverlay class first line with 'extends':
public class MiItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context; [....]
you can try
Intent intent = new Intent();
intent.setClass(getApplicationContext, EditarLugarActivity.class);
this.context.startActivityForResult (intent);
or
Intent i = new Intent(this, EditarLugarActivity.class);
startActivityForResult(i, 1);
There is no no method called startActivityForResult which takes only one argument.
Use, context.startActivityForResult(intent, 1); Here the second argument is the result code which would be returned back to you from EditarLugarActivity when finish() is called on that activity. You can define your own result code. And then you need to implement onActivityResult(int requestCode, int resultCode, Intent data) in your activity to catch this result code.
EDIT:
StartActivityForResult method is from Activity class. So you need to call it as below
((Activity) this.context).startActivityForResult(intent, 1);
Make sure that your context is indeed an Activity context, and implement the onActivtyResult in the mapActivity.
I am using the google maps api with overlay items and want to be able to get directions when the user clicks these overlays.
I want this linked to the middle button of my dialog:
// Middle button
dialog.setNeutralButton("Directions",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}
});
I get the following error "The method startActivity(Intent) is undefined for the type new DialogInterface.OnClickListener(){}"
There are several answers to this on here but I cannot get any of them to work. I have tried both creating a constructor and calling getContext().
I am not sure if it is because my class is:
public class ItemizedOverlayLayoutActivity extends ItemizedOverlay {
Any help greatly appreciated.
Thanks
In the constructer of your class ItemizedOverlayLayoutActivity add a parameter Context:
private Context context;
public MyItemizedNewOverlay(Drawable defaultMarker, Context context) {
//...
this.context = context;
}
just before you create your dialog add:
final Context fcontext = context;
Then in the dialog middle button use:
fcontext.startActivity(intent);
good luck
I have an activity that has a button which opens a new MapActivity to select a location by tapping on the map.
The map has an overlay that overrides the onTap method to get the location but I want to return that location to the previous activity but, I don't know how to return the geopoint to the mapactivity in order to call the setResult() and finish() methods, because I can't call them from the Overlay.onTap method.
Any ideas?
Solved this way:
class tapOverlay extends Overlay
{
public GeoPoint lastTap=null;
String strCalle;
private Context context;
public tapOverlay(Context c)
{
this.context=c;
}
}
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
lastTap = p;
mapView.getController().animateTo(p);
...
strCalle = sb.toString(); //from geocoder
...
devolverResultado();
return true;
}
private void devolverResultado()
{
MapActivity ma = (MapActivity) context;
Intent i = new Intent();
Bundle b = new Bundle();
b.putInt("dlat", lastTap.getLatitudeE6());
b.putInt("dlng", lastTap.getLongitudeE6());
b.putString("calle",strCalle);
i.putExtras(b);
ma.setResult(Activity.RESULT_OK, i);
ma.finish();
}
Call the new activity using an intent ...
Then , Use onActivityResult( int , int , Intent) to call the new activity from the current activity.....
U should get back the data from the new activity when u finish the called activity as the calling activity is placed on the stack ...
Hope this helps ... :)