0% found this document useful (0 votes)
14 views16 pages

4.3 Button

The document provides an overview of the Button and ImageButton classes in Android, detailing their attributes and how to implement them in XML and Java. It covers various attributes such as id, gravity, text, textColor, textSize, textStyle, background, padding, and drawable positioning. Additionally, it explains how to define click events for buttons programmatically.

Uploaded by

Rajwardhan Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

4.3 Button

The document provides an overview of the Button and ImageButton classes in Android, detailing their attributes and how to implement them in XML and Java. It covers various attributes such as id, gravity, text, textColor, textSize, textStyle, background, padding, and drawable positioning. Additionally, it explains how to define click events for buttons programmatically.

Uploaded by

Rajwardhan Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Button

-Android Button represents a push-button.


-The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class.
-There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc.
Button code in XML:
<Button
android:id="@+id/simpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abhi Android"/>
Attributes of Button
1. id: id is an attribute used to uniquely identify a text Button.
Example- <Button
android:id="@+id/simpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abhi Android"/>
2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of the text like left, right,
center, top, bottom, center_vertical, center_horizontal etc.
Example-
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Abhi Android"
android:layout_centerInParent="true"
android:gravity="right|center_vertical"/><!--set the gravity of button-->
3. text: text attribute is used to set the text in a Button. We can set the text in xml as well as in the java class.
Example-
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Learn Android @ AbhiAndroid"/><!--display text on button-->

Setting Text Using Java class:

Button button = (Button) findViewById(R.id.simpleButton);


button.setText("Learn Android @ AbhiAndroid");//set the text on button
4.textColor: textColor attribute is used to set the text color of a Button. Color value is in the form of “#argb”, “#rgb”,
“#rrggbb”, or “#aarrggbb”.
Example-
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="AbhiAndroid"
android:textColor="#f00"/><!--red color for the text-->

Setting Text Color On Button Inside Java class:

Button simpleButton=(Button) findViewById(R.id.simpleButton);


simpleButton.setTextColor(Color.RED);//set the red color for the text
5. textSize: textSize attribute is used to set the size of the text on Button. We can set the text size in sp(scale independent
pixel) or dp(density pixel).
Example-
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="AbhiAndroid"
android:textSize="25sp" /><!--25sp text size-->
Setting Text Color On Button Inside Java class:

Button simpleButton=(Button)findViewById(R.id.simpleButton);
simpleButton.setTextSize(25);//set the text size of button
6. textStyle: textStyle attribute is used to set the text style of a Button. The possible text styles are bold, italic and normal. If
we need to use two or more styles for a Button then “|” operator is used for that.
Example-
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="AbhiAndroid"
android:textSize="20sp"
android:textStyle="bold|italic"/><!--bold and italic text style-->
7. background: background attribute is used to set the background of a Button. We can set a color or a drawable in the
background of a Button.
8. padding: padding attribute is used to set the padding from left, right, top or bottom. In above example code of background
we also set the 10dp padding from all the side’s of button.
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Download"
android:textSize="20sp"
android:padding="15dp"
android:textStyle="bold|italic"
android:background="#147D03" /><!--Background green color-->
9. drawableBottom: drawableBottom is the drawable to be drawn to the below of the text.
Example-
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#147D03"
android:text="Download Code"
android:textSize="20sp"
android:padding="15dp"
android:textStyle="bold|italic"
android:drawableBottom="@drawable/ic_launcher"/><!--image drawable on button-->
10. drawableTop, drawableRight And drawableLeft: Just like the above attribute we can draw drawable to the left, right or
top of text.
Example-
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#147D03"
android:text="Download Code"
android:textSize="20sp"
android:padding="15dp"
android:textStyle="bold|italic"
android:drawableRight="@drawable/ic_launcher"/><!--image drawable on Right side of Text on button-->
Define Button Click Event in Activity File
-To define button click programmatically, create View.OnClickListener object and assign it to the button by
calling setOnClickListener(View.OnClickListener) like as shown below.
Button btnAdd = (Button)findViewById(R.id.addBtn);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Do something in response to button click
}
});
}
Example-
ImageButton- In Android, ImageButton is used to display a normal button with a custom image in a button.

Example-
!--Make Sure To Add Image Name home in Drawable Folder-->
<ImageButton
android:id="@+id/simpleImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home" />
Attributes of ImageButton:
1. id: id is an attribute used to uniquely identify a image button.
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.
Example-
<ImageButton
android:id="@+id/simpleImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home"/> <!--src(source)file from drawable folder which display an
imagebutton-->
Setting Image Source In ImageButton Using Java class:
/*Add in Oncreate() funtion after setContentView()*/
ImageButton simpleImageButton = (ImageButton)findViewById(R.id.simpleImageButton);
simpleImageButton.setImageResource(R.drawable.home); //set the image programmatically
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.
Example-
<ImageButton
android:id="@+id/simpleImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home"
android:background="#000"/><!-- black background color for image button-->
Setting Background In ImageButton Using Java class:

/*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.
Example-
<ImageButton
android:id="@+id/simpleImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/home"
android:padding="30dp"/><!-- set 30dp padding from all the sides of the view-->
Example-

You might also like