why my button does not work on the first click?
Asked Answered
T

9

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" />
Trimly answered 26/4, 2013 at 9:50 Comment(7)
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.Northing
why you are using this..?? android:layout_height="match_parent" can we see snapshot of this xml.?Recife
where is your activity code?Alric
@Ascorbin my setOnClickListener is in a method that is called in the onCreateTrimly
@Trimly open bounty and not to post whole code is not good way.post here whatever you have done.Soliz
@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 :(Trimly
Also, see if this works: https://mcmap.net/q/326928/-onclick-event-is-not-triggering-androidCredence
E
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

Eisenberg answered 6/5, 2013 at 4:52 Comment(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?Mou
P
13

I solved this by simply setting:

<ImageButton
    android:id="@+id/imageButtonFiltroNome"
    ...
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false" />
Phillada answered 1/4, 2014 at 9:35 Comment(2)
Nah, that has no effect.Azurite
amazingly it works!! clickable true but focusable false and android:focusableInTouchMode false specially when working with camera interface or surfaceview behind it!!.Wreathe
C
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;
    }
});
Croton answered 6/5, 2013 at 7:46 Comment(1)
setFocusabeInTouchMode(false) has no effect in this scenario.Azurite
B
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.

Beckybecloud answered 5/5, 2013 at 9:39 Comment(0)
K
2

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

android:descendantFocusability="afterDescendants"
Kilmer answered 20/11, 2015 at 21:8 Comment(1)
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.Goshen
V
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

Vetavetch answered 9/1, 2015 at 12:36 Comment(0)
G
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://mcmap.net/q/388084/-onclick-method-not-working-properly-after-nestedscrollview-scrolled

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

Gorge answered 16/7, 2018 at 16:43 Comment(0)
G
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.

Gan answered 25/10, 2021 at 11:59 Comment(0)
N
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.

Nils answered 11/1, 2022 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.