Are you new to the Android Programming world ? Or are you looking to complete that one assignment from your college. Regardless of your circumstance let us learn what an Android View is.
I have specially structured this blog post as a friendly introduction to android views. Let us move forward with a simple introduction. This article provides you with enough details to begin your research about the topic.
What are Android Views ?
Views are the base of any UI component in Android.
Are you familiar with the
Object class in
Java ? If you are then,
View is the top level UI component class in Android SDK. Just like the
Object class, which is the root of every class in Java.
Every UI component you see is built extending the
View class. Each and every
EditText, etc. extends the
View class. Also the view holders / containers extend the
ViewGroup which itself extends the
View class has all the necessary implementation to handle the given input. That is click listeners or input listener everything else is being handled by the sub class of
Is there anything more ? No they are just the class that performs canvas drawing in a specified way. And we can use it to create beautiful UI.
How to use Android View ?
Now that we have understood what an Android View is. Let us quickly see how we can use the Views in our Application.
There are two specific ways we can use the Views. What are they ?
- Inflating the XML layout
- Creating the view with code and adding it
Let us explore each way we can set the views in our layout.
Inflating XML Layout
We can create new views by using the
LayoutInflater and the layout file example: R.layout.my_layout.
Here is an example XML layout using the View.
Code language: HTML, XML (xml)
<LinearLayout> <!-- your layout declaration goes here --> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Click me!" /> </LinearLayout>
And to inflate this view we will do something like this in our activity.
Code language: Java (java)
LayoutInflater inflater = LayoutInflater.from(this) // this is the activity or context View view = inflater.inflate(R.layout.my_layout, null, false);
Now we can add the
view to our preferred layout. This is almost what the
setContentView() method of
Creating View Instance
Or we can just create an instance of any given view just by passing in the context. See the example below:
EditText editText = new EditText(this); // this - activity context or activity someLayout.add(editText); // some layout is the view group implementation
It is really that simple. We use this method to create views if we want dynamic addition and deletion of the
View in our UI.
Also Read: Splash Screen Tutorial using Android StudioTECHENUM
Attributes of Android View
Here are a few attributes which you must compulsorily know to use the views. Each view must have these attributes for the application to compile.
The android:id=”” helps to identify the view uniquely. We will be adding the id as string in the XML file. The system will compile and create a
R class containing all the mappings.
layout_height & layout_width
The android:layout_height=”” and android:layout_width=”” are the compulsory attributes. They will tell the
ViewGroup about the size adjustment.
The visibility attribute tells the
ViewGroup to either show or hide the view. There are three possible values of the attribute: visible invisible gone
How to customize Android View ?
Here I will briefly introduce to the
View customization in Android.
To do a custom view you need to extend the
View class. And then override certain methods in order to create new Views.
Though you might not need to do this very often. If you want to create really unique components then you might want to create custom views.
A simple example of creating custom view:
Where to go from here ?
The next step I suggest for learning is the official android documentation.
There are numerous articles describing about android views. How to use them and what are the specific use cases.