Home »
Android
Toast the result of dialog in Android
In this article, we are going to set two different options in a dialog box and toast the closable option as result.
Submitted by Manu Jemini, on January 30, 2018
In the example below, we are going to use the AlertDialog.Builder class to build an alert box. This AlertBox.Builder will ask for a question and provide two buttons for yes and no.
To implement this in your program you will need to import the ‘android.support.v7.app.AlertDialog’ in your java file. Then create a layout file with a single button in the center with any text for the button.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here.."
android:onClick="open"
android:layout_marginLeft="115dp"
android:layout_marginTop="150dp" />
As you can see the <button /> has a android:onClick attribute which is referring to open, now open is a function which is initiating the function for the process in JAVA file. It is easy and simple to create an AlertDialoge.Builer in JAVA file.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
The above line will make a Dialog and initiate it on the screen, when user clicks on any button of the dialog according to the choice we will make a Toast. That is:
Toast.makeText(MainActivity.this,"Answer is YES",Toast.LENGTH_LONG).show();
Now you are ready to use it in your project.
Java file:
package com.example.hp.myapplication;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open(View view){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Is includehelp helpful");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"Answer is YES",Toast.LENGTH_LONG).show();
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Answer is NO",Toast.LENGTH_LONG).show();
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hp.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here.."
android:onClick="open"
android:layout_marginLeft="115dp"
android:layout_marginTop="150dp" />
</RelativeLayout>
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output Images