Zach J.
Zach J.

Reputation: 54

Help with Image Buttons in Eclipse

Hey I was wondering how you make it so that when an image button is pushed it changes color to show it was pushed.

DragonFruitActivity.java

package com.Dragon_Fruit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                setBackgroundResource(R.drawable.playbuttonselected);
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
            }

            private void setBackgroundResource(int playbuttonselected) {
                // TODO Auto-generated method stub

            }


});
        ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); settingsbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, settingsbutton.class));
            }


});
    }
}

So the new Activity should be:

package com.Dragon_Fruit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                arg0.setBackgroundResource(R.drawable.playbuttonselected);
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
            }


});
        ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); settingsbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, settingsbutton.class));
            }


});
    }
}

Upvotes: 1

Views: 4745

Answers (4)

Phil
Phil

Reputation: 36299

In your onClick(...) method, you can setBackgroundResource(...)

ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); 
playbutton.setOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View arg0) {
        setBackgroundResource(...);
        startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
    }
}

(you will also need to define drawable to use as the background color).

Upvotes: 0

bitbybit
bitbybit

Reputation: 579

Please use StateListDrawable.

Upvotes: 1

the100rabh
the100rabh

Reputation: 4147

What you are looking for is selector http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Use them to change the image button color.

Upvotes: 0

user874649
user874649

Reputation:

You can have a look at this. You can download that example and replace the android:drawable with your images.

Upvotes: 0

Related Questions