Suppose you want to set a pop-up text box for clicking a button lets say bt whose id is button, then code using Toast will somewhat look like this:
Button bt;
bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"The text you want to display",Toast.LENGTH_LONG)
}
sample code show custom dialog in kotlin:
fun showDlgFurtherDetails(context: Context,title: String?, details: String?) {
val dialog = Dialog(context)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.dlg_further_details)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val lblService = dialog.findViewById(R.id.lblService) as TextView
val lblDetails = dialog.findViewById(R.id.lblDetails) as TextView
val imgCloseDlg = dialog.findViewById(R.id.imgCloseDlg) as ImageView
lblService.text = title
lblDetails.text = details
lblDetails.movementMethod = ScrollingMovementMethod()
lblDetails.isScrollbarFadingEnabled = false
imgCloseDlg.setOnClickListener {
dialog.dismiss()
}
dialog.show()
}
Use This And Call This In OnCreate Method In Which Activity You Want
public void popupMessage(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("No Internet Connection. Check Your Wifi Or enter code hereMobile Data.");
alertDialogBuilder.setIcon(R.drawable.ic_no_internet);
alertDialogBuilder.setTitle("Connection Failed");
alertDialogBuilder.setNegativeButton("ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.d("internet","Ok btn pressed");
finishAffinity();
System.exit(0);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
You can use Dialog to create this easily
create a Dialog instance using the context
Dialog dialog = new Dialog(contex);
You can design your layout as you like.
You can add this layout to your dialog by
dialog.setContentView(R.layout.popupview);//popup view is the layout you created
then you can access its content (textviews, etc.) by using findViewById
method
TextView txt = (TextView)dialog.findViewById(R.id.textbox);
you can add any text here. the text can be stored in the String.xml file in res\values.
txt.setText(getString(R.string.message));
then finally show the pop up menu
dialog.show();
more information http://developer.android.com/guide/topics/ui/dialogs.html
http://developer.android.com/reference/android/app/Dialog.html
Source: Stackoverflow.com