Eduard Castellano
Eduard Castellano

Reputation: 11

How to ad an effect to an ImageButton on an android app?

I currently added two ImageButtons on my android application project, but when I press then on my android device, there is no effect that the button gets indented. How can I put an effect to it so it looks more like a button when it's pressed?

My full source code looks something like this:

    final TextView text2 = (TextView) findViewById(R.id.textView2);
    text2.setText("");
    final TextView text = (TextView) findViewById(R.id.textView1);
    text.setText("");
    final Button button3 = (Button) findViewById(R.id.button1);
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked1 = 0;
            clicked2 = 0;
            text2.setText("  " + clicked1 + " SHOTS ");
            text.setText("  " + clicked2 + " CUPS ");
        }
    });

    final TextView text1 = (TextView) findViewById(R.id.textView2);
    text1.setText("  0 SHOTS");
    final ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked2++;
            text1.setText("  " + clicked2 + " SHOTS ");
        }
    });
    final TextView text3 = (TextView) findViewById(R.id.textView1);
    text3.setText("  0 CUPS");
    final ImageButton button = (ImageButton)findViewById(R.id.imageButton1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked1++;
            text3.setText("  " + clicked1 + " CUPS ");

        }


    });

}

}

Upvotes: 1

Views: 236

Answers (1)

nobalG
nobalG

Reputation: 4620

You can do this,very easily,lets try using simple button on place of your ImageButtons and do following


Create an xml file and save it in one of the drawable folders in your project,let bg.xml,set this one as the background of your Button

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

where button_pressed and button_notpressed are the images which you want to show on transition of the clicks and normal view of button

In your activity's xml file,set the background of the button as bg.xml

<Button
        android:id="@+id/btn"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:textSize="20dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="450dp"
        android:background="@drawable/bg"  <--! set it like this-->
        android:text="Submit"
        android:textColor="#FFFFFF" /> 

Upvotes: 1

Related Questions