-
-
Save codingwithsara/9c31590cd735f9f51fdcf179ce4edc09 to your computer and use it in GitHub Desktop.
【Android Studio】How to Display Custom AlertDialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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