turtleboy
turtleboy

Reputation: 7574

place an image over a button

I have a button on my main screen. I'd like to display a padlock image on the button when a global variable changes its state.

What would be the best way of setting an image on a standard button? Do i have to make the button an imageButton or do i simply setBackground...()?

Button signin = (Button)findViewById(R.id.buttonsignin);
               // signin.setBackgroundResource(R.drawable.signin);

                alpha = new AlphaAnimation(0.3F, 0.8F); //Set opacity - Range 0.0 to 1.0
                alpha.setDuration(0); // Set animation duration
                alpha.setFillAfter(true); // Maintaining the effect to the button
                signin.startAnimation(alpha);
                signin.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        .....

                    }
                });

Upvotes: 0

Views: 104

Answers (3)

Jay Snayder
Jay Snayder

Reputation: 4338

Often or not, I don't even use a button when I am dealing with a depression of something that will hold an image, I'll just use an ImageView with different states. But as mentioned, using a selector makes the job considerably easier from the XML code without having to worry about handling toggles on your own end; multiple ways to achieve the desired effect.

Upvotes: 0

Neha Gupta
Neha Gupta

Reputation: 857

I think it's better if you use button and then give its attributes in the xml file so that you can play with the attributes like pressed state. But if you give setBackground="image path" then you won't be able to give properties like state_pressed.

Upvotes: 2

ClintL
ClintL

Reputation: 1453

Another option would be to have a relative layout holding both an imageview and a standard button so the button can have text while also showing the button state in the imageview.

Upvotes: 1

Related Questions