What's the difference between a button and an imageButton?

3

The image I used to make the tests is a simple 100x100 size gear with the transparent background.

FirstofallInoticedthatunlikeButton,whencreatinganImageButtonyouareobligedtoaddanimagetoit:

Notethatifnoimageisselected,itscreationisdisabled(whichmakesperfectsense).

IselectedanimagesothatIcouldcreatetheImageButtonandsoonrealizedthatbehindtheimage(gear)appearsa"gray stop" covering all its background:

Butwhat'sup?That'sit?Icouldverymuchaddabackgroundtoanormalbuttonanditwouldhavethesameeffect,aswellasnothavingthat" useless gray stop " behind.

Followingthetests=>WhenIincreasedthesizeoftheButtonfrom100x100to150x150,obviouslythequalityoftheimageresolutiondecreased,butdoingsowiththeImageButtonthesizeofthegearremainedthesameandwhatincreasedwasthe" strong> "behind the image:

OnemorethingthathappensintheButtonthatdoesnothappenintheImageButton,isthatwhenyousetthebackgroundoftheButton,ifithassometextanditisnotremoved,itkeepsappearing,withtheimage:

InfavoroftheImageButton(dependingontheneed)Inoticedthatclickingonit,thereisaneffectontheclick.It'ssimple,butthere's:

IntheButtonthereisalsothiseffect,butonlywhennobackgroundissetinit.

InMainActivitythedeclarationanduseofbothisthesame:

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;

        }

    }

}

In the whole post, I quoted almost every time the " gray patch " that is created behind the image of the ImageButton as " stop gray useless ", but it (ImageButton) can and should have some utility , I just do not know which one.

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

    
asked by anonymous 30.03.2017 / 00:47

1 answer

3

The differences come from the class that each descend.

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

ImageButton descends from ImageView. Makes an ImageView clickable by adding a visual effect when clicked.

So, the foreground of a Button, assigned by android:text="" , is a text, whereas in ImageButton, assigned by android:src="" is an image.

In both you can 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 another depends on the foreground you want, text or image.

    
30.03.2017 / 16:28