Kami
Kami

Reputation: 49

Change class view using buttons

I'm trying to have a program go from one class to another by the press of a button. I know this needs to be done with intent, but I'm not sure exactly what action to use for intent. Also, i would rather not try to use OnClickListener since it seems to me to be a bit excessive for what I need to do. Though I could be wrong and I'm just over thinking it. Here's what I have for the code:

 public void loginScreen(View view) {
   // Should take user to the next view when button pressed
      Intent intent = new Intent(this, LoginActivity.class);
      Button login = (Button) findViewById(R.id.button1);
      intent.getData(login);
      startActivity(intent);
    }

I want it to go to the next screen when only button1 is pressed so i don't know if just lines 3 and 6 are the only ones needed, but it won't work if it's just those two. I believe the problem is getData, i don't know what to put there, but i know getData's wrong. I'm probably also missing something else, but I don't know what.

Also, forgive me if this isn't easy to follow, first time trying to ask a question here.

Upvotes: 0

Views: 76

Answers (2)

Blundell
Blundell

Reputation: 76458

In your XML file you can declare your on click:

<Button
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:onClick="loginScreen" />

Then in your Activity:

public void loginScreen(View button) {
  Intent intent = new Intent(this, LoginActivity.class);
  startActivity(intent);
}

Here is the onClick View API

Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).

Note that if you add an onClick to a Fragment layout it will still need to be caught in the Activity.

Upvotes: 1

gsgx
gsgx

Reputation: 12239

Without explicitly setting an onClickListener:

<button
    ...
    android:onClick="loginClicked" />

public void loginClicked(View v) {
    Intent intent = new Intent(this, LoginActivity.class);
    startActivity(intent);
}

Also setting an onClickListener explicitly is not overkill.

Your question isn't entirely clear, so let me know if you need more and I'll edit my answer.

Upvotes: 1

Related Questions