Daniel Lane
Daniel Lane

Reputation: 1

Android App Button 'onClick'

I am creating a charity app for Android. The app consists of 4 pages, each with a button which, when clicked, should navigate the user to the next page.

-Currently using Eclipse SDK-

The first (welcome) page button works and the code for this is:

public class CharityAppActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button main = (Button) findViewById(R.id.mybutton);
main.setOnClickListener (new OnClickListener(){

@Override
public void onClick(View v) {
setContentView(R.layout.donate);
// TODO Auto-generated method stub

}

});

}

I am wondering where I should put the code for the other buttons? (this java file is currently called CharityAppActivity.java)....

Any help would be gratefully received. I would be more than willing to offer you any more code if you need it to help me a little better

Ps. the pages are named main.xml, donate.xml, value.xml and thanks.xml

Upvotes: 0

Views: 2003

Answers (3)

ansorod
ansorod

Reputation: 367

You just need to create 4 Activities.

The OnClick method will call the next Activity using "startActivity"

@Override

public void onClick(View v) {

    Intent it = new Intent(NextClass.class);
    startActivity(it);

}

Upvotes: 0

ATom
ATom

Reputation: 16180

Activity is only one screen of application.

You should create more activities for every screen and do not try to only change content. It is not possible call setContentView() multiple times by default.

I suggest you try to more samples application from SDK directly, read some tutorials or book.

Upvotes: 1

Relsell
Relsell

Reputation: 771

Like you are finding Button main = (Button) findViewById(R.id.mybutton); find other buttons from your main activity and set their onClickHandler to invoke your different activities.

I am assuming all the four concerned buttons are in same layout.

Upvotes: 0

Related Questions