Search code examples
androideventsbuttonclick

why my button does not work on the first click?


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

Solution

  • 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