-3

Possible Duplicate:
how to start a new activity when click on button

I want to make my app go to a new screen when i click a button? Do i need a new activity for this or is there another way? If it is an activity can someone tell me how to make a button linked to a activity? thanks!

p.s. Please tell me which files the code u say go in, thansk!

EDIT: I appologize for my lack of awareness of the sites policys on searching for things, I am new to this and apologize. Thanks for the help though for linking me to the right spot for this. Once again sorry guys.

Community
  • 1
  • 1
William Reed
  • 1,717
  • 1
  • 17
  • 30
  • 4
    It sounds like you really need to start at the beginning. I recommend the series of tutorials by Vogella, http://www.vogella.com/articles/Android/article.html. stackoverflow tends to appreciate questions better when the poster shows that they've already made an effort to get started. – Turnsole Aug 17 '12 at 14:10
  • http://developer.android.com/training/basics/firstapp/starting-activity.html – Benito Bertoli Aug 17 '12 at 14:11

4 Answers4

2

In the xml-button definition

<Button
...
android:onClick="buttonClicked"
/>

in the activity

public void buttonClicked( View v ){
Intent i = new Intent( this, NewActivity.class );
startActivity( i );
}

Here you go

fklappan
  • 3,259
  • 2
  • 17
  • 18
1

I guess you already have a button defined. All you need to do is:

//Here you define a lisener that will be fired every time you press on the button
Button btnOpenActivity = (Button) findViewById(R.id.YOUR_BUTTON_ID);
btnOpenActivity.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) {
        //Here you start the activity, by creating and passing an Intent
        Intent activityIntent = new Intent (YOUR CONTEXT, SecondActivity.class);
        startActivity (activityIntent);
    }
});

That's it.

Because it looks like you are a beginner in Android programming, I would recommend you to go to TheNewBoston channel in youtube. This is where I learned android programming.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
RE6
  • 2,684
  • 4
  • 31
  • 57
1

Yes, you can create a new Activity. Or you could use Fragments. Activity is probably the easiest method, and yes, it can be done with a button click:

    Button yourButton = (Button) findViewById(R.id.yourButtonId);

yourButton.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) {

        Intent newIntent = new Intent (this, ActivityYouWantToStart.class);
        startActivity (newIntent);
    }
});
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
1

Yes, to display a new screen - you'll need to create a new activity and its corresponding layout xml file.

Suppose you have two activities: FirstActivity and SecondActivity and you want to open the later from the former. In that case you can set the event listener for the button on FirstActivity as:

btnSecondScreen.setOnClickListener(
    new View.OnClickListener(){
        public void onClick(View v){
            startActivity(new Intent(FirstActivity.this, SecondActivity.class));
        }
    }
);
Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114