Android Button Control

Android Button Control

Android Button control represents a push-button that can be pressed, or clicked to perform an action. Generally, while using an app, we see many buttons and being a user to initiate an action, we click one at a time.

Usage

Button can be an OK, Submit, or any other that initiates an action. For example, in the below figure 4 buttons are visible.

Create any number of buttons and initiate a listener, so that you can initiate an event on button click,

Android Button Control

Example

Here’s a simple example showing the working on Android Button on Android Studio. Here, we will be showing a message on button click.

If you’re beginning with Android, then learn how to run your first Android app in Android Studio.

Let’s start with the usage of Android Button control.

MainActivity.java

The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here we will set the View to be displayed with the setContentView.

On button click, a toast will appear with a message “Studyopedia is a free learning website!”. Toast in Android is a notification message that is visible in the form of popup.

activity_main.xml

The activity file is located under the following directory: src\main\res\layout\activity_main.xml.
We’ll add our UI content here, 1 TextView and 1 Button,

AndroidManifest.xml

The content of \src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,

strings.xml

Here’s the code for res/values/strings.xml. The app name and any hardcoded strings will be visible.

We hardcoded string “Studyopedia Demo App” and used @string/title in activity_main.xml. Here, it gets mentioned under <resources>,

Output

We created an AVD, so now we will run our app using the same AVD. Go to Run > Run app or press the run button or Shift + F10. This will install your app on the AVD and the following emulator with your app opens up,

Android Button Control Output1

On button click, a toast appears with the message,

Android Button Control Output2

Android TextView Control
Android ImageButton Control
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create free tutorials for all.

No Comments

Post A Comment