

Step 1: Create a new project in Android Studio and name it ButtonExample. In the same way you can do for other two attribute by your own:īelow is the example of button in which we display two buttons with different background and whenever a user click on the button the text of the button will be displayed in a toast. In the Below example we set the icon to the right of the text.

drawableTop, drawableRight And drawableLeft: Just like the above attribute we can draw drawable to the left, right or top of text. Make sure you have image saved in your drawable folder name ic_launcher.
Button toast android studio code#
drawableBottom: drawableBottom is the drawable to be drawn to the below of the text.īelow is the example code in which we set the icon to the below of the text.

In above example code of background we also set the 10dp padding from all the side’s of button.ĩ. padding: padding attribute is used to set the padding from left, right, top or bottom. BLACK) //set the black color of button backgroundĨ. Button simpleButton=(Button)findViewById(R.id. Setting background in Button In Java class:īelow is the example code in which we set the background color of a Button programmatically means in java class. We can set a color or a drawable in the background of a Button.īelow is the example code in which we set the gren color for the background, Black color for the displayed text and set 15dp padding from all the side’s for Button. background: background attribute is used to set the background of a Button. If we need to use two or more styles for a Button then “|” operator is used for that.īelow is the example code with explanation included, in which we set the bold and italic text styles for text of a button. The possible text styles are bold, italic and normal. textStyle: textStyle attribute is used to set the text style of a Button. tTextSize(25) //set the text size of buttonĦ.

īelow is the example code in which we set the text size of a Button programmatically means in java class. We can set the text size in sp(scale independent pixel) or dp(density pixel).īelow is the example code in which we set the 25sp size for the text of a Button. textSize: textSize attribute is used to set the size of the text on Button. Button simpleButton=(Button) findViewById(R.id. Setting Text Color On Button Inside Java class:īelow is the example code in which we set the text color of a Button programmatically means in java class. Color value is in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.īelow is the example code with explanation included in which we set the red color for the displayed text of a Button. Button button = (Button) findViewById(R.id.simpleButton) ītText("Learn Android AbhiAndroid") //set the text on buttonĤ.textColor: textColor attribute is used to set the text color of a Button. īelow is the example code in which we set the text on Button programmatically means in java class. We can set the text in xml as well as in the java class.īelow is the example code with explanation included in which we set the text “Learning Android AbhiAndroid” in a Button. text: text attribute is used to set the text in a Button. 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.īelow is the example code with explanation included in which we set the right and center vertical gravity for text of a Button. The below code will create Button and write “Abhi Android” text on it. Android buttons with images on are also called ImageButton. A button with images on can contain both an image and a text. These buttons can be divided into two categories: the first is Buttons with text on, and second is buttons with an image on. When the user taps/clicks on button in an Android app, the app can respond to the click/tap. On a button we can perform different actions or events like click event, pressed event, touch event etc.Īndroid buttons are GUI components which are sensible to taps (clicks) by the user. There are different types of buttons used in android such as CompoundButton, ToggleButton, RadioButton.īutton is a subclass of TextView class and compound button is the subclass of Button class. A Push buttons can be clicked, or pressed by the user to perform an action. In Android, Button represents a push button. Button Tutorial With Examples In Android Studio
