Prerequisite

If you not understand this lecture . android Buttonyou will also read my previous lectures.  Open new Activity on button click  and Layouts in android studio.After read these links you will also able to create button and handling click events .

Overview

A button is a physical switch that controls a function of a device. A button is used to perform a certain function. For example, Windows is used button to open the Start menu. Button consist of image and icons.When user click on button action occur depending on whether you want a button.

Introduction

In this lecture will we describe how can create different buttons .  we will describe  different types of buttons and also describes how can handled click events of button in android studio .  After Reading this lecture you will understand  Buttons and its click events I hope you will create Button and handled click event after reading this lecture.

Description of Button

button is used to perform a certain function.when button can be pressed, or clicked, by the user to perform an action. In this lecture we will describe how can create button in  android studio.First we create  button that contain only text after this step we create  second  button that contain only image. And after this step  we create a  third button that contain both image and text.for more detail about  button click on Android button .Now you will create a new project in android studio.Every project contain two file first is java class file and second is layout XML file your first step is open a layout XML file . and create a  button that contain only text. So we add a button  and set the height and width wrap_content. Wrap_content means height and width similar to the text .After this step you will  also set the text of  button . and Add this text into string resource, So you will find out the value folder expand this value folder and click on string .XML and add string resource. and look the design.

Alt Tag simple button image

Now we perform second task  button that contain only image.So we need to add a image into drawable folder. so copy a image and paste into drawable folder. show in below pic

Step 1=image paste into drawable->right click-> into drawable folder select option->paste-> and click.

Alt Tag paste image into drawable

Step2=Step1-> Write name of image->ok

Alt Tag  image name add

then image will  be add into drawable folder show in below pic.

Alt Tag add image into drawable folder

After this step add a image button and set the height and width  wrap_content. and add a image using this property(android src =”@drawable/sixlayout2″) and look the design.

Alt Tag simple button and image button

Now we perform third category , button text and image Add new button and set the height and width wrap_content wrap_content means height and width similar to the text of button.So we include a property (android:drawableLeft=”@drawable/sixlayout2″)Purpose of include this property  image is  left side of button And paste the following code.

XML Layout

In this example we are going to create a simple UI that includes only (Button) with Different attributes.

Different property that are used in layout.xml file  explain here

  • warp_content       warp_content means height and width similar to the text that are entered.
  • Match_parent       means height and width similar to parent.
  • Android text property    show text message in output screen.
  •  Android on Click  when user click on a button this method execute.
  • <image view>        tag is used for image button.

 Note : First of all you will remove these these line of code in layout XML .

After delete the above code our code is look like this.In this example we are going to create a simple UI that includes

Now you may open the Graphical layout editor to preview the User Interface you created.

Alt Tag button contain image and text

Response to click events

We will also describe how to create a  button response on click events. we will describe two method . First method is specify the on click method. That are define in XML file .And second method is the on click listener.So i will describes two method response on click events . First of all you will create a new project in android studio.Every project contain two file first is java class file and second is layout XML file So, your first step is open a layout XML file . and create a  button and set the height and width wrap_content. Wrap_content means height and width similar to the text. and also include a on click method and specify define the method name. when user click on button method is execute .Next step you will open a main activity and define that method into main activity.Now i am going to add another button in which perform second method …. on click list-near . for more information about response on click events please click Response on click event     After this step you will  also set the text of  button . and Add this text into string resource, So you will find out the value folder expand this value folder and click on string .XML and add string resource. and look the design.

In this example we are going to create a simple UI that includes only (Button and its click events) with Different attributes.And paste the following code into layout .XML file:

Now you may open the Graphical layout editor to preview the User Interface you created.

Alt Tag button image

In MainActivity two method will describe first is android: onclick which consists three elements void return type Be public and view parameter.and name of method is same as define in layout.XML file.

OnClickListener

OnclickListener is define in only java.class file not declare in XML layout. this method also perform same functionality as compare onclick.

Code that are placed in java class file Main Activity.java

 Run the application:

Alt Tag buttons output

Output if you click in first button this message will show

Alt Tag image if click on first button

Output if you click in second button this message will show

Alt Tag image if click on secondbutton

 

“Thank you”  for reading this lecture, Hope you got the idea.