How to create custom alert dialog in android?

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I am developing a sample app.I am able to show alert on button click having some tittle and button .But now I want to show a pop up window having username (Label) and text field (Edit field) and a button. on click on button.can I make another popup xml file for that ?

public void selfDestruct(View view) {          // Kabloey          Log.d("Naveen", "Test====");          System.out.println("----------------------ghfgjhf-----------------");          AlertDialog alertDialog = new AlertDialog.Builder(SecondActivity.this).create();          alertDialog.setTitle("Reset...");          alertDialog.setMessage("R u sure?");          alertDialog.setButton("OK", new DialogInterface.OnClickListener() {               public void onClick(DialogInterface dialog, int which) {                 //here you can add functions              } });          alertDialog.show();      } 

 

    

回答1:

custom_dialog.xml

                

Need to inflate custom_dialog.xml

final Dialog dialog = new Dialog(this);  dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Custom Alert Dialog");  final EditText editText = (EditText) dialog.findViewById(R.id.editText); Button btnSave          = (Button) dialog.findViewById(R.id.save); Button btnCancel        = (Button) dialog.findViewById(R.id.cancel); dialog.show(); 


回答2:

You can make a normal activity and set the android:theme to dialog:

   

and in your xml file for this activity you can decide the height and width of this dialog field. For example:

The important thing is the manifest part.



回答3:

Try to use: PopupWindow

You can find example here: http://android-er.blogspot.co.il/2012/03/example-of-using-popupwindow.html



回答4:

you can use Dialog , like this code :

final Dialog dialog = new Dialog(context); // dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.custom); dialog.setTitle("Title...");  // set the custom dialog components - text, image and button TextView text = (TextView) dialog.findViewById(R.id.text); text.setText("Android custom dialog example!"); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); 

if you want to remove title bar just use this code after define :

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 


回答5:

Try this...

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Reset...").setView(editText)    .setMessage("R u sure?").setCancelable(true)    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {     @Override     public void onClick(DialogInterface dialog, int arg1) {         //Do here whatever.....     } }); AlertDialog alert = builder.create(); alert.show(); } 


回答6:

its working for me

Dialog m_dialog;  m_dialog = new Dialog(BusinessDetail.this);             m_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);              LayoutInflater m_inflater = LayoutInflater.from(BusinessDetail.this);             View m_view = m_inflater.inflate(R.layout.rateus_popup, null);             myPopLay = (LinearLayout) m_view.findViewById(R.id.myPopLay); m_dialog.setContentView(m_view);             m_dialog.show(); 


回答7:

Here is xml code and activity code see it.

   

Activity file

package com.nkm.popup;  import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView;  public class PopupDemoActivity extends Activity implements OnClickListener { LinearLayout layoutOfPopup; PopupWindow popupMessage; Button popupButton, insidePopupButton; TextView popupText;  @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     init();     popupInit(); }  public void init() {     popupButton = (Button) findViewById(R.id.popupbutton);     popupText = new TextView(this);     insidePopupButton = new Button(this);     layoutOfPopup = new LinearLayout(this);     insidePopupButton.setText("OK");     popupText.setText("This is Popup Window.press OK to dismiss         it.");     popupText.setPadding(0, 0, 0, 20);     layoutOfPopup.setOrientation(1);     layoutOfPopup.addView(popupText);     layoutOfPopup.addView(insidePopupButton); }  public void popupInit() {     popupButton.setOnClickListener(this);     insidePopupButton.setOnClickListener(this);     popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.FILL_PARENT,             LayoutParams.WRAP_CONTENT);     popupMessage.setContentView(layoutOfPopup); }  @Override public void onClick(View v) {      if (v.getId() == R.id.popupbutton) {         popupMessage.showAsDropDown(popupButton, 0, 0);     }      else {         popupMessage.dismiss();     }   } } 


回答8:

Use the below coding to display a Pop up in android.

AlertDialog.builder builder=new AlertDilaog.Builder(this);     builder.setMessage("PopUP Example");     AlertDialog alert=builder.create();     alert.setTitle("");     alert.show();      new Handler.postDelayed(new Runnable()     {     @override     public void run()     {   //TODO     alert.dismiss();     }     },1*1000);     } 

Youtube link: https://www.youtube.com/watch?v=SEsVTTl6exg



回答9:

We will use the PopupWindow class to create the popup.

Another think I would like to mention is that we will use a 9 patch background image for the popup, so it will look more fancy. But of course you can skip it and put any background you want, or no background at all.

Create layout/main.xml file and add a button:

  

Create a new layout file: layout/popup_layout.xml that defines the layout of popup.

  

And now the most interesting part. Open the TestPopupActivity and fill it with below code. Carefully read the comments to understand what’s going on.

 public class TestPopupActivity extends Activity {  //The "x" and "y" position of the "Show Button" on screen. Point p;  @Override public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);     Button btn_show = (Button) findViewById(R.id.show_popup);    btn_show.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View arg0) {         //Open popup window        if (p != null)        showPopup(TestPopupActivity.this, p);      }    }); }  // Get the x and y position after the button is draw on screen // (It's important to note that we can't get the position in the onCreate(), // because at that stage most probably the view isn't drawn yet, so it will return (0, 0)) @Override public void onWindowFocusChanged(boolean hasFocus) {     int[] location = new int[2];    Button button = (Button) findViewById(R.id.show_popup);     // Get the x, y location and store it in the location[] array    // location[0] = x, location[1] = y.    button.getLocationOnScreen(location);     //Initialize the Point with x, and y positions    p = new Point();    p.x = location[0];    p.y = location[1]; }  // The method that displays the popup. private void showPopup(final Activity context, Point p) {    int popupWidth = 200;    int popupHeight = 150;     // Inflate the popup_layout.xml    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);    LayoutInflater layoutInflater = (LayoutInflater) context      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);     // Creating the PopupWindow    final PopupWindow popup = new PopupWindow(context);    popup.setContentView(layout);    popup.setWidth(popupWidth);    popup.setHeight(popupHeight);    popup.setFocusable(true);     // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.    int OFFSET_X = 30;    int OFFSET_Y = 30;     // Clear the default translucent background    popup.setBackgroundDrawable(new BitmapDrawable());     // Displaying the popup at the specified location, + offsets.    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);     // Getting a reference to Close button, and close the popup when clicked.    Button close = (Button) layout.findViewById(R.id.close);    close.setOnClickListener(new OnClickListener() {       @Override      public void onClick(View v) {        popup.dismiss();      }    }); } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!