android - findViewById usage in View -
i'm struggling problem few days , couldn't find solution problem far. have 2 classes: - startactivity extends activity - timegraphview extends surfaceview
what want achieve add dynamically buttons within timegraphview view (linearlayout). wanted linearlayout inside timegraphview findviewbyid() returns null, , should because call in timegraphview not in root element used setcontentview();
so question how can add button dynamically custom view level view.
and code:
public class startactivity extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.time_graph); linearlayout layout = (linearlayout) this.findviewbyid(r.id.timegraphlayout); //here can add button it's not want } }
and ...
public class timegraphview extends surfaceview implements surfaceholder.callback, runnable { public timegraphview(context context) { super(context); } public timegraphview(context context, attributeset set) { super(context, set); } public timegraphview(context context, attributeset set, int arg) { super(context, set, arg); } public void run() { while (run) { if (something) { linearlayout layout = (linearlayout) findviewbyid(r.id.timegraphlayout); if (layout != null) { button button = new button(context); button.settext(text); layout.addview(button); } else { log.e("timegraphview", "timegraphlayout null"); //and "layout" null , that's problem ;( } } } } }
... , xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/timegraphrootlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <horizontalscrollviewa android:id="@+id/timegraphpanel" android:layout_width="match_parent" android:layout_height="wrap_content" > <linearlayout android:id="@+id/timegraphlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> </horizontalscrollview> <my.package.timegraphview android:id="@+id/timegraphchart" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout>
you cannot use in way. if add root element linear layout , reference in addition might work. if want timegraphlayout class though:
timegraphview layout = (timegraphview) findviewbyid(r.id.timegraphlayout); setcontentview(layout);
the original way did not work because timegraphview not linearlayout
Comments
Post a Comment