Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created September 21, 2020 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/9c31590cd735f9f51fdcf179ce4edc09 to your computer and use it in GitHub Desktop.
Save codingwithsara/9c31590cd735f9f51fdcf179ce4edc09 to your computer and use it in GitHub Desktop.
【Android Studio】How to Display Custom AlertDialog
package com.codingwithsara.customalertdialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showAlertDialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
});
LayoutInflater inflater = this.getLayoutInflater();
View customAlertView = inflater.inflate(R.layout.custom_alert, null);
builder.setView(customAlertView);
TextView msg = customAlertView.findViewById(R.id.msg);
msg.setText("Hello World!");
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment