Usman Ahmed
Usman Ahmed

Reputation: 79

Android Onclick

How are you? I am new at Android programming. so far I have run a single app on my mobile phone using 4.1 API 16. My question is when releasing a click event the button image in the following code should be restored to previous state.

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        x = x + 1;
        edittext.setText("" + x);
        button.setBackgroundResource(R.drawable.buttonpressed1);
    }
});

Upvotes: 0

Views: 186

Answers (5)

ZInj
ZInj

Reputation: 341

You need to use a selector. The selector needs to be a drawable file which is assigned to the background of your button. An example of a selector

    <?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android">

          <!-- focused -->
        <item
            android:state_focused="true"
            android:drawable="@drawable/rounded_button_focused"
            />
        <!--pressed -->
        <item
            android:state_pressed="true"
            android:drawable="@drawable/rounded_button_pressed"
            />


        <item
            android:drawable="@drawable/rounded_button_unfocused"/>


    </selector>

The selector will basically assign the correct background depending on the state of your button. If the button is pressed then it will assign the file "rounded_button_pressed" to the background of the button. The rounded_button_pressed is another drawable file for example:

    <?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">


    <gradient android:startColor="@color/WhiteSmoke"
        android:endColor="@color/LightGrey"
        android:angle="270"/>
    <stroke
        android:width="2dp"
        android:color="@color/CornflowerBlue" />
    <corners
        android:radius="4dp"

        />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

Hope it solves your problem

Upvotes: 0

user4009281
user4009281

Reputation:

There is a full solution for you:

ImageButton guessButton;

guessButton = (ImageButton) findViewById(R.id.Knorr_Game_Guess_Button);
        guessButton.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) 
           {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    v.setBackgroundResource(R.drawable.ic_picture1);
                } 
                else if (event.getAction() == MotionEvent.ACTION_UP) 
                {
                    v.setBackgroundResource(R.drawable.ic_picture2);
                }
                return false;
            }
        });

It works:)

Upvotes: 0

Stanislav
Stanislav

Reputation: 405

For normal performance you must use selector like background property of Button view put all of this files to folder drawable selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/br_unpressed" />
</selector>

bg_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="@dimen/circle_padding"
        android:color="@android:color/transparent" />
    <solid android:color="@android:color/white" />
</shape>

bg_unpressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="@dimen/circle_padding"
        android:color="@android:color/transparent" />
    <solid android:color="@android:color/black" />
</shape>

and in layout set button background android:background="@drawable/selector"

Upvotes: 1

user4029378
user4029378

Reputation:

you can also use drawable touch states. an example showing color change on press:

in your button xml use android:background="@drawable/touchstates"

and touchstates.xml:

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

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke android:color="#cc0099CC" android:width="1dip" />
            <solid android:color="#660099CC" />
        </shape>
    </item>
</selector>

Upvotes: 2

gilonm
gilonm

Reputation: 1859

If you want to capture the release of a button, you need to use OnTouchListener

Here is an example:

yourButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //What happens when preessed
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            //What happens when released            }
    }
};

Hope this helped!

Upvotes: 0

Related Questions