Android starting a google maps intent from within a dialog - android

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

Related

Get shared element transitions inside the recylerView

I need a shared transition in recylerview to an activity but getting the error "Application cannot be cast to Activity" tried passing Adpater.this/this/Adapter.class but nothing is working
ViewCompat.setTransitionName(holder.imageView, photo.getId());
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, Fullscreen.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((MainActivity) mContext, holder.imageView, ViewCompat.getTransitionName(holder.imageView));
mContext.startActivity(intent, options.toBundle());
}
});
}
problem is in (MainActivity)mContext line. Your mContext is Application type but you try to cast it to Activity type, thats why it throws ClassCastException. Declare your field as Activity mContext; instead of Context mContext; and IDE will show you that you init your mContext variable with wrong type object.

How to open a small window in non activity class in android

I am trying to find a way to open a small window which along with a button in non activity class. Basically i tried to use this way:
public class Mytest{
private Context context;
public Mytes(Context context)
{
this.context = context.getApplicationContext();
}
AlertDialog alertDialog = new AlertDialog.Builder(context).create(); //Use context
alertDialog.setTitle("Warning");
alertDialog.setMessage("You are currently in a battle");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
but getting exception that
Unable to add window -- token null is not for an application
I have seen a thread in which someone suggested to use activity instead of context. i have try with both way to set context into nonactivity class from my main activity:
mytest = new Mytest(MainActivity.this);
but it is not working. Any other solution for this.
try to edit your constructor like this:
public Mytes(Context context) {
this.context = context;
}
Instead of getApplicationContext(), just use ActivityName.this.

how to launch an activity with 'sartActivityForResult' from an ItemizedOverlay class

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.

passing this to an outer class and starting an activity from alertdialog's list on Android

I am developing an Android app, this app has a dozen of Activities, each one is for a corresponding screen. Now I have this common subtitle bar on top of the screens.
this subtitle bar has a button to display an alert dialog which shows link list to go to a different screen.
I could write a same function for each activity to call the alert dialog, but that would be tedious if I want to modify them, so I created this class:
public class MenuAlertDialog extends Activity {
/*
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
*/
public void createMenu(final Context context){
AlertDialog.Builder dlg = new AlertDialog.Builder(context);
dlg.setTitle("menu");
String[] items = {"pageA", "pageB", "pageC", "pageD", "pageE"};
dlg.setItems(items, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which){
switch(which){
case 0:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
dlg.show();
}
}
and call it from each activity, like this:
MenuAlertDialog menu = new MenuAlertDialog();
menu.createMenu(this);
from inside of onCreate.
It can display the alertDialog, but whenever I press pageA link, it fails with an unexpected error.
Logcat says its a nullpointererror and the cause seems
startActivity(intent);
What am I doing wrong?
Remove the code
extends Activity
as you have no need to extend your class that you are creating since it does not rely on any activity related functionality.
Where you call startActivity(intent); replace it with
context.startActivity(intent);
You should change the class to Extends Dialog and not activity.
Also for Try this:
Check out this tutorial on how to create a custom dialog. Custom Dialog
Also Here Another Tutorial
And Here

Start Activity from ItemizedOverlay class

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);

Categories

Resources