HOW TO ADD IMAGE BUTTON IN ANDROID STUDIO - Andropro

Andropro

androidtutorialro

Breaking

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Sunday, July 1, 2018

HOW TO ADD IMAGE BUTTON IN ANDROID STUDIO

ADDING AN IMAGE BUTTON TO AN ANDROID STUDIO:

This article will help you how to add imagebutton to android studio,when an image is clicked then the particular action can be initiates for example when an icon is clicked it will do some specific tasks
follow the android tutorial to add an image button to android studio


STEP TO ADD AN IMAGE BUTTON TO ANDROID STUDIO:

<ImageButton   

 android:layout_width="wrap_content"   

 android:layout_height="wrap_content" 
   
android:id="@+id/imageButton" 
   
android:layout_centerVertical="true"
    
android:layout_centerHorizontal="true" 
 
  android:src="@drawable/jas"  
  
tools:ignore="ContentDescription"/>

FOLLOWING ARE THE ATTRIBUTES OF IMAGE BUTTON:



1. id: 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.


EX: android:id="@+id/imageButton"
2. src: 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.make sure you added an image to an DRAWABLE FOLDER, in this below example jas is the name of an image stored in drawable folder and it will load the image

EX:android:src="@drawable/jas"
You can add image at the runtime of an java program by placing these codes
 /*Add in Oncreate() funtion after setContentView()*/
ImageButton simpleImageButton = (ImageButton)findViewById(R.id.simpleImageButton);
simpleImageButton
.setImageResource(R.drawable.home);
//set the image name correctly
3. background: 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.
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.

EX:android:background="#000" you can set the background color ath the java program by adding these lines
/*Add in Oncreate() funtion after setContentView()*/
ImageButton simpleImageButton = (ImageButton) findViewById(R.id.simpleImageButton);
simpleImageButton
.setBackgroundColor(Color.BLACK); //set black background color for image button
4. padding: padding attribute is used to set the padding from left, right, top or bottom of the ImageButton.
  • paddingRight : set the padding from the right side of the image button.
  • paddingLeft : set the padding from the left side of the image button.
  • paddingTop : set the padding from the top side of the image button.
  • paddingBottom : set the padding from the bottom side of the image button.
  • padding : set the padding from the all side’s of the image button.
EX:android:padding="30dp"
SAMPLE CODING: DOWNLOAD SOURCE CODE
ACTIVITY_MAN.XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 xmlns:tools="http://schemas.android.com/tools" 

android:layout_width="match_parent"

android:layout_height="match_parent"

 tools:context=".MainActivity">

<TextView android:layout_width="wrap_content"

 android:layout_height="wrap_content" 

android:layout_alignParentTop="true" 

android:layout_centerHorizontal="true" 

android:layout_marginTop="26dp" 

android:text="ASTIN SALVI"

 android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

<ImageButton android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:id="@+id/imageButton"

 android:layout_centerVertical="true"

 android:layout_centerHorizontal="true" 

android:src="@drawable/jas" 

tools:ignore="ContentDescription"/>

</RelativeLayout>









MAINACTIVITY.JAVA:




public class MainActivity extends AppCompatActivity {

    ImageButton imgButton;


            @Override            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                imgButton =(ImageButton)findViewById(R.id.imageButton);
                imgButton.setOnClickListener(new View.OnClickListener() {
                    @Override                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(),"astin salvi",Toast.LENGTH_LONG).show();
                    }
                });
            }
}





When you run the above coding , you will get the following output





OUTPUT:


















































OUTPUT:










Post Bottom Ad

Responsive Ads Here

Pages