Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Button needs two click for action

I have a form. 7 EditText in vertical layout. On screen in one moment can see only 3 EditText (form is big so I need to scroll up and down to fill all fields). In bottom - Button.

When I fill top EditText (or one of top, which is invisible when I scrolled down to button), and focus (cursor) in this EditText, when I scrolled down and try to click Button once - nothing happens. when I click again - happens button action.

When both EditText with focus and Button is visible - Button needs one clicks.

I think that in first case first click just takes focus. And second click is "real" click.

How I can fix it? I need only one click on Button.

like image 340
newmindcore Avatar asked May 07 '11 08:05

newmindcore


People also ask

Why we use setOnClickListener in Android?

setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.


3 Answers

The question is old and I don't know whether this will solve your problem, since your pastebin link doesn't work anymore, but since I stumbled upon your post with the same problem and I found a solution I will post it anyway:

In my case the problem occured when I applied a custom style to a button following a tutorial:

<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> 

The problem was the following line:

<item name="android:focusableInTouchMode">true</item> 

Once I removed it the button worked as expected.

Hope this helps.

like image 134
maves Avatar answered Sep 27 '22 18:09

maves


I used the following to solve a similar problem. It automatically clicks on the item again if the first click only gets focus:

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

I needed focusableInTouchMode to be true.

like image 26
user2137040 Avatar answered Sep 27 '22 18:09

user2137040


Similar to Maves answer. This did the trick for me:

<Button 
    style="@android:style/Widget.EditText"
    android:focusableInTouchMode="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
like image 33
Chase Roberts Avatar answered Sep 27 '22 17:09

Chase Roberts