{"id":841,"date":"2016-02-07T07:47:45","date_gmt":"2016-02-07T07:47:45","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=841"},"modified":"2018-06-05T09:54:08","modified_gmt":"2018-06-05T09:54:08","slug":"imagebutton","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/imagebutton","title":{"rendered":"ImageButton Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, ImageButton is used to display a normal button with a custom image in a <a href=\"\/ui\/button\">button<\/a>. In simple words we can say, ImageButton is a button with an image that can be pressed or clicked by the users. By default it looks like a normal button with the standard button background that changes the color during different button states.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-855\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ImageButton.jpg\" alt=\"ImageButton\" width=\"453\" height=\"241\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageButton.jpg 453w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-300x160.jpg 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/center>An image on the surface of a button is defined within a xml (i.e. layout ) by using src attribute or within java class by using setImageResource() method. We can also set an image or custom drawable in the background of the image button.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Standard button background image is displayed in the background of button whenever you create an image button. To remove that image, you can define your own background image in xml by using background attribute or in java class by using setBackground() method.<\/p>\n<p><strong>Below is the code and\u00a0image which shows how custom imagebutton looks in Android:<\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">Important\u00a0Note:<\/span>\u00a0<\/strong>ImageButton has all the properties of a normal button so you can easily perform any event like click or any other event which you can perform on a normal button.<\/p>\n<p><strong>ImageButton code in XML:<\/strong><\/p>\n<pre>&lt;!--Make Sure To Add Image Name home in Drawable Folder--&gt;\r\n&lt;ImageButton\r\nandroid:id=\"@+id\/simpleImageButton\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:src=\"@drawable\/home\" \/&gt;\r\n<\/pre>\n<p><center>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-844\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-in-Android.jpg\" alt=\"ImageButton in Android\" width=\"220\" height=\"213\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Attributes of ImageButton:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some important\u00a0attributes that helps us to configure a image button in your xml file (layout).<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> id is an attribute used to uniquely identify a image button. Below is the example code in which we set the id of a image button.<\/p>\n<pre>&lt;ImageButton\r\n<strong>android:id=\"@+id\/simpleImageButton\"<\/strong>\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\/&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. src:<\/strong><\/span> src is an attribute used to set a source file of image or you can say image in your image button to make your layout look attractive.<\/p>\n<p>Below is the example code in which we set the source of an image button.\u00a0Make sure you have saved an image in drawable folder name home before using below code.<\/p>\n<pre>&lt;ImageButton\r\n    android:id=\"@+id\/simpleImageButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    <strong>android:src=\"@drawable\/home\"<\/strong>\/&gt; &lt;!--src(source)file from drawable folder which display an imagebutton--&gt;<\/pre>\n<p><strong>Setting Image Source In ImageButton Using\u00a0Java class:<\/strong><\/p>\n<p>We can also set the source image at run time programmatically in java class. For that we use setImageResource() function as shown in below example code.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nImageButton simpleImageButton = (ImageButton)findViewById(R.id.<em>simpleImageButton<\/em>);\r\nsimpleImageButton.setImageResource(R.drawable.<em>home<\/em>); \/\/set the image programmatically\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. background:<\/strong><\/span> background attribute is used to set the background of an image button. We can set a color or a drawable in the background of a Button.<\/p>\n<p>Below is the example code in which we set the black color for the background and an home image as the source of the image button.<\/p>\n<pre>&lt;ImageButton\r\n    android:id=\"@+id\/simpleImageButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:src=\"@drawable\/home\"\r\n    android:background=\"#000\"\/&gt;&lt;!-- black background color for image button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-845\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Background-in-ImageButton.jpg\" alt=\"Background in ImageButton\" width=\"224\" height=\"179\" \/><\/center><strong>Setting Background In ImageButton Using\u00a0Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the black background color of a image button programmatically means in java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nImageButton simpleImageButton = (ImageButton) findViewById(R.id.<em>simpleImageButton<\/em>);\r\nsimpleImageButton.setBackgroundColor(Color.<em>BLACK<\/em>); \/\/set black background color for image button\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. padding:<\/strong><\/span> padding attribute is used to set the padding from left, right, top or bottom of the ImageButton.<\/p>\n<ul>\n<li><strong>paddingRight :<\/strong> set the padding from the right side of the image button<strong>.<\/strong><\/li>\n<li><strong>paddingLeft :<\/strong> set the padding from the left side of the image button<strong>.<\/strong><\/li>\n<li><strong>paddingTop :<\/strong> set the padding from the top side of the image button<strong>.<\/strong><\/li>\n<li><strong>paddingBottom :<\/strong> set the padding from the bottom side of the image button<strong>.<\/strong><\/li>\n<li><strong>padding :<\/strong> set the padding from the all side\u2019s of the image button<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below is the example code of padding attribute in which we set the 20dp padding from all the side\u2019s of a image button.<\/p>\n<pre>&lt;ImageButton\r\n    android:id=\"@+id\/simpleImageButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:background=\"#000\"\r\n    android:src=\"@drawable\/home\"\r\n    android:padding=\"30dp\"\/&gt;&lt;!-- set 30dp padding from all the sides of the view--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-846\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-ImageButon.jpg\" alt=\"Padding in ImageButon\" width=\"218\" height=\"232\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example of ImageButton In Android Studio:<\/strong><\/h4>\n<p>In the below example of ImageButton\u00a0we display two custom image buttons with source and background. One is simple image button with simple background and other one is a round corner image button and whenever you click on an button, the name of the button will be displayed in a toast.\u00a0Below is the code and final output:<br \/>\n[cp_modal id=&#8221;cp_id_2ba06&#8243;]<img loading=\"lazy\" decoding=\"async\" src=\"\/ui\/wp-content\/uploads\/2016\/08\/Download-Code1.png\" alt=\"Download Code\" width=\"439\" height=\"65\" class=\"aligncenter size-full wp-image-2150\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/08\/Download-Code1.png 439w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/08\/Download-Code1-300x44.png 300w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/>[\/cp_modal]<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-848\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-Example-In-Android-Studio.jpg\" alt=\"ImageButton Example In Android Studio\" width=\"257\" height=\"423\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-Example-In-Android-Studio.jpg 257w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-Example-In-Android-Studio-182x300.jpg 182w\" sizes=\"auto, (max-width: 257px) 100vw, 257px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ImageButtonExample<\/p>\n<p>In this step we create a new project in android studio by filling all the necessary\u00a0details of the app like app name, package name, api versions etc.<\/p>\n<pre>Select File -&gt; New -&gt; New Project and Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Right click on\u00a0<strong>drawable<\/strong>\u00a0-&gt; New -&gt;\u00a0Drawable resource file and create new xml file name\u00a0<strong>custom_image-buttton.xml\u00a0<\/strong>and add following code<\/p>\n<p>In this Step we create drawable xml in which we used solid and corner properties, solid is used to set the background color for the image button and corner is used to set the radius for button corners.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;shape xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\r\n    &lt;solid android:color=\"#900\" \/&gt;&lt;!-- background color for imagebutton--&gt;\r\n    &lt;corners android:radius=\"20dp\" \/&gt;&lt;!-- round corners for imagebutton--&gt;\r\n&lt;\/shape&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open app\u00a0-&gt; layout -&gt;\u00a0<strong>activity_main.<\/strong><strong>xml (or) main.xml<\/strong>\u00a0and add following code:<\/p>\n<p>In this step, we open an xml file and add the code\u00a0which display\u00a0two custom image buttons by using src, background, gravity and other attributes in Relative Layout.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n&lt;!--Make sure to save two images home and youtube in drawable folder--&gt;\r\n\r\n    &lt;ImageButton\r\n        android:id=\"@+id\/simpleImageButtonHome\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:background=\"@drawable\/custom_image_button\"\r\n        android:padding=\"20dp\"\r\n        android:src=\"@drawable\/home\" \/&gt;\r\n\r\n    &lt;ImageButton\r\n        android:id=\"@+id\/simpleImageButtonYouTube\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/simpleImageButtonHome\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:background=\"#005\"\r\n        android:padding=\"20dp\"\r\n        android:src=\"@drawable\/youtube\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Open app\u00a0-&gt; package -&gt; <strong>MainActivity.<\/strong><strong>java<\/strong><\/p>\n<p>In this step, we add the code to\u00a0initiate the image button\u2019s and then perform click event on them and display the text for selected item using a toast.<\/p>\n<pre>package example.abhiandriod.imagebuttonexample;\r\n\r\nimport android.app.Activity;\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.ImageButton;\r\nimport android.widget.Toast;\r\n\r\n\r\npublic class MainActivity extends Activity {\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \r\n\/\/ initiate view's\r\n        ImageButton simpleImageButtonHome = (ImageButton)findViewById(R.id.simpleImageButtonHome);\r\n        ImageButton simpleImageButtonYouTube = (ImageButton)findViewById(R.id.simpleImageButtonYouTube);\r\n        \r\n\/\/ perform click event on button's\r\n        simpleImageButtonHome.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                Toast.makeText(getApplicationContext(),\"Home Button\",Toast.LENGTH_LONG).show();\/\/ display the toast on home button click\r\n            }\r\n        });\r\n        simpleImageButtonYouTube.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                Toast.makeText(getApplicationContext(),\"YouTube Button\",Toast.LENGTH_LONG).show();\/\/ display the toast on you tube button click\r\n            }\r\n        });\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>\u00a0Output:<\/strong><\/span><\/p>\n<p>Now start the AVD in Emulator and run the App. You will see two ImageButton out of which top one is round corner. Click on\u00a0any image and its name will be displayed on screen.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-850\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-Example-Output-in-Android.jpg\" alt=\"ImageButton Example Output in Android\" width=\"256\" height=\"442\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-Example-Output-in-Android.jpg 256w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageButton-Example-Output-in-Android-174x300.jpg 174w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, ImageButton is used to display a normal button with a custom image in a button. In simple words we can say, ImageButton is a button with an image that can be pressed or clicked by the users. By default it looks like a normal button with the standard button background that changes the &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/imagebutton\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ImageButton Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-841","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ImageButton Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn the concept of ImageButton and its attributes with example and code in Android Studio. Also one example with round corner Image Button.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/imagebutton\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=841"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/841\/revisions"}],"predecessor-version":[{"id":2630,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/841\/revisions\/2630"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}