Tutorial :Why my buttons OnClick event fails to fire?



Question:

I have an activity, where the ListView holds customized linear layout elements for each row. One of the rows has a button defined as:

 <Button              android:text="Pick a contact"              android:id="@+id/btnPickContact"              android:layout_width="wrap_content"              android:gravity="center_vertical"              android:layout_height="wrap_content"></Button>  

Then in java, I have this code:

((Button) row.findViewById(R.id.btnPickContact)).setOnClickListener(new View.OnClickListener() {                    @Override                  public void onClick(View v) {                      Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);                      intent.putExtra(EXTRA_ONLINE_ID, (String)v.getTag(TAG_ONLINE_ID));                      act.startActivityForResult(intent, PICK_CONTACT);                  }              });  

In this setup the event fails to start.

Also I've tried by implementing the interface:

@Override      public void onClick(View v) {              Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);              intent.putExtra(EXTRA_ONLINE_ID, (String)v.getTag(TAG_ONLINE_ID));              startActivityForResult(intent, PICK_CONTACT);      }  

still no luck, the event doesn't fire.

What to do?


Solution:1

In the xml defining the button you need to specify a handler using onclick="somehandler". And in your activity have

public someHandler(View v) {  }  

v will be the button.


Solution:2

Edit: I think this might answer your question: Android: ListView elements with multiple clickable buttons


Solution:3

Are you sure its the event that's not firing and not a problem launching the intent?

add log.d("Mytest","OnClick Fired!!!"); as the first line in onClick, then see if you get your message in the debug log.


Solution:4

You should consider to set clickable and forcusable property to the row layout.

@Override  public View getView(int position, View convertView, ViewGroup parent) {        //Inflate view ....          button1.setOnClickListener(onclickListener);            convertView.setClickable(true);        convertView.setFocusable(true);        return converView;  }  


Solution:5

try this use this

 (new OnClickListener(){....  

instead of this

(new View.OnClickListener(){....  

here is code...

 ((Button) row.findViewById(R.id.btnPickContact)).setOnClickListener(new OnClickListener() {                @Override              public void onClick(View v) {                  Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);                  intent.putExtra(EXTRA_ONLINE_ID, (String)v.getTag(TAG_ONLINE_ID));                  act.startActivityForResult(intent, PICK_CONTACT);              }          });  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »