Develop Android Apps, how to make android app buttons

New Tutorial 2022


View All Tutorials


Android - Button Types

Added March 2022

For this tutorial, we are going to learn how to code different button types. These are the basic button types which can be quite useful when developing your apps. In Android coding, they would be considered standard buttons which means they work across all Android platform without the requirement of specialty support code being added. Most code for these is included in xml pages, so quite easy to code.

We are going to create a new app for this tutorial, and name is - Buttons App

Android Tutorial, Android Button Types App


Creating Our New App

Open AIDE, Select New Project, from the left menu, or center menu hierachy of files; and do the following:

Select
Create an Android app, choose java xml programming language

choose NEW ANDROID APP(Gradle/Android SDK/java/xml) if your using the newest version of AIDE.

Choose HELLO WORLD example app template(java/xml);if your using an older version of AIDE,
For app name; type 'ButtonsApp'

For app package name type: com.aac.button_examples_app

Click Create

Your new app is now created for you - named 'ButtonsApp'. For now, you will have the little green Android image as your app icon. Once you add your apps' image(s) and run your app with the updated and new pages you create, you'll see the "Image of Buttons' instead.

AIDE automatically opens your new app in the editor for you.

Where Code is Coded

For this Buttons App; we are creating 6 buttons; 1 basic(no border or color), 1 bordered button(has border and gray color),1 two color gradient button, 1 three color gradient button, 1 custom state button, and 1 rounded shape colored button.
The first two buttons are simple system generated buttons. The buttons with gradient color added are more of a custom styled. The custom states button is one where the color of the button itself changes depending on whether it is pressed, focused or just in a non state, meaning a user is not clicking or has not already pressed the button.
The only difference amongst all these button types is the amount of code you need to include and what new pages, if any, have to be created.
As with all our tutorials, the code you need to create for your app is at the 'Example Code' section on this page.

All 6 buttons, are coded on the main.xml page. This page provides the layout for our button designs, also known as the homepage of your app. Typically, users can select a button which takes them to another page in the app or might just show another action like displaying a message. This is what each of our buttons does when clicked/selected; it shows them a message.

In Android, these types of 'displayed messages' are called 'Toast Messages'. These Toast Messages are of two types, long and short; and these simply refer to the amount of time the message displays- usually 1 to 5 seconds. Because Toast Messages are created from a user interaction, they require a 'onClickListener', in order to function. These 'onClickListeners' are coded in the java class of your app. For our tutorial, the java class is MainActivity.java. Since all the buttons display a Toast Message, we must add the code for the onClickListener to each button in the MainActivity.java of our app.

System Default Buttons

The first two buttons we added are the simplest to code. We only need add code at the main.xml page. Button one has a basic system default code. Note that we added style attribute to make our button borderless.

Button two is the same code as button one, but the button is not borderless and it has the system default look with the light shade of grey.

Color Gradient Buttons

Button three is the 2 color gradient button. For this button we have to create a xml and add the colors we want to it. Then, we link to this page from the button code at main.xml, by using the android;background attribute.
Also before we create each xml page, we have to create the 'drawable" folder at res/drawable of our app project. In this folder we put all the xml pages we created for button 3, button 4, and button 5, and button 6.
This image shows the created pages at res/drawable.

Android Tutorial, Android Buttons Xml Create

This image shows the xml for button 3. gradient_1.xml

Creating a gradient button in Android

This image shows the xml for button 4. gradient_2.xml
Android Tutorial, gradient buttons

Custom State Buttons

This image shows the xml code for button 5 - "custom button states'. custom_button_states.xml

How to make an android button with custom states The custom states button has 3 colors, and it requires a xml page for the custom states- including default, pressed and focused states. This page(custom_button_states.xml) links to each state xml page, and those xml pages must be created. So the custom states button requires four xml pages to be created: custom_button_states.xml
button_default.xml
button_pressed.xml
button_focused.xml

Each page; has 2 color choices added. We don't have to link to all four pages at main.xml, only the custom states xml (custom_button_states.xml); and we link it using the android background attribute like so;

android:background=
"@drawable/custom_button_states


Because this xml page is linked to the other 3 pages(focused, pressed, default), it will get all the colors for each button state when required.

Button six is a shaped button and we add the shape we want to the 'shape' attribute in the code. We used the 'rectangle' which gives the button the rounded look.

TIPS

Navigating The App Project Hierachy

To view A FILE do either: from right menu choose FILES or GOTO, this will show the apps files along the top of editor, dropdown, and left side menu. After editing a file, SAVE it, right side menu, choose SAVE. You can also Turn On AutoSave feature in settings.

Creating Pages

Check for the filename extensions of page your creating. If for example, your creating a new Java page, (goto java folder)you want to see pages already created in that folder that have the .java file extension. Click 'create new java class' to create your page.

For xml files, (like main.xml), you must create these in the 'res/layout' folder of your app project. Click 'add new xml file', to create your page.
For xml pages for colors you create,.like our Buttons App; those must be created at res/drawable.
When creating your java pages, leave no space of words.
FirstScreen.java

For xml pages, you can have space but use _ to separate words. And, use lowercase. Same applies to image names;.use lowercase and _ for spacing.

Updating Pages - Code From Our Example Code

Because we create our App using a template, all app pages are created for us. That way; we only have to edit the code on pages we have to then Save the page(s) and Run the app again. If you have to Create a new page; same thing, replace all code with code from the tutorial Example Code and Save page(s), then Run the app.

Additional Reading on Buttons at Android Developer Website
Coding Buttons




In Summary,
App Name is 'ButtonsApp' - located at AppProjects/ButtonsApp in AIDE; OPEN the AIDE app, then open App Projects, THEN select the '', app from your files hierachy(left menu); then DOUBLE CLICK on any file from the app, like strings.xml, to open it in AIDE.

Make sure at top of page, the app name 'ButtonsApp' is there next to AppProjects like so: AppProjects/ButtonsApp then you know you have the proper App.

Once a file is in the editor, you can edit, save it, then RUN your APP, install, to Open your app.

You can open the app Simply by Clicking on the Android little green App Icon,with the wording 'ButtonsApp'(ButtonsApp Screenshot Image) on your tablet/smartphone. It will be with your other installed apps.

If you make further changes to this App, you need to SAVE, RUN, UPDATE, INSTALL, OPEN the app again.
EXAMPLE CODE - Code For This Tutorial.

JUST copy the code for this tutorial from the Example Code in this section. Just replace this code with the code already on the page and Save the page.

Example Code:
Copy Paste the Code

SAVE IMAGE(S): Right Click and Save As
app_buttons480.png
button types android tutorial

This free script provided by
JavaScript Kit

Share This Page


AIDE for creating Android Apps on your tablet or cell phone(android).
Free and Paid.
GoTo Aide

ANDROID STUDIO - for creating Android Apps on your desktop computer.
Free to Install and Use
Learn more - Android Studio

The Java XML files we code(example codes) and the Android Apps we create in our Free Tutorials are compatible with Android Studio and AIDE. Just choose Java XML as your Programming Language.


You May Like:
Make Sushi Rolls Taste World Wines Flavours and Aromas


AndroidAppCoding.com, All Rights Reserved