Noobs DeSroobs
Noobs DeSroobs

Reputation: 283

My android java button shows up and animates but for some reason my code is never executed

I have a button in my android application.

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start scanning"
    android:id="@+id/bStartScan"
    android:enabled="true"
    android:clickable="true"
    android:layout_below="@+id/third_person_button"
    android:layout_toEndOf="@+id/StitchPCButton"
    android:longClickable="false" />

The button does animate and it does show up in my application, but none of teh code below is executed.

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bStartScan:
            findViewById(R.id.bStartScan).setEnabled(false);
            ...
            ...
            break;
        default:
            Log.w(TAG, "Unrecognized button click.");
    }
}

My other buttons works just fine and I do not understand why this button refuses to fire.

Any ideas? Any questions?

Thank you so much in advance.

Upvotes: 0

Views: 53

Answers (3)

Shaon Hasan
Shaon Hasan

Reputation: 740

add this line in ur xml button tag:

android:onClick="myMethod"

and in the activity class add ur method like below:

public void myMethod(View v) 
{
  // write your code here
}

Upvotes: 0

cham3333
cham3333

Reputation: 103

Add this line to your xml button code -> android:onClick="onClick"

Upvotes: 0

Nilay Dani
Nilay Dani

Reputation: 896

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start scanning"
android:id="@+id/bStartScan"
android:layout_below="@+id/third_person_button"
android:layout_toEndOf="@+id/StitchPCButton"
/>

Use this and in your activity/Fragment onCreate() in

      Button startBtn = (Button) findViewById(R.id.bStartScan);
startBtn.setonClicklistner(this);

Remove findViewById(R.id.bStartScan) from switch() statement...

Upvotes: 1

Related Questions