Madhukar Hebbar
Madhukar Hebbar

Reputation: 3183

OnClick Listener button pressed state

I am having four button where user should select one button at a time.

<Button
    android:id="@+id/button1"
    android:layout_weight="1"
    android:background="@drawable/btnindicator"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:drawableLeft="@drawable/btnbg"
    android:gravity="center"
    android:padding="20dip"
    android:text="Button1"
    android:textColor="#000000" />

<Button
    android:id="@+id/button2"
    android:layout_weight="1"
    android:background="@drawable/btnindicator"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:drawableLeft="@drawable/btnbg"
    android:gravity="center"
    android:padding="20dip"
    android:text="Button2"
    android:textColor="#000000" />

<Button
    android:id="@+id/button3"
    android:layout_weight="1"
    android:background="@drawable/btnindicator"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:drawableLeft="@drawable/btnbg"
    android:gravity="center"
    android:padding="20dip"
    android:text="Button3"
    android:textColor="#000000" />

<Button
    android:id="@+id/button4"
    android:layout_weight="1"
    android:background="@drawable/btnindicator"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:drawableLeft="@drawable/btnbg"
    android:gravity="center"
    android:padding="20dip"
    android:text="Button4"
    android:textColor="#000000" />

here is the btnindicator.xml which is used to show pressed/focused state of a button.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <gradient android:angle="270" android:endColor="@color/LightBlue" android:startColor="@color/DodgerBlue" />
            <stroke android:width="3dp" android:color="@color/PowderBlue" />
            <corners android:radius="3dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <gradient android:angle="270" android:endColor="@color/LightBlue" android:startColor="@color/DodgerBlue" />
            <stroke android:width="3dp" android:color="@color/PowderBlue" />
            <corners android:radius="3dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>

    <item>
        <shape>
            <gradient android:angle="270" android:endColor="@color/Wheat" android:startColor="@color/WhiteSmoke" />
            <stroke android:width="3dp" android:color="@color/FloralWhite" />
            <corners android:radius="3dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>
</selector>

Here is the code to listen on Click listener

public Button button1,button2,button3,button4;
button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"Clicked button1");
                selected = true;
            }
        });

But the problem is the click event is triggered for the non focused button only if the button clicked twice.

Ex: If button1 is pressed then if i click twice on button2 then button2 on click listener will get invoked.

I changed with button1.setOnFocusChangeListener() and button1.setOnTouchListener() where both are working fine.

But i am not able to get what's the problem with setOnClickListener method ..

Upvotes: 0

Views: 454

Answers (1)

Volodymyr Yatsykiv
Volodymyr Yatsykiv

Reputation: 3211

Set to your root layout

android:splitMotionEvents="false"

Hope, this will help you.

Upvotes: 1

Related Questions