4.3 Button
4.3 Button
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: