Jack
Jack

Reputation: 2053

Button not responding to OnClick

I've set up a button in my XML layout, then set it up with an onClickListener in my Activity. Once I click the button, nothing happens. I have set up a TAG, so once the button has been pressed, it should show a message in the LOG.

Any help would be appreciated!

EDIT: I have found out what the problem is, whenever I remove the code

    if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
    final FragmentTransaction ft = getSupportFragmentManager()
            .beginTransaction();
    ft.add(android.R.id.content, new ImageGridFragment(), TAG);
    ft.commit();

 }

everthing works perfectly. I'm not sure how I'm going to fix this, any help would be appreciated...

Activity

public class ImageGridActivity extends FragmentActivity {
private static final String TAG = "TEST";

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.image_grid_fragment);
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);

    if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
        final FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        ft.add(android.R.id.content, new ImageGridFragment(), TAG);
        ft.commit();

    }

    Button B1 = (Button) findViewById(R.id.button1);
    B1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Log.d(TAG, "clicked");

        }
    });

  } 
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<GridView
    android:id="@+id/gridView"
    style="@style/PhotoGridLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="@dimen/image_thumbnail_size"
    android:horizontalSpacing="@dimen/image_thumbnail_spacing"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="@dimen/image_thumbnail_spacing" >
</GridView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</RelativeLayout>

Upvotes: 1

Views: 3135

Answers (3)

ClaireG
ClaireG

Reputation: 1244

The following should work:

Button B1 = (Button) findViewById(R.id.button1);
B1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Log.d(TAG, "clicked");

    }
}),

EDIT:

The reason why it is still not working is because you have to import View. If you are using Eclipse press Ctrl+Shift+O to fix all imports.

Otherwise add

 import android.view.View.OnClickListener;

Upvotes: 1

Vikalp Patel
Vikalp Patel

Reputation: 10877

As you ain't catch the clickEvent on a View. Your onClickListener is not being called. Try to add onClickListener on your View. Just as the snippet shown below:-

Button B1 = (Button) findViewById(R.id.button1);
B1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    Log.d(TAG, "clicked");

   }
 });

Upvotes: 0

invisbo
invisbo

Reputation: 3438

Make sure you are importing import android.view.View.OnClickListener;

or you can also try like this:

B1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Log.d(TAG, "clicked");

    }
});

Upvotes: 3

Related Questions