Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【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
You can’t perform that action at this time.