Android Pop-up message
I'm trying to get a pop up text box, with some disclaimer and app info at the start of my Android application's launch. Does anyone know how to im开发者_运维百科plement this? Also could it read from a txt file?
Thanks
If you want a Popup that closes automatically, you should look for Toasts. But if you want a dialog that the user has to close first before proceeding, you should look for a Dialog.
For both approaches it is possible to read a text file with the text you want to display. But you could also hardcode the text or use R.String to set the text.
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
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");
// add these two lines, if you wish to close the app:
finishAffinity();
System.exit(0);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
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()
}
精彩评论