Skip to content

Instantly share code, notes, and snippets.

@codinginflow
Last active April 25, 2023 03:00
Show Gist options
  • Save codinginflow/58ff1f0e84be860312f6379d58e2a4b6 to your computer and use it in GitHub Desktop.
Save codinginflow/58ff1f0e84be860312f6379d58e2a4b6 to your computer and use it in GitHub Desktop.
Simple AlertDialog Tutorial
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.application.example.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="open dialog" />
</RelativeLayout>
package com.example.application.example;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
public class ExampleDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Information")
.setMessage("This is a Dialog")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
}
package com.example.application.example;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
ExampleDialog exampleDialog = new ExampleDialog();
exampleDialog.show(getSupportFragmentManager(), "example dialog");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment