Android Imageview's Onclicklistener Does Not Work

Android ImageView's onClickListener does not work

Ok,

I managed to solve this tricky issue. The thing was like I was using FrameLayout. Don't know why but it came to my mind that may be the icon would be getting hidden behind some other view.

I tried putting the icon at the end of my layout and now I am able to see the Toast as well as the Log.

Thank you everybody for taking time to solve the issue.. Was surely tricky..

ImageView onClickListener() not working

this should solve the problem

ImageView cus_image = (ImageView) findViewById(R.id.cust_list);
cus_image.setOnClickListener(new View.OnClickListener()
{
public void onClick(final View v)
{
Intent myIntent = new Intent(v.getContext(), Customer.class);
startActivity(myIntent);
}
});

change R.drawable with R.id

OnClickListener isn't working for ImageView

you may need to make your ImageView clickable, either using setClickable(true) or android:clickable="true" in XML

http://developer.android.com/reference/android/view/View.html#setClickable(boolean)

onClickListener() misses sometimes for ImageView

  1. Size of Image is so small
  2. You can use Toolbar instead of custom action bar. You can call: getSupportActionBar().setDisplayHomeAsUpEnabled(true);

OnClickListener of ImageView is not Working

  1. You must not findviewbyid() of an ImageView in some other function other than Oncreate(Bundle b) or Some Listener(e.g OnclickListener ,onselectionchange etc) . if you want to findviewbyid(...) of a view in a function other than Oncreate ,Atleast Call that function once after SetContentView() and other prerequisite Findviewbyid(...).

2.
In order to Refresh your Activity ,when you can not use function containing findviewbyid(...) of ImageView,like in my case, in body some button(e.g Refresh ,Reset) OnclickListner .You must write the following code

     buttonreset.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Bundle newBundle=new Bundle();
onCreate(newBundle);

//func(); >>We can not use this func() here if we are using it in Oncreate(...) in case of Array of ImageView

}});
}

For views other than Imageview ,Maybe Regular Line of Code work ...But in case of ImageView or Array of ImageView ....These things must be kept in mind and Simply Oncreate() must be used to Refresh.

OnClick Listener on ImageView on Toolbar not working

Try this code snippet:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);

AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ImageView imageButton = (ImageView) toolbar.findViewById(R.id.back);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
}

EDIT

for setting the toolbar you need to add:

in manifest:

<activity android:name=".YourActivity"
android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->

in res/values/styles.xml:

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>


Related Topics



Leave a reply



Submit