java

android

onclicklistener

I can't get my click listeners working. The ImageButton is retrieved correctly.

Listener (Extending my custom Activity which is setting the view):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("UserActivity", "Loading imagebutton...");
    ImageButton iButton = (ImageButton) findViewById(R.id.user_button_ratings);
    Log.d("UserActivity", "Button " + iButton);
    iButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Button", "Image was clicked");
        }
    });
}

BaseActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout,
                R.drawable.ic_drawer, 0, 0) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(R.string.menu);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        layers = getResources().getStringArray(R.array.layers_array);
        drawerList = (ListView) findViewById(R.id.left_drawer);

        drawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.adapter_navigation, R.id.navigation_text, layers));

        final BaseActivity activity = this;
        drawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {
                // Do something
            }
        });

        drawerList.setBackgroundColor(Color.LTGRAY);
    }

Layout:

<ImageButton
    android:id="@+id/user_button_ratings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@color/transparent"
    android:scaleType="fitCenter"
    android:src="@drawable/mmb_rating_big" />

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.panic.xenira.mmb"
    android:versionCode="2"
    android:versionName="0.1.1" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SearchActivity"
            android:label="@string/title_activity_search" >
        </activity>
        <activity
            android:name=".BaseActivity"
            android:label="@string/title_activity_base" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <activity
            android:name=".UserActivity"
            android:label="@string/title_activity_user" >
        </activity>
        <activity
            android:name=".UserViewerActivity"
            android:label="@string/title_activity_user_viewer" >
        </activity>
    </application>

</manifest>

There is no error in the LogCat and the onClick method is not caled. I've tryed different methods of using the listener but never got any response. I also tryed a code that worked for me befor but no response.

Would be great if you culd help me out ;)

Solution 1

When using a DrawerLayout, there should be only one main content View, with the drawer View - in this case, your ListView - listed after it. Using a DrawerLayout in any other way will result in incorrect, unpredictable behavior, often preventing normal interaction with other layout elements.

A tutorial with links to a sample and docs can be found on this developer page.

Solution 2

Here I am giving an example of Image button. If it is also giving error then clean projects of eclipse and try again.

<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=".MainActivity" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="35dp"
    android:layout_marginTop="32dp"
    android:contentDescription="image"
    android:src="@drawable/ic_launcher" />

 import android.app.Activity;
 import android.os.Bundle; 
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.ImageButton;
 import android.widget.Toast;

 public class MainActivity extends Activity {

ImageButton imgButton;

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

    addListenerOnButton();
}

private void addListenerOnButton() {

    imgButton = (ImageButton) findViewById(R.id.imageButton1);

    imgButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT)
                    .show();
        }
    });
}
}