Why is my button not working the first time?

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

In my onCreateView snippet, 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 snippet implements OnClickListener, but my: @Override public void onClick (View vue) {} get nothing the first time ...

button id: buttonError

here is the start 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" /> 
+4
source share
6 answers

I do not know if this will solve your problem. But even I had a problem with a button press. This code worked for me. I think that in the first case, the first click just takes focus. And the second click is a β€œ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 and let me know.

Another solution you can try is to add to 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 this worked.

+11
source

Just install 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; } }); 
+5
source

You probably have a validation event handler that fires for the first time. Look at the checks in the last control in the tab sequence. You can find something there.

+2
source

I solved this by simply setting:

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

If your control is inside a scrollable container, set it in the container:

 android:descendantFocusability="afterDescendants" 
+1
source

I ran into the 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 has the following 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 has not received the first click, even when touched. 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.

0
source

All Articles