1

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));
            }


});
    }
}
Zach J.
  • 54
  • 1
  • 10
  • Check this old post out http://stackoverflow.com/questions/3024983/how-do-i-change-the-tint-of-an-imagebutton-on-focus-press – sealz Aug 04 '11 at 15:36
  • You don't need your own `setBackgroundResource(...)` method, there's one built-in to the `View` class: http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int) – Phil Aug 04 '11 at 16:56
  • ahh, I see where the problem is. This is my mistake: remove the extra method setBackgroundResource, and add `arg0.` before the call to this method in your onClick method: `arg0.setBackgroundResource(R.drawable.playbuttonselected);` – Phil Aug 04 '11 at 17:04
  • I added what the new activity should be. Does it look correct? – Zach J. Aug 04 '11 at 17:12

4 Answers4

1

Please use StateListDrawable.

bitbybit
  • 579
  • 1
  • 6
  • 16
0

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

0

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.

the100rabh
  • 4,077
  • 4
  • 32
  • 40
0

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).

Phil
  • 35,852
  • 23
  • 123
  • 164
  • So this is the code for the button in the activity: ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub startActivity(new Intent(DragonFruitActivity.this, playbutton.class)); – Zach J. Aug 04 '11 at 16:29
  • so where would setBackgroundResource go? – Zach J. Aug 04 '11 at 16:30
  • before the `startActivity(...)` method is called. – Phil Aug 04 '11 at 16:33
  • ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() { public void setBackgroundResource (int redid) { } @Override public void onClick(View arg0) { // TODO Auto-generated method stub startActivity(new Intent(DragonFruitActivity.this, playbutton.class)); – Zach J. Aug 04 '11 at 16:36
  • so like that? But how do I use the background resource to change the color when pushed – Zach J. Aug 04 '11 at 16:37
  • Ok I got that part but how do I actually fill in the setBackgroundResource? is it like @drawable? – Zach J. Aug 04 '11 at 16:44
  • Assuming you have the drawable `res/drawable/mybackground.png`, or something like this, you can access it with `R.drawable.mybackground`. So the method call would be `setBackgroundResource(R.drawable.mybackground);` – Phil Aug 04 '11 at 16:46
  • Well its not working but I will put my activity code in the question. Could you take a look and see if theres anything wrong. Also, thanks for helping me out. – Zach J. Aug 04 '11 at 16:52