In this example, there are three alert dialog boxes are shown. If you wish to display only ok button then just keep the setNeutralButton and remove the setPositiveButton and setNegativeButton.
In the alert_dialog_activity.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f08442"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:gravity="top"
android:weightSum="2">
<Button
android:id="@+id/one_optionBTN"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#f06d63"
android:text="One option"
android:onClick="oneOption"
android:textSize="15sp"
android:layout_marginRight="5dp"
android:textColor="#ffffff"/>
<Button
android:id="@+id/two_optionsBTN"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ff3f3f"
android:text="Two Options"
android:layout_marginLeft="5dp"
android:onClick="twoOptions"
android:textColor="#ffffff"
android:textSize="15sp"/>
</LinearLayout>
<Button
android:id="@+id/three_optionsBTN"
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#f05442"
android:text="Three Options"
android:layout_marginLeft="5dp"
android:onClick="threeOptions"
android:textSize="15sp"
android:textColor="#ffffff"/>
</LinearLayout>
package com.app.yourappname;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class AlertDialogActivity extends Activity
{
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.alert_dialog_activity);
}
public void oneOption(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(this)
.setTitle("This is TITLE!")
.setMessage("This is MESSAGE!")
.setCancelable(true)
.setIcon(R.mipmap.ic_launcher);
b.setNeutralButton(android.R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(AlertDialogActivity.this, "Neutral Button Pressed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
b.show();
}
public void twoOptions(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(this)
.setTitle("This is TITLE!")
.setMessage("This is MESSAGE!")
.setCancelable(true)
.setIcon(R.mipmap.ic_launcher);
b.setNeutralButton(android.R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(AlertDialogActivity.this, "Neutral Button Pressed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(AlertDialogActivity.this, "Positive Button Pressed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
b.show();
}
public void threeOptions(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(this)
.setTitle("This is TITLE!")
.setMessage("This is MESSAGE!")
.setCancelable(true)
.setIcon(R.mipmap.ic_launcher);
b.setNeutralButton(android.R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(AlertDialogActivity.this, "Neutral Button Pressed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(AlertDialogActivity.this, "Positive Button Pressed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(AlertDialogActivity.this, "Negative Button Pressed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
b.show();
}
}
In the manifest file, add the Activity name: <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".AlertDialogActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------No Changes in the build.gradle (Module:app)------------------
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.app.yourappname"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
No comments:
Post a Comment