java - Android Custom Bar Graph Layouts Not Adding -


i'm trying create own custom bar graph in android having issues. graphing libraries i've seen don't offer flexibility , customization need. anyway, i'm in beginning phases , have basic layout placeholders of "bars" placed. xml below

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="250dp"     android:layout_gravity="center"     android:layout_margin="0dp"     android:duplicateparentstate="true"     android:orientation="horizontal">     <framelayout          android:layout_width="150dp"         android:layout_height="match_parent"         android:background="@drawable/sectioned_borders"         >          <framelayout             android:id="@+id/frame1"             android:layout_width="match_parent"             android:layout_height="238dp"             android:layout_gravity="bottom" >          </framelayout>     </framelayout>     <framelayout         android:layout_width="150dp"         android:layout_height="match_parent"         android:layout_marginleft="10dp"         android:background="@drawable/sectioned_borders"         >          <framelayout             android:id="@+id/frame2"             android:layout_width="match_parent"             android:layout_height="238dp"             android:layout_gravity="bottom" >          </framelayout>     </framelayout>       <framelayout          android:layout_width="150dp"         android:layout_height="match_parent"         android:layout_marginleft="10dp"         android:background="@drawable/sectioned_borders"         >          <framelayout             android:id="@+id/frame3"             android:layout_width="match_parent"             android:layout_height="238dp"             android:layout_gravity="bottom" >          </framelayout>     </framelayout>  </linearlayout> 

each framelayout section i'm going place bars in layout. now, want place bars dynamically since can vary how many bars present. put basic class , activity drawn in section. have following class contains "bar" information:

package com.example.graph.resources;  import android.content.context; import android.graphics.color; import android.view.view; import android.view.viewgroup; import android.view.viewgroup.layoutparams; import android.widget.framelayout; import android.widget.tablelayout;  public class bar {     private final static int max_percent = 100;    private final static int min_percent = 0;     private int percent = 0;    private string buttontext = "";    private string barid = "";    private drive bardrive;       public tablelayout barlayout;     public bar(context context, int percent, string buttontext, string barid, int width) {       this.percent = (percent >= 0 && percent <= 100) ? percent : 0;       this.buttontext = (buttontext != null) ? buttontext : "";       this.barid = (buttontext != null) ? buttontext : "";        bardrive = new drive();        // create bar view       tablelayout tablelayout = new tablelayout(context);       tablelayout.layoutparams tableparams = new tablelayout.layoutparams(46, tablelayout.layoutparams.match_parent);       tablelayout.setstretchallcolumns(true);       tablelayout.setlayoutparams(tableparams);        framelayout framelayout = new framelayout(tablelayout.getcontext());       framelayout.layoutparams frameparams = new framelayout.layoutparams(framelayout.layoutparams.match_parent, (int)(tablelayout.getheight() * 0.9));       frameparams.leftmargin = 3;       framelayout.setlayoutparams(frameparams);        // create views (bars)       view viewunderlay = new view(framelayout.getcontext());       layoutparams viewunderlayparams = new viewgroup.layoutparams(layoutparams.match_parent, layoutparams.match_parent);       viewunderlay.setlayoutparams(viewunderlayparams);        view viewoverlay = new view(framelayout.getcontext());       layoutparams viewoverlayparams = new viewgroup.layoutparams(layoutparams.match_parent, layoutparams.match_parent);             viewoverlay.setlayoutparams(viewoverlayparams);       viewoverlay.setbackgroundcolor(color.blue);         // combine views       framelayout.addview(viewunderlay);       framelayout.addview(viewoverlay);       tablelayout.addview(framelayout);       this.barlayout = tablelayout;          }     // maybe more advantageous make separate class...    private class drive {        private int drivenumber = 0;       private int value = 0;        public drive() {          // todo default drive info       }        private drive(int drivenumber, int value) {          // todo setup drive info       }      }  } 

as can see. "bar" class sets member data , have "bar" layout try add here. in main activities class try populate first frame. main class then:

package com.example.bargraphtest2;  import android.app.activity; import android.content.context; import android.graphics.color; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.viewtreeobserver; import android.view.viewtreeobserver.ongloballayoutlistener; import android.widget.framelayout; import android.widget.linearlayout;  import com.example.bargraphtest2.r; import com.example.graph.resources.bar;  public class mainactivity extends activity {     linearlayout la;    framelayout frame1;    context ctx;     @override    protected void oncreate(bundle savedinstancestate) {       super.oncreate(savedinstancestate);       setcontentview(r.layout.dynamic_graph_base_layout);         frame1 = (framelayout)this.findviewbyid(r.id.frame1);       ctx = this;       viewtreeobserver vto = frame1.getviewtreeobserver();        vto.addongloballayoutlistener(new ongloballayoutlistener() {            @override            public void ongloballayout() {               frame1.getviewtreeobserver().removeongloballayoutlistener(this);               //test frame 1              int testpadding = 3;              int barcount = 3;               int width = (frame1.getwidth() - ((barcount + 1) * testpadding)) / barcount;               for(int = 1; <= barcount; i++){                 bar bar = new bar(ctx,i * 10, "1", "bar" + i, width);                 frame1.addview(bar.barlayout);              }              frame1.refreshdrawablestate();           }        });     }     @override    public boolean oncreateoptionsmenu(menu menu) {       // inflate menu; adds items action bar if present.       getmenuinflater().inflate(r.menu.main, menu);       return true;    } } 

so, try running , generic layout (in xml). don't (3) bars. when debug code below , step through when bars getting created (the layouts), width , height in layoutparams not being set correctly. see negative values here, right match_parent not actual width. see legit value calculated width. so... doing wrong here? suggestions/help appreciated. thank much!

alright all, think figured out. so, there's 2 things consider here... first, when defining layoutparams, make sure creating parameters based off of parent container not container/layout creating. instance,in above example, when creating new tablelayout, define layoutparams framelayout.layoutparams. thing, think thee cause of of problems context... wasn't using same context throughout , messing layouts. use context generated activity/application. carrying on each child context , think messing things up. anyway, able toget blue bar on desired frame wanted now. hope helps someone.


Comments

Popular posts from this blog

javascript - Wordpress slider, not displayed 100% width -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -