I am using eclipse and are programming my first app, but I have a beginner question, i want to use some of the methods from my button in the Main class, but i want to use them in my Start class, how do i set this up?
Is it by using:
Button1 = (Button) find View By Id(R.id.button1)
In every class i want to use it? Or is there a much simpler way?
I am not sure that this will give me the correct result, i am currently getting an error, and i don't know if this is the fault.
Thanks for your help.
If you want to use button1 in your Start Class, then you also have to declare it in the Layout file of your Start Class.
You are getting an error most likely because that line of code will make the application search for button1 in your Start Class Layout file (which most likely isn't there).
One option you have is to create a
BaseActivity
and add theButton
there along with its functionality. Then you canextends BaseActivity
in theActivities
which you want thisButton
to be used.Doing this, you will need to use the
<include>
tag in the xml of yourActivities
that you want thisButton
. Now you can use thisButton
in otherActivities
and you canoverride
the method that theButton
uses if you need different functionality in certainActivities
.If doing it that way seems too complicated then you will need to put the
Button
in each xml and the associated functionality in the class that uses that xml. But you might give this a try...it can really make things easier and cut down on duplicate code.include