Solution:
Main.xml
<?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"
tools:context="MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Alert Dialog"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to show custom alert"
android:id="@+id/button"
android:layout_marginTop="62dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
.java file
package com.example.alertdialog_greet;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button closeButton;
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
closeButton = (Button) findViewById(R.id.button);
builder = new AlertDialog.Builder(this);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Setting message manually and performing action on button click
builder.setCancelable(false)
.setPositiveButton("Isha", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),"Hello Isha",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Sofiya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),"Hello Sofiya",
Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("Masirah", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),"Hello Masirah",
Toast.LENGTH_SHORT).show();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("My Custom Alert");
alert.show();
}
});
}
}
custom_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:background="@android:color/background_dark"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_b1"
android:layout_margin="10dp"
android:text="Isha" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_b2"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Sofiya" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_b3"
android:layout_margin="10dp"
android:text="Masirah" />
</LinearLayout>
</RelativeLayout>
0 Comments