Android Custom dialog box, progress bar, dropdown refresh, etc.

Source: Internet
Author: User

This demo is a collection of custom dialogs, progress bars, drop-down refreshes and popup popup boxes. After learning the online open source project, the collection of common dialog boxes, progress bar, drop-down refresh and popup popup box. Now the structure catalog is simple, easier to learn and expand to use. Comments unload the code. The following is a brief introduction and some code presentation.

This demo download: click

1, the overall realization of


2. Project Structure Chart


The above project structure is also at a glance, what is the function of the show. We also see, this above Class A bit more, if all posted out, unlikely, interested in downloading the source of this article.

3, look at the basic class baseactivity

I'll just post the base class, and the implementation class. Because these pieces of code are easy for other projects to use.

Package Com.org.base;import Java.util.arraylist;import Java.util.iterator;import java.util.list;import Com.example.activity.r;import Com.example.activity.r.layout;import Com.org.dialog.loadingdialog;import Com.org.view.handytextview;import Android.os.asynctask;import Android.os.bundle;import Android.util.displaymetrics;import Android.util.log;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.widget.toast;import android.app.Activity; Import Android.content.intent;public class Baseactivity extends Activity {protected Loadingdialog mloadingdialog;/** * Screen width, height, density */protected int mscreenwidth;protected int mscreenheight;protected float mdensity;//Asynchronous Load task listprotected List<asynctask<void, void, boolean>> masynctasks = new arraylist<asynctask<void, void, Boolean> > (); @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mloadingdialog =New Loadingdialog (This, "request in Submission");D isplaymetrics metric = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay () . Getmetrics (metric); mscreenwidth = Metric.widthpixels;mscreenheight = Metric.heightpixels;mdensity = metric.density;} protected void Putasynctask (asynctask<void, void, boolean> asynctask) {Masynctasks.add (Asynctask.execute ());} protected void Clearasynctask () {iterator<asynctask<void, void, boolean>> Iterator = Masynctasks.iterator ( ); while (Iterator.hasnext ()) {asynctask<void, Void, boolean> asynctask = Iterator.next (); if (asynctask! = null &amp ;&!asynctask.iscancelled ()) {Asynctask.cancel (true);}} Masynctasks.clear ();} protected void Showloadingdialog (String text) {if (text! = null) {mloadingdialog.settext (text);} Mloadingdialog.show ();} protected void Dismissloadingdialog () {if (mloadingdialog.isshowing ()) {Mloadingdialog.dismiss ();}} /** briefly displays Toast prompt (from res) **/protected void showshorttoast (int resId) {Toast.maketext (this, getString (resId), Toast.leNgth_short). Show ();} /** briefly displays Toast prompt (from string) **/protected void Showshorttoast (string text) {Toast.maketext (this, text, Toast.length_short ). Show ();} /** long time display Toast prompt (from res) **/protected void showlongtoast (int resId) {Toast.maketext (this, getString (resId), Toast.length_long). Show ();} /** long time display Toast prompt (from string) **/protected void Showlongtoast (string text) {Toast.maketext (this, text, Toast.length_long) . Show (); /** display a custom toast prompt (from res) **/protected void showcustomtoast (int resId) {View toastroot = Layoutinflater.from ( Baseactivity.this). Inflate (R.layout.common_toast, NULL);((Handytextview) Toastroot.findviewbyid (R.id.toast_text) ). SetText (GetString (resId)); Toast toast = new Toast (baseactivity.this); toast.setgravity (gravity.center, 0, 0); Toast.setduration (toast.length_ short); Toast.setview (toastroot); Toast.show ();} /** displays a custom toast prompt (from string) **/protected void Showcustomtoast (string text) {View toastroot = Layoutinflater.from ( Baseactivity.this). Inflate (R.layout.common_toast, NULL);((Handytextview)Toastroot.findviewbyid (R.id.toast_text)). SetText (text); Toast toast = new Toast (baseactivity.this); toast.setgravity (gravity.center, 0, 0); Toast.setduration (toast.length_ short); Toast.setview (toastroot); Toast.show ();} /** Debug output log log **/protected void Showlogdebug (string tag, string msg) {LOG.D (tag, msg);} /** Error Output log log **/protected void Showlogerror (string tag, string msg) {LOG.E (tag, msg);} /** through the Class jump interface **/protected void StartActivity (class<?> cls) {startactivity (CLS, null);} /** contains bundles through the Class jump interface **/protected void StartActivity (class<?> cls, bundle bundle) {Intent Intent = new Intent (); I Ntent.setclass (this, CLS); if (bundle! = null) {Intent.putextras (bundle);} StartActivity (intent);} /** default exit **/protected void Defaultfinish () {super.finish ();}}

4, Basepopupwindow

Package Com.org.base;import Android.annotation.targetapi;import Android.content.context;import Android.graphics.drawable.bitmapdrawable;import Android.os.build;import Android.util.attributeset;import Android.view.gravity;import android.view.view;import Android.widget.PopupWindow; @TargetApi (build.version_codes. Honeycomb) public abstract class Basepopupwindow extends Popupwindow {protected View mcontentview;protected Onsubmitclicklistener monsubmitclicklistener;public Basepopupwindow () {super ();} Public Basepopupwindow (context context, AttributeSet attrs,int defstyleattr, int defstyleres) {Super (context, attrs, Defstyleattr, defstyleres);} Public Basepopupwindow (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public Basepopupwindow (context context, AttributeSet Attrs) {Super (context, attrs);} Public Basepopupwindow (Context context) {super (context);} public Basepopupwindow (int width, int height) {Super (width, height);} Public Basepopupwindow (View Contentview, int width, int height,boolean focusable) {super (Contentview, width, height, focusable);} Public Basepopupwindow (View Contentview) {super (Contentview);} @SuppressWarnings ("deprecation") public Basepopupwindow (View contentview, int width, int height) {Super (Contentview, width, height, true); Mcontentview = contentview;setbackgrounddrawable (New bitmapdrawable ()); Initviews (); Initevents ( ); init ();} public abstract void Initviews ();p ublic abstract void initevents ();p ublic abstract void init ();p ublic View Findviewbyid (i NT ID) {return Mcontentview.findviewbyid (ID);} /** * displayed in the upper part of the parent and centered horizontally * * @param parent */public void Showviewtopcenter (View parent) {showatlocation (parent, gravity.to P | Gravity.center_horizontal, 0, 0);}  /** * displayed in the center of the parent * * @param parent */public void Showviewcenter (View parent) {showatlocation (parent, gravity.center, 0, 0);} /** * Add Confirmation Click Listen * * @param l */public void Setonsubmitclicklistener (Onsubmitclicklistener l) {monsubmitclicklistener = l;} public interface OnsubmitclicklIstener {void OnClick ();}} 

5, Basearraylistadapter
Package Com.org.base;import Java.util.arraylist;import Java.util.arrays;import java.util.list;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup Import android.widget.baseadapter;//Basic Adapter public class Basearraylistadapter extends Baseadapter {protected Context mcontext;protected layoutinflater minflater;protected list<string> mdatas = new ArrayList<String> ();// String array Add data source public basearraylistadapter (context context, String ... datas) {mcontext = Context;minflater = Layoutinflater.from (context); if (datas! = null && datas.length > 0) {mdatas = Arrays.aslist (datas);}} Add a data source using List Mode public basearraylistadapter (context context, list<string> datas) {mcontext = Context;minflater = Layoutinflater.from (context); if (datas! = null && datas.size () > 0) {mdatas = Datas;}} @Overridepublic int GetCount () {return mdatas.size ();} @Overridepublic Object getItem (int position) {return Mdatas.get (positION);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {return null;}}
6, implement the custom dialog, progress bar and Popup popup, main mainactivity

Package Com.example.activity;import Com.org.base.baseactivity;import Com.org.dialog.edittextdialog;import Com.org.dialog.simplelistdialog;import Com.org.dialog.simplelistdialog.onsimplelistitemclicklistener;import Com.org.popupwindow.chatpopupwindow;import Com.org.popupwindow.ChatPopupWindow.onChatPopupItemClickListener; Import Com.viw.adapt.checklistdialogadapter;import Android. R.layout;import Android.content.dialoginterface;import Android.os.bundle;import Android.os.Handler;import Android.util.typedvalue;import Android.view.gravity;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.widget.Button;import Android.widget.relativelayout;public class Mainactivity extends Baseactivity implements Onclicklistener, onchatpopupitemclicklistener{protected simplelistdialog mdialog;private int mcheckid = 0;private ChatPopupWindow mchatpopupwindow;private int mwidth;private int mheaderheight; @Overrideprotected void OnCreate (BundLe Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); FindViewById ( R.id.button1). Setonclicklistener (This), Findviewbyid (R.id.button2). Setonclicklistener (this); Findviewbyid ( R.id.button3). Setonclicklistener (This), Findviewbyid (R.id.button4). Setonclicklistener (this); Findviewbyid ( R.ID.BUTTON5). Setonclicklistener (this); Initpopupwindow ();} public void Onsimplelistitem () {string[] modes = new string[]{"Test One", "Test Two", "test three"};mdialog = new Simplelistdialog (this); Mdialog.settitle ("Radio Box"); mdialog.settitlelinevisibility (View.gone); Mdialog.setadapter (New Checklistdialogadapter (Mcheckid, this, modes)); /use anonymous inner class to implement Interface monitoring Mdialog.setonsimplelistitemclicklistener (new Dialogitemclicklistener ()); Mdialog.show ();} Protected class Dialogitemclicklistener Implementsonsimplelistitemclicklistener {@Overridepublic void Onitemclick ( int position) {mcheckid = position;}} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case R.id.button1:showloadingdialog ("Loading ... "); new Handler(). postdelayed (New Runnable () {@Overridepublic void run () {Dismissloadingdialog ();}}, 10000); Break;case R.id.button2 : Onsimplelistitem (); break;case r.id.button3:onedittextdialog (); break;case r.id.button4:relativelayout layoutShow = (relativelayout) Findviewbyid (r.id.layoutshow); Mchatpopupwindow.showatlocation (Layoutshow, Gravity.RIGHT| Gravity.top, -10, Mheaderheight +); break;case r.id.button5:startactivity (Pullrefreshlistviewactivity.class); Break;default:break;}} private void Onedittextdialog () {final Edittextdialog medittextdialog = new Edittextdialog (this); Medittextdialog.settitle ("Fill referrer"); Medittextdialog.setbutton ("Cancel", new Dialoginterface.onclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which) {medittextdialog.cancel ();}}, "Confirm", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {String text = Medittextdialog.gettext (); if (text = = null) {Medittextdialog.requestfocus ();} else {Medittextdialog.dismiss ();}}}); Medittextdialog.show ();} Initialize popupwindowprotected void Initpopupwindow () {mwidth = (int) typedvalue.applydimension (typedvalue.complex_unit_ dip,130, Getresources (). Getdisplaymetrics ()); mheaderheight = (int) typedvalue.applydimension (typedvalue.complex_ Unit_dip, Getresources () getdisplaymetrics ()), Mchatpopupwindow = new Chatpopupwindow (this, mwidth, layoutparams.wrap_content); Mchatpopupwindow.setonchatpopupitemclicklistener (this);} The following is the selection of the Popupwindow listener event @overridepublic void Onvoicemodeclick () {showcustomtoast ("Pop listener 1");} @Overridepublic void Oncreateclick () {showcustomtoast ("Pop Listener 2"); @Overridepublic void Onsynchronousclick () {showcustomtoast ("Pop listener 3");}}

