java - How to add a view (circular progress bar for example) relative to another view in a tablelayout -


hope can advise.

i have created android app in android studio dynamically , programmatically adds load of buttons tablelayout. number of buttons set user there 1 or there 100. using tablelayout added neatly 2 buttons each row , sized correctly. happy this.

i add view (in form of circular progress bar) in front of each button when button pressed circle rotates while whatever button has been setup actioned. want progress bar preferably sit in middle of button i'm not overly fussy.

can advice best way achieve this? need redo buttons in relative layout? there way of adding views on top of other views in table layout? advice or suggestions welcome.

thank

my current button code...

(it assumes there 2 buttons on each row , know number of rows , number of buttons).

   tablelayout.layoutparams tableparams = new tablelayout.layoutparams(tablelayout.layoutparams.wrap_content, tablelayout.layoutparams.wrap_content);    tablerow.layoutparams rowparams = new tablerow.layoutparams(0, android.widget.tablerow.layoutparams.match_parent, 3f);      tablelayout tablelayout = (tablelayout) findviewbyid(r.id.thetablelayout);    tablelayout.removeallviews();     (int = 0; < numberofrows; i++) {           tablerow tablerow = new tablerow(this);         tablerow.setlayoutparams(tableparams);          button btnone = new button(this);         btnone.setid(1001 + i);         button btntwo = new button(this);         btntwo.setid(2001 + i);           btnone.setlayoutparams(rowparams);         btntwo.setlayoutparams(rowparams);          tablerow.addview(btnone);          view adivider = new view(this);         adivider.setlayoutparams(new tablerow.layoutparams(20, tablerow.layoutparams.match_parent));         adivider.setbackgroundcolor(color.transparent);      // bit of code deals odd/even numbers of buttons.           if (((i + 1) * 2) < numberofbuttons + 1) {             tablerow.addview(adivider);             tablerow.addview(btntwo);             btntwo.setonclicklistener(mglobal_onclicklistener);             btntwo.setonlongclicklistener(mglobal_onlongclicklistener);             btntwo.settextsize(14);             btntwo.setsingleline(true);             btntwo.settextcolor(0xffffffff);             btntwo.settext(btnnamearray.get((i * 2) + 1));             btntwo.setbackgroundresource(r.drawable.buttonright);         } else {             tablerow.addview(adivider);             btntwo.setbackgroundresource(android.r.color.transparent);             tablerow.addview(btntwo);         }           view aline3 = new view(this);         aline3.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.match_parent, 20));         aline3.setbackgroundcolor(color.transparent);           tablelayout.addview(tablerow);          tablelayout.addview(aline3);    } 

as mentioned, i'd suggest redoing buttons relative layouts, allow nest progressbar(set visibility="gone") in each button-view. i'd happy further if post code.

edit: okay mocked you. take time read all, comments!

result: onclick shows progressbar, onlongclick hides

    private void setuptablelayout(int numberofrows, int numberofbuttons){         tablelayout.layoutparams tableparams = new tablelayout.layoutparams(tablelayout.layoutparams.wrap_content, tablelayout.layoutparams.wrap_content);         tablerow.layoutparams rowparams = new tablerow.layoutparams(0, android.widget.tablerow.layoutparams.match_parent, 3f);           tablelayout tablelayout = (tablelayout) findviewbyid(r.id.thetablelayout);         tablelayout.removeallviews();          (int = 0; < numberofrows; i++) {             tablerow tablerow = new tablerow(this);             tablerow.setlayoutparams(tableparams);              relativelayout btnonelayout = (relativelayout)getlayoutinflater().inflate(r.layout.custom_button, null);             relativelayout btntwolayout = (relativelayout)getlayoutinflater().inflate(r.layout.custom_button, null);              progressbar btnoneprogressbar = (progressbar)btnonelayout.findviewbyid(r.id.progressbar);             progressbar btntwoprogressbar = (progressbar)btntwolayout.findviewbyid(r.id.progressbar);              button btnone = (button)btnonelayout.findviewbyid(r.id.button);             btnone.settext("btn 1, row " + i);             btnone.setid(1001 + i);             button btntwo = (button)btntwolayout.findviewbyid(r.id.button);             btntwo.settext("btn 2, row " + i);             btntwo.setid(2001 + i);  /*            btnone.setlayoutparams(rowparams);   //weight width set in layout file on relativelayout             btntwo.setlayoutparams(rowparams);*/              setbuttonclicklistener(btnonelayout, btnoneprogressbar);             setbuttonlongclicklistener(btnonelayout, btnoneprogressbar);              tablerow.addview(btnonelayout); //add layout, instead of button              //is done invisible separator? can instead use margin on above/below views             view adivider = new view(this);             adivider.setlayoutparams(new tablerow.layoutparams(20, tablerow.layoutparams.match_parent));             adivider.setbackgroundcolor(color.transparent);              // bit of code deals odd/even numbers of buttons.             if (((i + 1) * 2) < numberofbuttons + 1) {                 tablerow.addview(adivider);                 tablerow.addview(btntwolayout);                  //click listeners                 setbuttonclicklistener(btntwolayout, btntwoprogressbar);                 setbuttonlongclicklistener(btntwolayout, btntwoprogressbar);  /*                btntwo.settextsize(14);              //if true buttons, can done in layout file                 btntwo.setsingleline(true);                 btntwo.settextcolor(0xffffffff);*/                  //btntwo.settext(getbuttonname((i * 2) + 1));                 //btntwo.setbackgroundresource(r.drawable.buttonright); //i didn't have drawable             } else {                 tablerow.addview(adivider);                  btntwolayout.setbackgroundresource(android.r.color.transparent); //i changed overall layout, not sure if want                 //btntwolayout.setvisibility(view.invisible); //or view.gone   //perhaps want instead                  tablerow.addview(btntwolayout);             }              //is done invisible separator? can instead use margin/padding on above/below views             view aline3 = new view(this);             aline3.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.match_parent, 20));             aline3.setbackgroundcolor(color.transparent);              tablelayout.addview(tablerow);             tablelayout.addview(aline3);         }      }      private string getbuttonname(int position){         string name = "";         switch (position){             case 0:                 name = "bob";                 break;             case 1:                 name = "jim";                 break;             default:                 name = "default";                 break;         }         return name;     }      private void setbuttonclicklistener(relativelayout layout, final progressbar progressbar){         layout.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 //do                 progressbar.setvisibility(view.visible);             }         });     }     private void setbuttonlongclicklistener(relativelayout layout, final progressbar progressbar){         layout.setonlongclicklistener(new view.onlongclicklistener() {             @override             public boolean onlongclick(view v) {                 //do                 progressbar.setvisibility(view.gone);                 return true;             }         });     } 

call function desire...

setuptablelayout(10, 2); 

and necessary layout files

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity">      <scrollview         android:layout_width="match_parent"         android:layout_height="match_parent">     <tablelayout         android:id="@+id/thetablelayout"         android:layout_width="match_parent"         android:layout_height="match_parent">         </tablelayout>     </scrollview> </relativelayout> 

custom_button.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:background="@android:color/holo_blue_bright"     android:padding="24dp">      <button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@android:color/holo_red_dark"         android:text="button"         android:gravity="center"         android:textcolor="@android:color/white"         android:singleline="true"         android:clickable="false">     </button>      <progressbar         android:id="@+id/progressbar"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@android:color/holo_green_dark"         android:layout_centerinparent="true"         android:visibility="gone"         /> </relativelayout> 

good luck, , don't afraid ask further questions. :-)


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -