14

I have a problem with a button that does not generates click event when I use it for the first time, but if I click on the screen other than on the button and then I click on it. It works directly!

In my fragment onCreateView I have:

 viewAnimator = (ViewAnimator) inflater.inflate(R.layout.fragment_login_supplier, container, false); initView(viewAnimator); 

and in initView:

private void initView(ViewAnimator ll) { ...... errorButton = (Button) errorLayout.findViewById(R.id.buttonError); errorButton.setBackgroundResource(btnErrorSelector); errorButton.setOnClickListener(FragmentLoginSupplier.this); ..... 

}

my fragment implements OnClickListener but my : @Override public void onClick(View vue) {} receive nothing first time ...

the button id : buttonError

in here the beginning of the layout:

<ScrollView android:id="@+id/scrollViewForm" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" > <LinearLayout android:id="@+id/login_form_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout android:id="@+id/RelativeLayoutErrorMessage" android:layout_width="match_parent" android:layout_height="@dimen/button_height" android:background="@color/DarkGray" android:visibility="gone" > <ImageView android:id="@+id/ImageViewErrorMessage" android:layout_width="15dp" android:layout_height="15dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:contentDescription="@string/todo" android:src="@drawable/alert_white" android:visibility="gone" /> <TextView android:id="@+id/textViewErrorMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_toLeftOf="@+id/buttonError" android:layout_toRightOf="@+id/ImageViewErrorMessage" android:text="@string/vous_n_avez_pas_encore_ajout_de_compte" android:textColor="@color/white" /> <Button android:id="@+id/buttonError" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="5dp" android:background="@drawable/button_suppression_noir_selector" /> </RelativeLayout> <View android:id="@+id/RelativeLayoutErrorMessageBottomBorder" android:layout_width="wrap_content" android:layout_height="1dp" android:background="#FFFFFFFF" android:visibility="gone" /> 
7
  • 2
    Let us see your actual code, not just your layout. I'm sure you set the onClickListener to the button somewhere else than in onCreate. Commented Apr 26, 2013 at 9:54
  • why you are using this..?? android:layout_height="match_parent" can we see snapshot of this xml.? Commented Apr 26, 2013 at 9:55
  • 1
    where is your activity code? Commented Apr 26, 2013 at 10:23
  • @Ascorbin my setOnClickListener is in a method that is called in the onCreate Commented Apr 26, 2013 at 11:57
  • 1
    Also, see if this works: stackoverflow.com/a/43777655/1155282 Commented May 4, 2017 at 8:32

9 Answers 9

26
+50

I don't know whether this will solve your problem.But even I had a problem with button clicks.This code worked out for me.I think that in first case first click just takes focus. And second click is "real" click.

<style name="ButtonStyle" parent="@android:style/Widget.Holo.Button"> <item name="android:focusable">true</item> <item name="android:focusableInTouchMode">true</item> <item name="android:clickable">true</item> <item name="android:background">@drawable/custom_button</item> <item name="android:textColor">@color/somecolor</item> <item name="android:gravity">center</item> </style> 

Please try it and let me know.

The other solution you can try put is to add in abc.java

input.setOnFocusChangeListener(new View.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { v.performClick(); } } }); 

and make <item name="android:focusableInTouchMode">true</item> in your Xml.

Please let me know if it worked out

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain why the button click first took focus and then from second time onwards it really got clicked? Buttons don't usually behave this way but why did this happen to you?
13

I solved this by simply setting:

<ImageButton android:id="@+id/imageButtonFiltroNome" ... android:clickable="true" android:focusable="false" android:focusableInTouchMode="false" /> 

2 Comments

Nah, that has no effect.
amazingly it works!! clickable true but focusable false and android:focusableInTouchMode false specially when working with camera interface or surfaceview behind it!!.
10

Just set errorButton:

errorButton.setFocusableInTouchMode(false); 

or add

errorButton.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View view, MotionEvent event) { if (MotionEvent.ACTION_UP == event.getAction()) { view.performClick(); return true; } return false; } }); 

1 Comment

setFocusabeInTouchMode(false) has no effect in this scenario.
2

You probably have a validation event handler somewhere that runs the first time. Look at the validations in the last control in the tab sequence. You may find something there.

Comments

2

If your control is inside a scrollable container, set this on the container:

android:descendantFocusability="afterDescendants" 

1 Comment

I am facing the same issue for some views in custom keyboard. descendantFocusability="afterDescendants" did not work for me for normal view group views. I used android:descendantFocusability="blocksDescendants" in view groups layouts and it worked for me. changing descendantFocusability attribute is the key for my issue.
0

I faced same issue, and let me tell you how I fixed it. I have xml in the form.

<RelativeLayout android:id="@+id/rlContactUs" style="@style/SettingsItemContainer" > <com.CustomTextView android:id="@+id/tvContactUs" style="@style/SettingsItem" android:text="@string/contactUs"/> </RelativeLayout> 

SettingsItemContainer had the following in style.

<style name="SettingsItemContainer"> other things <item name="android:clickable">true</item> <item name="android:focusable">true</item> <item name="android:focusableInTouchMode">true</item> </style> 

Now the relative layout did not receive the first click even on touch. So I changed it to:

<style name="SettingsItemContainer"> other things <item name="android:clickable">true</item> <item name="android:focusable">true</item> <item name="android:focusableInTouchMode">false</item> </style> 

This solved the problem

Comments

0

Well I suffered also from such an issue. In my case I was having a form and a button in a NestedScrollView. I could solve this click issue following this answer https://stackoverflow.com/a/47161693/3014036

basically you need to add this costum layout behaviour to your AppBar https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2.

Comments

0

So, if you use not correct style to your view, you can have this problem too. For examle, I use editText_style for textview.

Comments

0

If you are experiencing this issue with TextView, the reason might be that textIsSelectable is set to "true". Removing this attribute or setting it to "false" will make the first tap detected.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.