User interface classes
A user interface (UI) class represents the visual elements and layout of an app's user interface. It defines how different views and components are organized and interact with each other. The most commonly used UI class in Android is the Activity
class, which represents a single screen with a user interface.
Example of a basic Activity
class in Android:
1. Activity
Mainactivity.java
activity_main.xml
In this example, we have a MainActivity
class that extends the Activity
class. It represents the main screen of the app.
Inside the onCreate
method, we perform the following tasks:
Call the superclass
onCreate
method usingsuper.onCreate(savedInstanceState)
to ensure the base class initialization is done.Set the content view of the activity using
setContentView(R.layout.activity_main)
. This associates the layout fileactivity_main.xml
with theMainActivity
class.Retrieve a reference to the button widget defined in the layout using
findViewById(R.id.my_button)
. TheR.id.my_button
is the unique identifier assigned to the button in the XML layout.Set a click listener on the button using
myButton.setOnClickListener(...)
. Here, we use a lambda expression to define the behavior when the button is clicked.Inside the click listener, we create a
Toast
message usingToast.makeText(...)
. It displays a short-lived pop-up message saying "Button Clicked".Finally, we call
show()
on theToast
object to display the message.
Remember to define the layout XML file (activity_main.xml
in this example) to define the UI elements, such as buttons, text views, and layouts, and their properties.
This is just a basic example, and there are many other UI classes and concepts in Android development, such as fragments, custom views, and layouts. But the Activity
class is a fundamental UI class that represents a screen in an Android app.
2. Fragment
Fragments represent reusable portions of a user interface within an Activity
. They allow you to create flexible and modular UI components that can be combined and reused across different screens. Fragments have their lifecycle and can be added, removed, and replaced dynamically within an Activity
. for example:
MyFragment.java
fragment_layout.xml
3. DialogFragment
DialogFragments are specialized fragments that can be used to display dialog windows on top of an Activity
. They provide a convenient way to present dialogs with custom layouts and behavior. for example:
4. View
Button
: Represents a clickable button.TextView
: Displays text on the screen.EditText
: Provides an interactive text input field.ImageView
: Displays images on the screen.CheckBox
: Represents a selectable option that can be checked or unchecked.RadioButton
: Represents a selectable option within a group.SeekBar
: Allows the user to select a value within a specified range.ProgressBar
: Displays the progress of an ongoing operation.Switch
: Represents a toggle switch that can be turned on or off.Many more specialized views for specific purposes.
Views are the basic building blocks of the UI, representing individual UI components such as buttons, text fields, or images. They are the visual elements that make up the user interface of an Android app.
5. ViewGroup
LinearLayout
: Arranges child views linearly either horizontally or vertically.RelativeLayout
: Arranges child views relative to one another or to the parent.FrameLayout
: Places child views on top of each other, with the last one added being the topmost.ConstraintLayout
: Provides flexible positioning and sizing of views with constraints.TableLayout
: Organizes child views in rows and columns.GridLayout
: Places child views in a grid of specified rows and columns.ScrollView
: Allows scrolling of child views when the content exceeds the available screen space.CardView
: Provides a container for displaying content with a consistent appearance and elevation.Many more layout managers and containers for organizing and managing views.
ViewGroup classes can contain and manage multiple child views and determine their position, size, and interaction behavior within the layout.
6. Custom
You can create custom UI classes in Android by extending existing classes or implementing custom views. Example of creating a custom Button
class:
In this example, we create a custom Button
class called CustomButton
by extending the AppCompatButton
class. We override the onDraw
method to perform custom drawing operations on the canvas.
Inside the init
method, we initialize a Paint
object and customize it as needed. In this example, we set the color to red.
In the onDraw
method, we perform custom drawing operations using the Canvas
object. Here, we draw a red rectangle that covers the entire button area using the drawRect
method. After that, we call super.onDraw(canvas)
to let the superclass handle the default button drawing, ensuring that the button text and other visual states are properly rendered.
Once you have created the custom Button
class, you can use it in your layout XML files just like any other view:
By creating custom UI classes, you have full control over the appearance and behavior of the views, allowing you to create unique and personalized user interfaces for your Android app.
Last updated
Was this helpful?