Reputation: 43
I am trying to make my imageview that is defined in my custom layout for Actionbarsherlock clickable. My activity first sets a layout:
setContentView(R.layout.myLayout);
The actionbar_layout is set in the same activity like this:
View cView = getLayoutInflater().inflate(R.layout.actionbar_layout,
null);
actionBar.setCustomView(cView);
The actual actionbar_layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal" >
<ImageView
android:id="@+id/actionBarLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="50dp"
android:paddingLeft="10dp"
android:paddingRight="50dp"
android:paddingTop="50dp"
android:scaleType="centerCrop"
android:src="@drawable/logo" />
</LinearLayout>
Adding this:
actionBarLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code
}
});
Results in a Nullpointer exception for this line:
actionBarLogo.setOnClickListener(new View.OnClickListener()
The custom layout works fine, i just need the imageview to be clickable. Any ideas how to fix this? How can my activity get a reference to my actionBarLogo id which is defined in actionbar_layout.xml?
Upvotes: 2
Views: 1366
Reputation: 2097
Use ImageButton insteadOf ImageView
<ImageButton
android:id="@+id/actionBarLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="50dp"
android:paddingLeft="10dp"
android:paddingRight="50dp"
android:paddingTop="50dp"
android:scaleType="centerCrop"
android:onClick="somemethod"
android:src="@drawable/logo" />
Upvotes: 2
Reputation: 4119
Try something like this:
//your code
View cView = getLayoutInflater().inflate(R.layout.actionbar_layout,
null);
actionBar.setCustomView(cView);
ImageView actionBarLogo = (ImageView) cView.findViewById(R.id.actionBarLogo);
actionBarLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code
}
});
It should work.
Upvotes: 3
Reputation: 5694
as far as i understand your problem, its not possible to access the desired view, because its not part of the view or part of the window decoration. Check this post here, to find a workaround: https://stackoverflow.com/a/10187801/1965084
Upvotes: 0