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" />
letroll
  • 1,059
  • 1
  • 14
  • 36
  • 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. – fweigl Apr 26 '13 at 09:54
  • why you are using this..?? `android:layout_height="match_parent"` can we see snapshot of this xml.? – SilentKiller Apr 26 '13 at 09:55
  • 1
    where is your activity code? – MR. Kumar Apr 26 '13 at 10:23
  • @Ascorbin my setOnClickListener is in a method that is called in the onCreate – letroll Apr 26 '13 at 11:57
  • @letroll open bounty and not to post whole code is not good way.post here whatever you have done. – TheFlash May 06 '13 at 07:28
  • @Pratik I have set Answer to PeeVee to give him the bounty and not lost it, but nothing help me. I think the bug is an issue from android plateforme... But yes I'm not sure. I say that because, I use onClickListener since severals years without any issue. But here nothing work... onclick in xml, in code in very different ways... Nothing! And I have checked Id and other things, but no typo errors :( – letroll May 09 '13 at 17:16
  • 1
    Also, see if this works: http://stackoverflow.com/a/43777655/1155282 – Irshu May 04 '17 at 08:32

9 Answers9

26

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

SemperFi
  • 2,358
  • 6
  • 31
  • 51
  • 1
    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? – Parag Kadam Oct 20 '17 at 10:18
13

I solved this by simply setting:

<ImageButton
    android:id="@+id/imageButtonFiltroNome"
    ...
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false" />
Energy Dome
  • 153
  • 3
  • 8
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;
    }
});
timyau
  • 812
  • 1
  • 16
  • 27
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.

Justjyde
  • 322
  • 1
  • 3
  • 13
2

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

android:descendantFocusability="afterDescendants"
Johann
  • 27,536
  • 39
  • 165
  • 279
  • 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. – user1510006 Jan 02 '19 at 11:18
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

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.

ahmed_khan_89
  • 2,755
  • 26
  • 49
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.

user12927542
  • 151
  • 2
  • 2
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.

steerr
  • 151
  • 2
  • 4
  • 13