Upload Image to Use as Button in Android Studio

Button Tutorial With Examples In Android Studio

In Android, Push represents a push button. A Button buttons can be clicked, or pressed past the user to perform an action. There are different types of buttons used in android such as CompoundButton, ToggleButton, RadioButton.

Button Example Android

Button is a subclass of TextView grade and compound button is the subclass of Button class. On a button we tin can perform different actions or events like click event, pressed event, touch event etc.

Android buttons are GUI components which are sensible to taps (clicks) past the user. When the user taps/clicks on button in an Android app, the app can respond to the click/tap. These buttons can be divided into 2 categories: the first is Buttons with text on, and second is buttons with an image on. A push with images on can contain both an image and a text. Android buttons with images on are too chosenImageButton.

Button code in XML:

The below code will create Button and write "Abhi Android" text on it.

<Button android:id="@+id/simpleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Abhi Android"/>        
Button in Android

Attributes of Button in Android:

At present let's  we discuss some important attributes that helps the states to configure a Push in your xml file (layout).

1. id: id is an aspect used to uniquely place a text Button. Beneath is the case code in which we ready the id of a Button.

<Push 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 command the alignment of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.

Below is the instance code with explanation included in which we gear up the correct and center vertical gravity for text of a Button.

<Push button android:id="@+id/simpleButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Abhi Android" android:layout_centerInParent="truthful" android:gravity="right|center_vertical"/><!--set the gravity of button-->        
Button Gravity in Android

3. text: text attribute is used to ready the text in a Push. We can set the text in xml as well as in the java class.

Below is the example lawmaking with explanation included in which nosotros prepare the text "Learning Android @ AbhiAndroid" in a Button.

<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 push-->
Setting Text on Button in Android

Setting Text Using Java class:

Below is the example code in which we set up the text on Button programmatically ways in java class. The output will be same as the above.

Push button = (Button) findViewById(R.id.simpleButton); push.setText("Learn Android @ AbhiAndroid");//set the text on push button

iv.textColor: textColor aspect is used to set the text color of a Push button. Color value is in the form of "#argb", "#rgb", "#rrggbb", or "#aarrggbb".

Below is the example code with explanation included in which we set the carmine color for the displayed text of a Button.

<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"/><!--cherry-red color for the text-->        
Setting Text Color on Button in Android

Setting Text Color On Button Inside Java grade:

Below is the example lawmaking in which we prepare the text colour of a Push programmatically ways in coffee class.

Push button simpleButton=(Push button) findViewById(R.id.simpleButton); simpleButton.setTextColor(Colour.RED);//set up the red color for the text

5. textSize: textSize attribute is used to set the size of the text on Button. Nosotros tin prepare the text size in sp(scale independent pixel) or dp(density pixel).

Beneath is the example code in which nosotros set up the 25sp size for the text of a Push.

<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 TextSize on Button in Android

Setting textSize In Java class:

Below is the example code in which we fix the text size of a Button programmatically means in coffee course.

Button simpleButton=(Button)findViewById(R.id.simpleButton); simpleButton.setTextSize(25);//set the text size of push button        

half dozen. textStyle: textStyle aspect is used to set the text style of a Push button. The possible text styles are bold, italic and normal. If we demand to utilize two or more styles for a Button and so "|" operator is used for that.

Below is the example code with caption included, in which we set the bold and italic text styles for text of a button.

<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 manner-->
Set textStyle on Button in Android

seven. background: background attribute is used to ready the background of a Push button. We tin set up a colour or a drawable in the background of a Push.

Below is the example code in which we gear up the gren color for the background, Black color for the displayed text and set 15dp padding from all the side'south for Button.

<Button android:id="@+id/simpleButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="truthful" android:text="Download" android:textSize="20sp" android:padding="15dp" android:textStyle="bold|italic" android:background="#147D03" /><!--Background greenish colour-->
setting background in Button Android

Setting background in Push In Java class:

Below is the example code in which we set the background color of a Push button programmatically means in java form.

Button simpleButton=(Button)findViewById(R.id.simpleButton); simpleButton.setBackgroundColor(Color.Black);//set the black colour of button background        

8. padding: padding attribute is used to set the padding from left, correct, tiptop or bottom. In above example code of background we also set the 10dp padding from all the side's of button.

9. drawableBottom: drawableBottom is the drawable to be drawn to the below of the text.

Below is the example code in which nosotros set the icon to the beneath of the text.

Make sure you have prototype saved in your drawable folder name ic_launcher.

<Button     android:id="@+id/simpleButton"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_centerInParent="truthful"     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 push-->
drawableBottom in Button in Android

10. drawableTop, drawableRight And drawableLeft: Just like the higher up aspect we can depict drawable to the left, right or tiptop of text.

In the Below example we set the icon to the right of the text. In the same way you can practise for other 2 aspect past your own:

<Push     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 Correct side of Text on button-->
drawableRight of Text on Button in Android

Push Example In Android Studio:

Below is the example of push button in which we display ii buttons with different background and whenever a user click on the push button the text of the button will exist displayed in a toast.

Download Code ?

Button Example in Android Studio

Step ane: Create a new project in Android Studio and proper noun it ButtonExample.

Select File -> New -> New Project and Fill the forms and click "Stop" button.        

Pace two: Now open res -> layout -> xml (or) activity_main.xml and add post-obit code. Hither we are designing the UI of two button in Relative Layout.

<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"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity">      <Button         android:id="@+id/simpleButton1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_centerHorizontal="truthful"         android:layout_marginTop="100dp"         android:background="#00f"         android:drawableRight="@drawable/ic_launcher"         android:hint="AbhiAndroid Button1"         android:padding="5dp"         android:textColorHint="#fff"         android:textSize="20sp"         android:textStyle="assuming|italic" />      <Button         android:id="@+id/simpleButton2"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:background="#f00"         android:drawableLeft="@drawable/ic_launcher"         android:hint="AbhiAndroid Button2"         android:padding="5dp"         android:textColorHint="#fff"         android:textSize="20sp"         android:textStyle="assuming|italic" />   </RelativeLayout>

Step iii: Now Open  app -> package -> MainActivity.java and the post-obit code. Hither using setOnClickListener() method on push button and using Toast we will display which button is clicked past user.

package example.abhiandriod.buttonexample;  import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast;  public class MainActivity extends AppCompatActivity {      Button simpleButton1, simpleButton2;      @Override     protected void onCreate(Parcel savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         simpleButton1 = (Button) findViewById(R.id.simpleButton1);//get id of push one         simpleButton2 = (Button) findViewById(R.id.simpleButton2);//become id of button ii          simpleButton1.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 Toast.makeText(getApplicationContext(), "Elementary Button 1", Toast.LENGTH_LONG).show();//brandish the text of button1             }         });         simpleButton2.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 Toast.makeText(getApplicationContext(), "Uncomplicated Button 2", Toast.LENGTH_LONG).show();//brandish the text of button2             }         });     }       }

Output:

At present start the AVD in Emulator and run the App. You will see two button. Click on whatever button and you lot volition see the message on screen which button is clicked.

button example output android

garciaweept1984.blogspot.com

Source: https://abhiandroid.com/ui/button

0 Response to "Upload Image to Use as Button in Android Studio"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel