Android Custom Popup effect

Source: Internet
Author: User
Tags gety

Android 's pop-up effect there are many kinds of, in the simplest case, you can call a alertdialog pop-up window display, but to customize the pop-up window effect has the following method, I personally feel very convenient, applicability is quite wide.

First of all, write a simple use of alertdialog

public void ShowDialog () {alertdialog dialog = new Alertdialog.builder (this). Settitle ("hint"). Setmessage (Getresources () . getString ("Are you sure you want to quit?"). Setpositivebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) { Dialog.dismiss ();}}). Setnegativebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO A uto-generated method Stub}}). Create ();d ialog.show ();}}



The next step is to customize the pop-up window:






Here is the sample code:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/     Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:orientation=" vertical " > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Androi        D:gravity= "Center_horizontal" android:orientation= "vertical" android:layout_alignparentbottom= "true" Android:id= "@+id/top" > <textview android:layout_width= "match_parent" android:layout_height = "40.5dip" android:text= "@string/select_head" android:layout_marginleft= "7.2dip" Android:        layout_marginright= "7.2dip" android:gravity= "center" android:textcolor= "#aaaaaa"/> <button android:id= "@+id/takephoto" android:layout_width= "Match_parent" android:l           ayout_height= "40.5dip" android:layout_marginleft= "7.2dip" android:layout_marginright= "7.2dip" android:text= "@string/takeph            Oto "android:background=" @null "android:textcolor=" #fc4643 "/> <button Android:id= "@+id/selectfromalbum" android:layout_width= "Match_parent" android:layout_height= " 40.5dip "android:layout_marginleft=" 7.2dip "android:layout_marginright=" 7.2dip "android:t         ext= "@string/takefromalbum" android:background= "@null" android:textcolor= "#fc4643"/> <button android:id= "@+id/cancel" android:layout_width= "Match_parent" android:l            ayout_height= "40.5dip" android:layout_marginleft= "7.2dip" android:layout_marginright= "7.2dip"            android:layout_margintop= "7.2dip" android:text= "@string/cancel" android:background= "@null" Android:textcolor= "#fc4643"/> </LinearLayout></RelativeLayout> 



public class Selectpopupwindow extends Popupwindow {Button takephoto; Button Selectfromalbum; Button Cancel; View menuview;public Selectpopupwindow (Context ctx, Onclicklistener onclicklistener) {Layoutinflater Inflater = ( Layoutinflater) Ctx.getsystemservice (context.layout_inflater_service); Menuview = Inflater.inflate (R.layout.modify _head, NULL); You can use a variety of styles, custom Viewtakephoto = (Button) Menuview.findviewbyid (R.id.takephoto); Takephoto.setonclicklistener ( Onclicklistener); selectfromalbum = (Button) Menuview.findviewbyid (r.id.selectfromalbum); Selectfromalbum.setonclicklistener (onclicklistener); cancel = (Button) Menuview.findviewbyid (r.id.cancel); Cancel.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubdismiss ();}}); Setcontentview (Menuview); SetWidth (layoutparams.match_parent); SetHeight (layoutparams.wrap_content); setFocusable   (true);   Pop-up form can be clicked//setanimationstyle (R.style.animbottom); Set the animation Menuview.setontouchlistener (new OntouchliStener () {@Overridepublic Boolean onTouch (View V, motionevent event) {int height = Menuview.findviewbyid (r.id.top). GetTop (); LOG.E ("Huang", "Top height:" +height+ "y:" +event.gety ()); int y = (int) event.gety (); if (y


public class Testpopupwindow extends Activity {private Selectpopupwindow popupwindow;private ImageView image;private     Button btn, btn1;private Uri Fileuri; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main1); init ();} private void init () {image = (ImageView) Findviewbyid (r.id.image); btn = (Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (onclicklistener); btn1 = (Button) Findviewbyid (R.ID.BTN1); Btn1.setonclicklistener ( Onclicklistener);} View.onclicklistener Onclicklistener = new View.onclicklistener () {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.btn:test (); Break;case r.id.btn1:startactivity (New Intent (Testpopupwindow.this, Testgsonactivity.class)); break;case r.id.takephoto:intent Intent = new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Fileuri); Startactivityforresult (intent, +); break;case R.id.selectfromalbUm:intent intent1 = new Intent (Intent.action_pick); Intent1.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT _uri, "image/*"); Startactivityforresult (Intent1, n); break;}}; public void Test () {Popupwindow = new Selectpopupwindow (this, onclicklistener);p opupwindow.showatlocation ( Findviewbyid (R.id.main), gravity.bottom| Gravity.center_horizontal, 0, 0);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Custom Popup effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.