I am new to coding and am trying to work on a project when my main Activity page has a series of buttons. From these buttons I would like each to open a different activity or command. I been searching and found what I thought SHOULD work, however, it does not. I get a crash when it comes loading the app and then clicking on the button. Below is the code. Any pointers to show my mistake somewhere would be kindly appreciated.
package com.example.finalproject2;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize Buttons
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button02);
Button b3 = (Button) findViewById(R.id.button03);
Button b4 = (Button) findViewById(R.id.button01);
Button b5 = (Button) findViewById(R.id.button04);
//Set OnCLickListeners
b1.setOnClickListener(chicagoListener);
b2.setOnClickListener(sanJoseListener);
b3.setOnClickListener(baltimoreListener);
b4.setOnClickListener(westPalmBeachListener);
b5.setOnClickListener(websiteListener); }
private OnClickListener chicagoListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Chicago.class));
}
};
private OnClickListener sanJoseListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, SanJose.class));
}
};
private OnClickListener baltimoreListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Baltimore.class));
}
};
private OnClickListener westPalmBeachListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
}
};
private OnClickListener websiteListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
}
};
{
}
}
Mention your all activity in your app manifesto file (like this):
<activity
android:name="com.example.finalproject2.Chicago"
android:label="#string/app_name" >
</activity>
And try to do somthing like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
startActivity(new Intent(MainActivity.this, Chicago.class));
break;
case R.id.button02:
startActivity(new Intent(MainActivity.this, SanJose.class));
break;
case R.id.button03:
startActivity(new Intent(MainActivity.this, Baltimore.class));
break;
case R.id.button01:
startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
break;
case R.id.button04:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
break;
}
}
Button - Android developer
sample from developer site
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
another good example of how to use onclick
Related
So I've got multiple buttons in one activity, and I can only use them one after another. I've read I need to do something with the (OnClickListener) part but I'm unsure what or how to do it. Any help is greatly appreciated. Code is below:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
EDIT: thanks guys for all the help- out at the moment so when I get home I’ll test these. They look very similar to what I’ve read so they should work.. thanks!
You are setting your click listeners sequentially. If you want all the buttons to be clickable at any time, move the listeners into your onCreate():
protected void onCreate() {
B3.setOnClickListener(){};
B4.setOnClickListner(){};
// etc.
}
A click listener is just that – it "listens" for clicks. Your B3, for example, is the only one listening when the Activity gets created, so all the other buttons will ignore your clicks on them. When B3 is clicked, moveToDanceScheduleMenu() is invoked and B4 starts listening.
I hope that clears things up a bit.
Complete code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}
You can put all your code in OnCreate() method like this
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
You are making Buttons depended of other Buttons in a Sequential order.
Try Binding Button and their OnclickListners in oncreate.Like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
I think this code is work
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
private a=false,b=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
a=true ;
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(a==true ){
moveToWhatsOn();
}
}
});
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
b=true ;
B5 = findViewById(R.id.button11);
}
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(b==true){
moveToMainResultsMenu();
}
}
});
}
If you want to use a button only after a previous button was pressed, you should disable all next buttons in the begining in the onCreate() method and enable them later when the previous button was pressed
B3.setEnabled(true);
B4.setEnabled(false);
B5.setEnabled(false);
And then enable the next button after onClick
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
B4.setEnabled(true);
}
});
And as far for your code
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu() {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn() {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}
There are several ways to interact with the buttons:
First method: Class implementation
public class Main2Activity extends AppCompatActivity implements OnClickListener {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(this);
B4.setOnClickListener(this);
B5.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==B3.getId()){
moveToDanceScheduleMenu();
}else if(v.getId()==B4.getId()){
moveToWhatsOn();
}else if(v.getId()==B5.getId()){
moveToMainResultsMenu();
}
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Second Method: Private variable (large blocks)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.button3).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button10).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button11).setOnClickListener(mGlobal_OnClickListener);
}
//Global On click listener for all views
final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
public void onClick(final View v) {
switch(v.getId()) {
case R.id.button3:
moveToDanceScheduleMenu();
break;
case R.id.button10:
moveToWhatsOn();
break;
case R.id.button11:
moveToDanceScheduleMenu();
break;
}
}
};
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Third Method: Online (small blocks)
public class Main2Activity extends AppCompatActivity{
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
I created a simple counter and want to add a button that proceeds to another activity, here's what I got:
//counter starts
#Override
public void onClick(View v) {
if (v == btn1){
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.CYAN);
}
if (v == btn2){
counter--;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
}
if (v == btn3){
counter = 0;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
}
//There the counter ends
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondAcitivity.this, Aktivity.class);
startActivity(intent);
}});
}}
After the //There the counter ends tag I set an OnClickListener to a button "NEXT" that proceeds to another activity and created an Intent that brings to the activity called "Aktivity".
When I press the button, however, nothing happnendz. halp
Cut and Paste this code in your onCreate() Method
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondAcitivity.this, Aktivity.class);
startActivity(intent);
}});
You can't set click listener to an object inside onClick() Method
Change to
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btn1: // btn1 clicked
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.CYAN);
break;
case R.id.btn2: // btn2 clicked
counter--;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
break;
case R.id.btn1: // btn3 clicked
counter=0;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
break;
case R.id.next: // assuming button next id is next. btn next clicked
Intent intent = new Intent(SecondAcitivity.this, Aktivity.class);
startActivity(intent);
break;
}
}
Then in onCreate
next.setOnClickListener(this); // similarly fro other buttons
Ans make sure your Activity implements OnClickListener
Instead of switch case you can use if else.
Code:
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
b1 = (Button) findViewById(R.id.f);
b2 = (Button) findViewById(R.id.s);
b1.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
b1.setVisibility(View.INVISIBLE);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setVisibility(View.INVISIBLE);
}
});
}
#Brontok I tested this code and it works fine. So, onClick on an object can be implemented inside other onClick.
#MiroslavVitula I tested your scenario too. I was able to open the same activity by same method you used. Maybe some other problem.
I'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}
I have a class called Menu and inside that class I want to place a menu of buttons such as buttonA, buttonB, buttonC, and so on. However when I run the app on my phone I cant tap buttonB before I tap buttonA. If I tap buttonA first, I can choose buttonA or buttonB all I want. The question is how do you separate the buttons in the Menu class to be able to tap any button at any time?
package com.emods.app1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
});
}
}
You need to take your btnB and place it outside your onClick event for btnA. Currently you have your declaration for btnB inside your onClick event for btnA.
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
You defined the listener for the second button inside the listener for the first one.
Nesting issue.
Change:
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
});
to:
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
That should fix it.
Only the brackets are wrong:
package com.emods.app1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
}
You should activate the automatic formatting in your IDE.
I'm having trouble switching views with button presses in my Android app. The code shows no errors in Eclipse, but the app quits unexpectedly in the emulator when the button is clicked. My code is below. Thanks
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button go = (Button)findViewById(R.id.goButton);
go.setOnClickListener(mGoListener);
}
private OnClickListener mGoListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("android.taboo.Activities", "android.taboo.Activities.MainMenu");
startActivity(intent);
}
};
}
public class MainMenu extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
TextView quickStart = (TextView)findViewById(R.id.quickStart);
quickStart.setOnClickListener(mQuickStartListener);
TextView gameSetup = (TextView)findViewById(R.id.gameSetup);
gameSetup.setOnClickListener(mGameSetupListener);
TextView settings = (TextView)findViewById(R.id.settings);
settings.setOnClickListener(mSettingsListener);
TextView wordEntry = (TextView)findViewById(R.id.wordEntry);
wordEntry.setOnClickListener(mWordEntryListener);
}
//Listeners for MainMenu navigation buttons
private OnClickListener mQuickStartListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.quickstart);
}
};
private OnClickListener mGameSetupListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.gamesetup);
}
};
private OnClickListener mSettingsListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.settings);
}
};
private OnClickListener mWordEntryListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.word);
}
};
}
Take a look at this code that i have here, this should help you out some.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class SmartApp extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
final Button firstTimeButton = (Button) findViewById(R.id.firstTimeButton);
firstTimeButton.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent userCreationIntent = new Intent(v.getContext(), UserCreation.class);
startActivityForResult(userCreationIntent, 0);
}
});
}
}
When the user clicks the "first time button" the user will be taken to the "user creation page". I believe in your code you have a few things wrong. Compare yours to what i provided and you should be able to see the differences and make the appropriate modifications. Let me know if this helps!