Basic UI Components: Text View, Button, Radio Button , Edit Text, Image View for image, Check Box , Progress Bar, Event Handling in Android in hindi

Generally, in android the user interface of an app is made with a collection of View and ViewGroup objects.

The View is a base class for all UI components in android and it is used to create interactive UI components such as TextViewEditTextCheckboxRadio Button, etc. and it is responsible for event handling and drawing.

The ViewGroup is a subclass of View and it will act as a base class for layouts and layout parameters. The ViewGroup will provide invisible containers to hold other Views or ViewGroups and to define the layout properties.

To know more about View and ViewGroup in android applications, check this Android View and ViewGroup.

In android, we can define a UI or input controls in two ways, those are

  • Declare UI elements in XML
  • Create UI elements at runtime

The android framework will allow us to use either or both of these methods to define our application’s UI. 

Declare UI Elements in XML

In android, we can create layouts same as web pages in HTML by using default Views and ViewGroups in the XML file. The layout file must contain only one root element, which must be a View or ViewGroup object. Once we define the root element, then we can add additional layout objects or widgets as a child elements to build View hierarchy that defines our layout.

Following is the example of defining UI controls (TextViewEditTextButton) in the XML file (activity_main.xml) using LinearLayout.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=””

        android:text=”Enter Name” />
        android:text=”Get Name” />

In android, each input control is having a specific set of events and these events will be raised when the user performs particular action like, entering the text or touches the button.

Note: we need to create a user interface (UI) layout files in /res/layout project directory, then only the layout files will compile properly.

Load XML Layout File from an Activity

Once we are done with the creation of layout with UI controls, we need to load the XML layout resource from our activity onCreate() callback method like as shown below.

protected void onCreate(Bundle savedInstanceState) {

If you observe above code we are calling our layout using setContentView method in the form of R.layout.layout_file_name. Here our xml file name is activity_main.xml so we used file name activity_main.

Generally, during the launch of our activityonCreate() callback method will be called by android framework to get the required layout for an activity.

Create UI Element at Runtime

If we want to create UI elements at runtime, we need to create our own custom View and ViewGroup objects programmatically with required layouts.

Following is the example of creating UI elements (TextViewEditTextButton) in LinearLayout using custom View and ViewGroup objects in an activity programmatically.

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        TextView textView1 = new TextView(this);
        EditText editText1 = new EditText(this);
        editText1.setText(“Enter Name”);
        Button button1 = new Button(this);
        button1.setText(“Add Name”);
        LinearLayout linearLayout = new LinearLayout(this);

By creating a custom View and ViewGroups programmatically, we can define UI controls in layouts based on our requirements in android applications.

Width and Height

When we define a UI controls in a layout using an XML file, we need to set width and height for every View and ViewGroup elements using layout_width and layout_height attributes.

Following is the example of setting width and height for View and ViewGroup elements in the XML layout file.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=””

        android:text=”Enter Name” />

If you observe above example, we used different values to set layout_width and layout_height, those are

  • match_parent
  • wrap_content

If we set value match_parent, then the View or ViewGroup will try to match with parent width or height.

If we set value wrap_content, then the View or ViewGroup will try to adjust its width or height based on the content.

Android Different Types of UI Controls

We have a different type of UI controls available in android to implement the user interface for our android applications.

Following are the commonly used UI or input controls in android applications.

Android TextView

In android, TextView is a user interface control that is used to display the text to the user.

To know more about TextView control check this, Android TextView with Examples.

Android EditText

In android, EditText is a user interface control which is used to allow the user to enter or modify the text.

To know more about EditText, check this Android EditText with Examples.

Android AutoCompleteTextView

In android, AutoCompleteTextView is an editable text view which is used to show the list of suggestions based on the user typing text. The list of suggestions will be shown as a dropdown menu from which the user can choose an item to replace the content of the textbox.

To know more about AutoCompleteTextView, check this Android AutoCompleteTextView with Examples.

Android Button

In android, Button is a user interface control that is used to perform an action when the user clicks or tap on it.

To know more about Button in android check this, Android Buttons with Examples.

Android Image Button

In android, Image Button is a user interface control that is used to display a button with an image to perform an action when the user clicks or tap on it.

Generally, the Image button in android looks similar as regular Button and perform the actions same as regular button but only difference is for image button we will add an image instead of text.

To know more about Image Button in android check this, Android Image Button with Examples.

Android Toggle Button

In android, Toggle Button is a user interface control that is used to display ON (Checked) or OFF (Unchecked) states as a button with a light indicator.

To know more about Toggle Button in android check this, Android Toggle Button with Examples.

Android CheckBox

In android, Checkbox is a two-states button that can be either checked or unchecked.

To know more about CheckBox in android check this, Android CheckBox with Examples.

Android Radio Button

In android, Radio Button is a two-states button that can be either checked or unchecked and it cannot be unchecked once it is checked.

To know more about Radio Button in android check this, Android Radio Button with Examples.

Android Radio Group

In android, Radio Group is used to group one or more radio buttons into separate groups based on our requirements.

In case if we group radio buttons using the radio group, at a time only one item can be selected from the group of radio buttons.

To know more about Radio Group in android check this, Android Radio Group with Examples.

Android ProgressBar

In android, ProgressBar is a user interface control which is used to indicate the progress of an operation.

To know more about ProgressBar, check this Android ProgressBar with Examples.

Android Spinner

In android, Spinner is a drop-down list which allows a user to select one value from the list.

To know more about Spinner, check this Android Spinner with Examples.

Android TimePicker

In android, TimePicker is a widget for selecting the time of day, either in 24-hour or AM/PM mode.

To know more about TimePicker, check this, Android TimePicker with Examples.

Android DatePicker

In android, DatePicker is a widget for selecting a date.

To know more about DatePicker, check this Android DatePicker with Examples×250&×280&correlator=1593742181097&frm=20&pv=1&ga_vid=123880624.1649991224&ga_sid=1649991224&ga_hid=614230444&ga_fc=1&u_tz=-420&u_his=3&u_h=768&u_w=1366&u_ah=738&u_aw=1366&u_cd=24&u_sd=1&dmc=4&adx=1027&ady=482&biw=1349&bih=578&scr_x=0&scr_y=0&eid=44759876%2C44759927%2C44759842%2C182982100%2C182982300%2C31067131%2C31066184%2C21067496&oid=2&pvsid=2606336455617619&pem=981&tmod=297360022&uas=0&nvt=1&!4&fsb=1&xpc=p1bgJBv0g6&p=https%3A//×600&×280%2C300x250&correlator=1593742181097&frm=20&pv=1&ga_vid=123880624.1649991224&ga_sid=1649991224&ga_hid=614230444&ga_fc=1&u_tz=-420&u_his=3&u_h=768&u_w=1366&u_ah=738&u_aw=1366&u_cd=24&u_sd=1&dmc=4&adx=1027&ady=752&biw=1349&bih=578&scr_x=0&scr_y=0&eid=44759876%2C44759927%2C44759842%2C182982100%2C182982300%2C31067131%2C31066184%2C21067496&oid=2&pvsid=2606336455617619&pem=981&tmod=297360022&uas=0&nvt=1&!5&btvi=1&fsb=1&xpc=6nK9aJyaNG&p=https%3A// US

Author: admin

Leave a Reply

Your email address will not be published.