GeekedOut
GeekedOut

Reputation: 17185

Android dev - how to create buttons on the home screen that lead to actions

I just compiled and ran the hello world app and it worked. And I read through a bunch of stuff on the android documentation about the diff components of android and how it all works together. Now I want to make a few buttons which link to various actions.

For example, I want to make a button that goes to a new screen. Is there a tutorial for this sort of a thing? Or maybe someone could explain to me how to do that?

Thanks!

Upvotes: 1

Views: 282

Answers (2)

Dongminator
Dongminator

Reputation: 835

The Form stuff tutorial is really useful. I believe that's the next thing you want to learn after HelloWorld.

To start a new screen call startActivity() or startActivityForResult(), depending if you intend to get data back from the new activity. You also want to learn something called Intent, which you can add information to it and pass between screens(or activity). Using intents are most often used for stating a new activity (Android dev Guide for Acticities).

Hope that's helpful.

Upvotes: 2

Daniel Lockard
Daniel Lockard

Reputation: 615

This is really simple. To make a button that goes to a new screen you should put a new button on your XML layout, and assign it an id. After you've done this, in your code, you need to do something like the following:

Button mButton = (Button) findViewById(R.id.mybutton);
mButton.setOnClickListener(new Button.OnClickListener() {  
    public void onClick(View v) {
      Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
      CurrentActivity.this.startActivity(myIntent);
    }
});

Hope this helps you.

Also, be sure that the activity that you're trying to start A) Exists, and B) is in your manifest.

Upvotes: 2

Related Questions