What is the real difference between a button and an imageButton?

Asked

Viewed 1,958 times

3

The image I used to do the tests is a simple gear of dimensions 100x100 with transparent background.

inserir a descrição da imagem aqui

At first I noticed that differently from the Button, by creating a Imagebutton you are bound to add an image to it:

inserir a descrição da imagem aqui

Note that if no image is selected, its creation is impossible (which makes perfect sense).

I selected an image so I could create the Imagebutton and immediately realized that behind the image (gear) appears a "gray stop" covering all your background:

inserir a descrição da imagem aqui

But what? Is that it? I could very much add a background to a normal button and get the same effect besides not having that "useless gray stop" behind.

inserir a descrição da imagem aqui

Following with the tests => When I increased the size of the Button from 100x100 to 150x150, obviously the quality of the image resolution decreased, but in doing so with the Imagebutton the size of the gear remained the same and what increased was the "useless gray stop" behind the image:

inserir a descrição da imagem aqui

One more thing that also happens in the Button that does not happen in Imagebutton, is that when setting the background of the Button, if it has some text and the same is not removed, it keeps appearing, thus coming into conflict with the image:

inserir a descrição da imagem aqui

In favor of Imagebutton (depending on the need) I noticed that when clicking on it, there is an effect on the click. It is simple, but there is:

inserir a descrição da imagem aqui

Button also has this effect, but only when no background is set in it.

In Mainactivity the declaration and use of both is the same:

package genesysgeneration.classsound;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button button;
    private ImageButton imageButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=(Button)findViewById(R.id.button);
        imageButton2=(ImageButton)findViewById(R.id.imageButton2);

        button.setOnClickListener(this);
        imageButton2.setOnClickListener(this);

    }

    public void onClick(View v){

        switch (v.getId()){

            case R.id.button:

                Intent it01 = new Intent(this, Main2Activity.class);
                startActivity(it01);
                break;

            case R.id.imageButton2:

                Intent it02 = new Intent(this, Main2Activity.class);
                startActivity(it02);
                break;

        }

    }

}

Throughout the post, I cited almost every time the "gray stuff" which is created behind the image of Imagebutton as "useless gray stop", but he (Imagebutton) can and should be of some use, I just don’t know which one yet.

I would like to know in which cases would be preferable to use an Imagebutton instead of a Button and what their real differences are other than these, which I consider to be minimal and do not necessarily have greater and/or better functionality.

  • 1

    guy, on android, Voce can use anything as a button since Voce implements the way Component.setOnClickListener(new Onclicklistener ...{}); dai whatever makes it a layout, a button, an imageview, a toglebutton, and so on, remembering that Voce can create a file. xml in the drawable folder with a "selector" to create any effect you want using images and the "state_checked=true or false" items to change component targeting

  • living and learning - I find very interesting these possibilities that the android studio provides

  • 1

    You can remove the gray stop using this: style="?attr/borderlessButtonStyle" and then this to add a click effect: android:background="?attr/selectableItemBackgroundBorderless"

1 answer

4


The differences come from the class from which each descends.

Button descends from Textview. Makes a clickable Textview by adding a visual effect when clicked.

Imagebutton descends from Imageview. Makes a clickable Imageview by adding a visual effect when clicked.

Thus the foreground of a Button, assigned by android:text="", is a text, while in Imagebutton, assigned by android:src="" is an image.

In both it is possible to change the background, but the visual effect, when clicked, can be lost if the chosen drawable does not have states.

The choice between one and the other depends on the foreground what you want, text or image.

  • For a cuter visual effect, I usually use: style="?attr/borderlessButtonStyle" and android:background="?attr/selectableItemBackgroundBorderless‌​"...

  • 2

    So you take the standard "gray background" and leave only your image with a click effect

  • I didn’t know I could classify as TextView in relation to the Button. +1

  • So in terms of functionality do not differ at all? Are there no cases where one is extremely necessary and the other is not? The use of both is more for convenience and/or practicality?

  • Both are buttons, so in terms of functionality they are equal. As I said in the reply, the choice depends mainly on whether you want to have a text or an image on the button.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.