user3052839
user3052839

Reputation: 13

Android button not responding to click event/

As far as I can tell, everything needed for a button to respond is in place. Im baffled so would appreciate a pair of extra eyes on this.

Simple homepage activity, login or create a profile buttons displayed. Click the login button and the next activity should display. Application runs on device but buttons do not respond.

Code

public class HomeActivity extends Activity implements OnClickListener {

    Button login;
    Button createProfile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        login           = (Button) findViewById(R.id.loginButton);
        login.setOnClickListener(this);

        createProfile   = (Button) findViewById(R.id.createProfileButton);
        createProfile.setOnClickListener(this);         
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public void onClick(View view) {

        Intent intent;
        switch(view.getId()){

          case R.id.loginButton: /** Start a new Activity LoginActivity.java */
              intent = new Intent(this, LoginActivity.class);
              this.startActivity(intent);
              break;

          case R.id.createProfileButton: /** Start a new Activity About.java */
              //intent = new Intent(this, AboutActivity.class);
              //this.startActivity(intent);
              break;
          } 
    }
}

HomeScreen layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="160dp"
    android:layout_marginRight="51dp"
    android:text="Login" />

<Button
    android:id="@+id/createProfileButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/loginButton"
    android:layout_alignBottom="@+id/loginButton"
    android:layout_toLeftOf="@+id/loginButton"
    android:text="createProfile" />

Login Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is the login page!" />

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nutrtionintuition"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.nutrtionintuition.HomeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name" >
    </activity>
</application>

LoginACtivity

public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }
}

Upvotes: 1

Views: 3102

Answers (2)

Michael Yaworski
Michael Yaworski

Reputation: 13483

In both classes (HomeActivity and LoginActivity), you set the content view like this:

setContentView(R.layout.activity_home);

Your code is working, but the new activity is loading the same layout.


Change
setContentView(R.layout.activity_home);
to
setContentView(R.layout.activity_login);

(or whatever your login layout is named) in your LoginActivity class.

Upvotes: 2

Didac Perez Parera
Didac Perez Parera

Reputation: 3834

Your code is OK and it must work. It seems that your activites are not included in your AndroidManifest.xml.

If so, maybe you are actually launching your another activity, but you may have missed to put the right layout id in the setContentView function.

Upvotes: 0

Related Questions