Skip to content

Instantly share code, notes, and snippets.

@codixil
Created Sep 7, 2021
Embed
What would you like to do?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/display_dialog_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="הצג דיאלוג" />
</LinearLayout>
package com.codix.app;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button displayDialogBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayDialogBtn = findViewById(R.id.display_dialog_btn);
displayDialogBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayDialog();
}
});
}
public void displayDialog() {
AlertDialog dialog;
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
builder.setTitle("כותרת לדוגמה");
builder.setMessage("הודעה לדוגמה");
builder.setPositiveButton("כן", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "בחרת כן", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("לא", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "בחרת לא", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("סגירה", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "בחרת סגירה", Toast.LENGTH_SHORT).show();
}
});
dialog = builder.create();
dialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment