1

I'm struggling with a problem for a few days already and couldn't find solution to my problem so far. I have two classes: - StartActivity extends Activity - TimeGraphView extends SurfaceView

What I want to achieve is to add dynamically buttons from within TimeGraphView to another view (LinearLayout). To do so wanted to get that LinearLayout inside TimeGraphView with findViewById() but it returns null, and it should because I call it in TimeGraphView not in root element where I used setContentView();

So my question is how can I add button dynamically from custom view level to another view.

And my 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 I can add button but it's not what I 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 is null");
                    //and "layout" is always null and that's the problem ;(
                }
            }
        }
    }
}

... and my 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>
ahodder
  • 11,353
  • 14
  • 71
  • 114
Wolv47
  • 11
  • 5
  • Check out [this question](http://stackoverflow.com/questions/4486034/android-how-to-get-root-view-from-current-activity) to see if you can get the "root view"... assuming the layout you're trying to find is in the same activity. – Rob I Jun 05 '12 at 19:21
  • tried getRootView() and it yelled at me with: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. – Wolv47 Jun 05 '12 at 19:48
  • Have you tried requesting focus to the new button? That might bring it to front... (I have no clue why it isn't working, sorry) – bhekman Jun 05 '12 at 20:11
  • I can't request focus to new button because sadly I can not create it which is the problem. – Wolv47 Jun 05 '12 at 20:27
  • Sounds like you need to use a [Handler](http://developer.android.com/reference/android/os/Handler.html) to get that `getRootView()` called from the same thread. – Rob I Jun 05 '12 at 20:37
  • Ye I think I'm gonna create and add TimeGraphView in StartActivity's onCreate() method and pass it in constructor; – Wolv47 Jun 05 '12 at 21:09

1 Answers1

1

You cannot use it in that way. If you add a root element Linearlayout and reference that in addition it might work. If you want to get the TimeGraphLayout class though:

 TimeGraphView layout = (TimeGraphView) findViewById(R.id.TimeGraphLayout);
 setContentView(layout);

The original way you did it will not work because TimeGraphView is not a LinearLayout

Zoe
  • 27,060
  • 21
  • 118
  • 148