Question :
The image I used to make the tests is a simple 100×100 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;
}
}
}
Answer :
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.