7. Drop-Down Refresh pullrefreshlistviewactivity
Package Com.example.activity;import Android.os.asynctask;import Android.os.bundle;import android.view.View;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Com.org.base.baseactivity;import Com.org.view.pullrefreshlistview;import Com.org.view.pullrefreshlistview.oncancellistener;import Com.org.view.PullRefreshListView.OnRefreshListener; Import Com.viw.adapt.checklistdialogadapter;public class Pullrefreshlistviewactivity extends Baseactivityimplementsonrefreshlistener,oncancellistener,onitemclicklistener{private PullRefreshListView Mrefreshlistview; String[] modes = new string[]{"Test One", "Test Two", "Test Three"}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_refresh); initViews ();} protected void Initviews () {Mrefreshlistview = (Pullrefreshlistview) Findviewbyid (r.id.otherfeedlist_mmrlv_list);// Adapter I used a mrefreshlistview.setadapter (new CHECKLISTDIAlogadapter (1, this, modes)); Mrefreshlistview.setonitemclicklistener (this); Mrefreshlistview.setonrefreshlistener (this); Mrefreshlistview.setoncancellistener (this);} @Overridepublic void OnCancel () {//Cancel refresh Mrefreshlistview.onrefreshcomplete ();} @Overridepublic void Onrefresh () {//Async-Load Thread putasynctask (new asynctask<void, Void, boolean> () {@Overrideprotected Boolean doinbackground (Void ... params) {try {Thread.Sleep],} catch (Interruptedexception e) {}return null;} @Overrideprotected void OnPostExecute (Boolean result) {Super.onpostexecute (result);// Here write refresh adapt data on the line can be Mrefreshlistview.onrefreshcomplete ();});} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {//ListView's Click event is not written}}

This demo download: click





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

Android Custom dialog box, progress bar, dropdown refresh, etc.

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.