ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Mobile Application Development
    EC-333
    Progress0 / 33 topics
    Topics
    1. Mobiles Application Development Platform2. HTML5 for Mobiles3. Android OS: Architecture, Framework and Application Development4. iOS: Architecture, Framework5. Application Development with Windows Mobile6. Eclipse7. Fragments8. Calling Built-in Applications using Intents9. Displaying Notifications10. Components of a Screen11. Adapting to Display Orientation12. Managing Changes to Screen Orientation13. Utilizing the Action Bar14. Creating the User Interface15. Listening for UI Notifications16. Views17. User Preferences18. Persisting & Sharing Data19. Sending SMS Messages20. Getting Feedback21. Sending E-mail22. Displaying Maps23. Consuming Web Services Using HTTP24. Web Services: Accessing and Creating25. Threading26. Publishing Android Applications27. Deployment on App Stores28. Mobile Programming Languages29. Challenges with Mobility and Wireless Communication30. Location-aware Applications31. Performance/Power Tradeoffs32. Mobile Platform Constraints33. Emerging Technologies
    EC-333›Views
    Mobile Application DevelopmentTopic 16 of 33

    Views

    6 minread
    1,093words
    Intermediatelevel

    Views in Android

    In Android, a View is a fundamental building block of the user interface (UI) of an app. It represents the basic element on the screen that the user interacts with. Everything that you see and interact with on an Android app screen—such as buttons, text fields, images, and layouts—is a type of View or a subclass of it. Views are used to create the app's interface and respond to user input.

    1. What is a View?

    A View is a UI element that occupies a rectangular area on the screen. It could represent anything from a button, an image, or even an entire layout. Views are the building blocks for user interface elements. In Android, almost everything visible to the user is a View.

    • View class: The base class for all UI elements in Android.
      • It provides basic functionality for drawing, handling events, and managing layout.
      • It can be extended to create custom views for specific use cases.

    2. Types of Views

    Here are some of the most common types of Views that are used in Android development:

    A. TextView

    • TextView is used to display text on the screen.
    • It can also be used to set the font, color, size, and other text attributes.

    Example:

    TextView textView = new TextView(this);
    textView.setText("Hello, Android!");
    setContentView(textView);
    

    B. Button

    • Button is a clickable UI element that performs an action when clicked.
    • You can set an OnClickListener to respond to button clicks.

    Example:

    Button button = new Button(this);
    button.setText("Click Me");
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
        }
    });
    

    C. ImageView

    • ImageView is used to display images on the screen.
    • It can display images from resources, files, or URLs.

    Example:

    ImageView imageView = new ImageView(this);
    imageView.setImageResource(R.drawable.my_image);
    setContentView(imageView);
    

    D. EditText

    • EditText is a subclass of TextView and is used to allow users to enter text.
    • It is commonly used for forms, search fields, and user input.

    Example:

    EditText editText = new EditText(this);
    editText.setHint("Enter your name");
    setContentView(editText);
    

    E. CheckBox

    • CheckBox is a UI element that allows users to select or deselect an option.
    • It’s used when there are multiple options to choose from, but only a single option can be selected at a time.

    Example:

    CheckBox checkBox = new CheckBox(this);
    checkBox.setText("Accept Terms and Conditions");
    setContentView(checkBox);
    

    F. RadioButton

    • RadioButton is used to select one option from a group of options. Only one radio button can be selected at a time in a group.

    Example:

    RadioButton radioButton = new RadioButton(this);
    radioButton.setText("Option 1");
    setContentView(radioButton);
    

    G. ImageButton

    • ImageButton is a type of Button that displays an image rather than text. It’s often used for icon-based buttons.

    Example:

    ImageButton imageButton = new ImageButton(this);
    imageButton.setImageResource(R.drawable.icon);
    setContentView(imageButton);
    

    H. ProgressBar

    • ProgressBar is a UI element that shows the progress of a task, either as a spinning wheel (indeterminate) or a horizontal bar (determinate).

    Example (Indeterminate):

    ProgressBar progressBar = new ProgressBar(this);
    setContentView(progressBar);
    

    Example (Determinate):

    ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
    progressBar.setMax(100);
    progressBar.setProgress(50);
    setContentView(progressBar);
    

    I. Spinner

    • Spinner is a UI element that allows users to select one item from a list of options, similar to a dropdown in web development.

    Example:

    Spinner spinner = new Spinner(this);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.spinner_items, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
    setContentView(spinner);
    

    3. View Hierarchy and Layouts

    In Android, Views are often organized in a hierarchical structure. This structure is necessary for grouping related views together to form a complete layout.

    A. ViewGroup

    • A ViewGroup is a type of View that can contain other Views (or ViewGroups). It acts as a container or parent for other views.
    • Examples of ViewGroups: LinearLayout, RelativeLayout, FrameLayout, ConstraintLayout, and TableLayout.

    LinearLayout Example:

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    Button button = new Button(this);
    button.setText("Click Me");
    linearLayout.addView(button);
    setContentView(linearLayout);
    
    • LinearLayout arranges child views (like buttons or text fields) either vertically or horizontally.

    RelativeLayout Example:

    RelativeLayout relativeLayout = new RelativeLayout(this);
    TextView textView = new TextView(this);
    textView.setText("This is a text.");
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    textView.setLayoutParams(params);
    relativeLayout.addView(textView);
    setContentView(relativeLayout);
    
    • RelativeLayout arranges views relative to each other. You can position a view above, below, left, or right of another view.

    ConstraintLayout Example:

    ConstraintLayout layout = new ConstraintLayout(this);
    TextView textView = new TextView(this);
    textView.setText("ConstraintLayout Example");
    layout.addView(textView);
    setContentView(layout);
    
    • ConstraintLayout is a flexible layout that allows you to define complex relationships between views using constraints.

    4. Handling Events with Views

    Views in Android can respond to user interactions like taps, clicks, gestures, and touch events. You can attach Listeners to a View to handle these events.

    A. Button Click Example (OnClickListener):

    Button button = new Button(this);
    button.setText("Click Me");
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();
        }
    });
    

    B. Text Input Example (TextWatcher):

    EditText editText = new EditText(this);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}
    
        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {}
    
        @Override
        public void afterTextChanged(Editable editable) {
            // Code to handle text change
        }
    });
    

    5. Custom Views

    You can create custom views by extending the View class. This allows you to create completely new UI elements with custom behavior and appearance.

    Example:

    public class CustomView extends View {
        public CustomView(Context context) {
            super(context);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            // Custom drawing code here
        }
    }
    
    • onDraw(Canvas canvas): Override this method to draw custom graphics (e.g., shapes, lines, text).
    • onMeasure(int widthMeasureSpec, int heightMeasureSpec): Override this method to measure the size of your custom view.

    6. Conclusion

    In Android, Views are the foundation of the user interface. They are used to display information and accept input from the user. From simple UI elements like TextView and Button to complex containers like LinearLayout and RelativeLayout, Views and ViewGroups are used together to create Android’s UI.

    Understanding how to work with views and how to listen for user interactions through listeners is essential for building interactive and responsive Android apps. You can also extend the View class to create custom components tailored to your specific needs.

    Previous topic 15
    Listening for UI Notifications
    Next topic 17
    User Preferences

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time6 min
      Word count1,093
      Code examples0
      DifficultyIntermediate