Reputation: 181
I have a project in eclipse called "mijnenveger". Of course, the file Mijnenveger.java was automatically created. It has for example the onCreate method wihch places buttons on the screen.
Now I want to make a menu in front of it, with, for example, menu.xml. With the buttons options, help and play. And when I click play, the normal view (which is main.xml and includes Mijnenveger.java).
I know I can set setContentView to menu:
public class Mijnenveger extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //main would be replaced by menu
And make an onClick action for the play button to show main.xml:
setContentView(R.layout.main);
But there is one problem with that. When I start the app, it doesn't need to load all the buttons and stuff which are in the onCreate() of Mijnenveger.java. Only the menu in this case.
So is it possible to throw everything from Mijnenveger.java in a new class (class1.java for example) and make Mijnenveger.java only load the menu, and then when the play button is clicked, it loads the class class1.java which opens main.xml with all the buttons and stuff.
I hope you understand it, it was hard to explain in English :)
EDIT: Found a possible answer(http://stackoverflow.com/questions/2865238/how-do-i-call-a-java-file-on-click-in-another-java-class):
startActivity(new Intent(this, Game.class));
I've done what I said above, and called the file Game.java. But then it gives me this error: The constructor Intent(new View.OnClickListener(){}, Class) is undefined
EDIT2: I now have this:
final Button startgame = (Button) findViewById(R.id.startknop);
startgame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, Game.class);
startActivity(i);
setContentView(R.layout.main);
}
});
And it still gives this error: The constructor Intent(new View.OnClickListener(){}, Class) is undefined
There is somthing wrong about "this".
Upvotes: 0
Views: 5741
Reputation: 2622
create second acitvity class, for example GameActivity.java.
in method onClick for button play, start GameActivity class.
Intent i = new Intent(this, GameActivity.class);
startActivity(i);
Upvotes: 2
Reputation: 17813
put the menu screen in the activity which runs first and make your button start different activity for the relevant code.
learn more about activities and intents from the Android developer's documentation
Upvotes: 0