Reputation: 233
I once read a tutorial but since then my phone had been wiped out by XBOX, and so I lost it. I want there to be a button on the MainActivity to open up an AlertDialog Box stating app info such as version number, about current version, what android version this one is for, etc.
package com.apw.games.rpg.medieval;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.*;
import android.util.*;
import android.graphics.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu); return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.quit:
Intent intent = new Intent(this, Exit.class);
startActivity(intent);
return true;
case R.id.new_game:
Intent i = new Intent(this, New_Game.class);
startActivity(i);
return true;
case R.id.visit_site:
Intent inte = new Intent(this, Site.class);
startActivity(inte);
return true;
default: return super.onOptionsItemSelected(item);
}}
Upvotes: 1
Views: 8078
Reputation: 504
// here is a snippet code work for me
new AlertDialog.Builder(this)
.setTitle("Mobile Raksha")
.setMessage(
"Your Message")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
finish();
}
}).show();
Upvotes: 2
Reputation: 7304
The above explanations are good. There are four types of android dialog boxes based on the class types as AlertDialog,Progress Dialog,DatePickerDialog, TimePickerDialog. We can choose based on the need. If its plain dialog to display the messages, just use AlertDialog. If you want to read step by step process to create a dialog, please go through this simple example on how to create alert dialog box in android.
Upvotes: -1
Reputation: 649
First of all declarate the AlertDialog type object:
AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();
Main.this is my activity's context. You can set your dialog's title like this:
alertDialog.setTitle("Title");
And a message:
alertDialog.setMessage("Your text");
Next thing, set your button(s) functions:
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//here you can add functions
} });
And you can change the icon of your AlertDialog using this line:
alertDialog.setIcon(R.drawable.icon);
Last thing, don't forget to show your dialog:
alertDialog.show();
Upvotes: 6
Reputation: 8645
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this)
.setTitle("alert dialog")
.setMessage("message")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Activity.this.finish();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Activity.this.finish();
}
})
.show();
Upvotes: 2