android - showAsDropDown but upwards like spinner -
i have used popup listview work spinner .i want pop upwards spinner when @ bottom of screen . have tried :
popupwindowdogs.showasdropdown(buttonshowdropdown,5,0);
also,
popupwindowdogs.showasdropdown(buttonshowdropdown, (int)(math.round(buttonshowdropdown.getx())),-(totallinear_layouts+buttonshowdropdown.getheight()));
totallinearlayouts
= sum of heights of linear layouts till button.
this works on few devices not spinner. how can make work spinner? mean inflate size of device , list height. appreciate help. in advance.
reference:https://www.codeofaninja.com/2013/04/show-listview-as-drop-down-android.html
mainactivity.java
public class mainactivity extends activity { string tag = "mainactivity.java"; string popupcontents[]; popupwindow popupwindowdogs; button buttonshowdropdown; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // initialize pop window items list // add items on array dynamically // format dogname::dogid list<string> dogslist = new arraylist<string>(); dogslist.add("akita inu::1"); dogslist.add("alaskan klee kai::2"); dogslist.add("papillon::3"); dogslist.add("tibetan spaniel::4"); // convert simple array popupcontents = new string[dogslist.size()]; dogslist.toarray(popupcontents); // initialize pop window popupwindowdogs = popupwindowdogs(); // button on click listener view.onclicklistener handler = new view.onclicklistener() { public void onclick(view v) { switch (v.getid()) { case r.id.buttonshowdropdown: // show list view dropdown popupwindowdogs.showasdropdown(v, -5, 0); break; } } }; // our button buttonshowdropdown = (button) findviewbyid(r.id.buttonshowdropdown); buttonshowdropdown.setonclicklistener(handler); } public popupwindow popupwindowdogs() { // initialize pop window type popupwindow popupwindow = new popupwindow(this); // drop down list list view listview listviewdogs = new listview(this); // set our adapter , pass our pop window contents listviewdogs.setadapter(dogsadapter(popupcontents)); // set item click listener listviewdogs.setonitemclicklistener(new dogsdropdownonitemclicklistener()); // other visual settings popupwindow.setfocusable(true); popupwindow.setwidth(250); popupwindow.setheight(windowmanager.layoutparams.wrap_content); // set list view pop window content popupwindow.setcontentview(listviewdogs); return popupwindow; } /* * adapter list values set */ private arrayadapter<string> dogsadapter(string dogsarray[]) { arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, dogsarray) { @override public view getview(int position, view convertview, viewgroup parent) { // setting id , text every items in list string item = getitem(position); string[] itemarr = item.split("::"); string text = itemarr[0]; string id = itemarr[1]; // visual settings list item textview listitem = new textview(mainactivity.this); listitem.settext(text); listitem.settag(id); listitem.settextsize(22); listitem.setpadding(10, 10, 10, 10); listitem.settextcolor(color.white); return listitem; } }; return adapter; } }
dogsdropdownonitemclicklistener.java
public class dogsdropdownonitemclicklistener implements onitemclicklistener { string tag = "dogsdropdownonitemclicklistener.java"; @override public void onitemclick(adapterview<?> arg0, view v, int arg2, long arg3) { // context , main activity access variables context mcontext = v.getcontext(); mainactivity mainactivity = ((mainactivity) mcontext); // add animation when list item clicked animation fadeinanimation = animationutils.loadanimation(v.getcontext(), android.r.anim.fade_in); fadeinanimation.setduration(10); v.startanimation(fadeinanimation); // dismiss pop mainactivity.popupwindowdogs.dismiss(); // text , set button text string selecteditemtext = ((textview) v).gettext().tostring(); mainactivity.buttonshowdropdown.settext(selecteditemtext); // id string selecteditemtag = ((textview) v).gettag().tostring(); toast.maketext(mcontext, "dog id is: " + selecteditemtag, toast.length_short).show(); } }
Comments
Post a Comment