How to add Android exit dialog with rating and quit button on Android application

How to add Android exit dialog with rating and quit button on Android application

How to add Android exit dialog with rating and quit button on Android application

In this tutorial we are going to implement an exit dialog box for our android application. When user clicks back button on applications main activity then exit dialog box is invoked and displayed

with a custom message and 2 buttons “Rate us” and “Quit”.

Example Screen shot

device-2016-11-04-200851

 

Here is the code snippet

public class MainHomeActivity extends AppCompatActivity
{
 AlertDialog.Builder builder;
protected void onCreate(Bundle savedInstanceState)
{
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.mainpage);
}
 @Override
 public void onBackPressed()
 {
 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
 builder = new AlertDialog.Builder(MainHomeActivity.this);
 } 
 else 
 {
 builder = new AlertDialog.Builder(MainHomeActivity.this, AlertDialog.BUTTON_NEUTRAL);
 }
 builder.setTitle("Thank You");
 builder.setMessage("Thank You For Using Our Application Please Give Us Your Suggestions and Feedback ");
 builder.setNegativeButton("RATE US",
 new DialogInterface.OnClickListener() 
 {
 public void onClick(DialogInterface dialog,
 int which)
 {
 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=ADD YOUR APPS PACKAGE NAME")); // Add package name of your application
 startActivity(intent);
 Toast.makeText(MainHomeActivity.this, "Thank you for your Rating", 
 Toast.LENGTH_SHORT).show();
 }
 });
 builder.setPositiveButton("QUIT",
 new DialogInterface.OnClickListener() 
 {
 public void onClick(DialogInterface dialog,
 int which)
 {
 finish();
 }
 });
 
 builder.show();
 }
}