Im asking to see if anyone could help me with my problem which is that I have a custom dialog box in a java class. This custom dialog box has a button which when pressed will call a method from my activity class. When I run the code nothing happens, it seems that the method is never being called and also no errors are given, the reason that im trying it this way is because the java class is being used for overlayitems. Below is a snippit of the code that I have, cheers to anyone who has insight on the problem
Java Class for overlayitem
public boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
dialog.setTitle(item.getTitle());
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(item.getSnippet());
Button CallButton = (Button) dialog.findViewById(R.id.CallButton);
CallButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try {
TheActivityClass.showMessage();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}
);
dialog.show();
return true;
}
Activity Class
public void showMessage(){
Context context = getApplicationContext();
CharSequence text = "I have just been pressed";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
You have an encapsulation problem… The activity class is not on the top of the current stack and the showMessage() method is not static.
You should be controlling the application logic from inside the Activity class not the Java overlay class (follow the MVVM logic ie MVC where the activity is your controller). The best option would be to encapsulate the dialog/overlay object in the Activity and set the logic within the Activity class (create appropriate methods in the overlay class to do this) or to just make your overlay and inner class of the Activity. This will allow you do to what you are trying to do.
Hope that helps.