First declare your warning and button in MainActivity:
public class Mainactivity extends Activity { private AlertDialog.Builder alert; private Button btAbout;
Then in onCreate (), create your warning, just like you, except for this line:
alert.show(); // <--- remove this line as not to show the alert immediately
Since you declared a global warning, be sure to also remove AlertDialog.Builder, so instead:
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("About");
you must have:
alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("About");
Then, take the handle of your button:
btAbout = (Button) findViewById(R.id.about);
Set onClickListener to the button:
btAbout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
All this in onCreate (). Now when the button is pressed, your warning will appear.
Melquiades
source share