views:

253

answers:

2

I have an android question: I've successfully created a countdown kitchen timer activity, however my goal is to have an activity that has 3 timers on it that all work independently. I created a separate layout just for the timer itself and moved the timer code into a class and I've used layoutinflater to create the views and then added them into the linear layout for the activity. I get the layouts fine, however there's no functionality. There doesn't seem to be anything that ties the class code to the activity.

How should I approach this? Can anyone point me to some working example code?

thanks in advance!

+1  A: 

Assuming you have inflated a view from an XML layout you could call findViewById(Int) on the inflated view to get a hold of any view in that layout. Then you could do the wireing manually, for instance adding a onClick listener to some button:

inflatedView.findViewById(ID_OF_SOME_BUTTON_IN_THE_INFLATED_VIEW).setOnClickListener(
   new View.OnClickListener() {
       public void onClick(View v) {
           // DO SOMETHING WHEN BUTTON IS CLICKED
       }
   });
Emil H
I've done this but I get an exception every time I try and click on a button. Basically all I did was move my timer code from the main activity into a separate class. All the wiring is done in that class' constructor. The activity simply does a new TimerBox(LinearLayout). The class I've created doesn't extend LinearLayout but I don't see why it should need to.
gdolph
Hmm, this is just a guess, but have you tried doing the wiring after the constructor call in an init method? Not sure if it would help, but it would be worth a try. I.e. ClassThatWillInflateTheLayout toInitalize = new ClassThatWillInflateTheLayout(); toInitialize.init()
Emil H
A: 

This was simply a case of not knowing enough. What I did in the end was to extend relativelayout in my class and fix my constructors to inflate the view in the class with the context of the activity. Then the view was added into the correct layout in the activity.

Thanks for all the good suggestions

gdolph