Android ViewGroup


Android ViewGroup Tutorial and Examples.

A ViewGroup is a special view that can contain other views (called children.) This is class is very important as it acts as the base class for countless view containers and layouts. All those AdapterViews as well as Layouts subclass this class.

Furthermore, it also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.

API Definition

ViewGroup was added in API level 1

public abstract class ViewGroup 
extends View implements ViewParent, ViewManager

Here's it's inheritance hierarchy:

java.lang.Object
   ↳    android.view.View
       ↳    android.view.ViewGroup

Some of the subclasses of the ViewGroup include:

  1. AbsoluteLayout
  2. AdapterView.
  3. FragmentBreadCrumbs.
  4. FrameLayout.
  5. GridLayout.
  6. LinearLayout.
  7. RelativeLayout.
  8. Toolbar etc.

To create for example a custom layout, you subclass a ViewGroup:

public class CustomViewGroup extends ViewGroup {..}

Find a full example of creating a layout manager here.

Then you can create constructors this way:

    public void testConstructor() {
        new CustomViewGroup(mContext);
        new CustomViewGroup(mContext, null);
        new CustomViewGroup(mContext, null, 0);
    }

Here's an example in use:

CustomViewGroup vg = new CustomViewGroup(mContext);

You can then invoke various methods to do different things. For example if you desire to add another view to a viewgroup, here's how you do it:

TextView textView = new TextView(mContext);
vg.addView(textView);

Remember vg is the instance of our custom view group.

Quick ViewGroup Examples

Let's see various full methods of examples of important actions you can perform to a viewgroup or it's children

1. How to add a View to a ViewGroup Programmatically

A ViewGroup is meant to be a container to other views. So how do we add those views? Well you simply invoke the addView() method, passing in a View object.

    public void doAddView() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        TextView textView = new TextView(mContext);

        assertEquals(0, vg.getChildCount());

        vg.addView(textView);
        assertEquals(1, vg.getChildCount());
    }
2. How to add Focusables to a ViewGroup

Let's start by seeing how to add focusables to the viewgroup. We will use the addFocusables() method. But first after instantiating the ViewGroup we set the focusable to true by invoking the setFocusable(true) method.

    public void doAddFocusables() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        vg.setFocusable(true);

        ArrayList<View> list = new ArrayList<View>();
        TextView textView = new TextView(mContext);
        list.add(textView);
        vg.addView(textView);
        vg.addFocusables(list, 0);

        assertEquals(2, list.size());

        list = new ArrayList<View>();
        list.add(textView);
        vg.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        vg.setFocusable(false);
        vg.addFocusables(list, 0);
        assertEquals(1, list.size());
    }
3. How to add States From Children to a ViewGroup

After instantiating our view group, we instantiate a textView too, then add that textview to our ViewGroup.

By this time invoking the addStatesFromChildren() returns false, so we pass true to setAddStatesFromChildren().

Then we perform click to our listview by invoking the performClick() method.

This time our addStatesFromChildren() will return true as well as the isDrawableStateChangedCalled():

    public void doAddStatesFromChildren() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        TextView textView = new TextView(mContext);
        vg.addView(textView);
        assertFalse(vg.addStatesFromChildren());

        vg.setAddStatesFromChildren(true);
        textView.performClick();
        assertTrue(vg.addStatesFromChildren());
        assertTrue(vg.isDrawableStateChangedCalled);
    }
4. How to add Touchables to ViewGroup
    public void doAddTouchables() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        vg.setFocusable(true);

        ArrayList<View> list = new ArrayList<View>();
        TextView textView = new TextView(mContext);
        textView.setVisibility(View.VISIBLE);
        textView.setClickable(true);
        textView.setEnabled(true);

        list.add(textView);
        vg.addView(textView);
        vg.addTouchables(list);

        assertEquals(2, list.size());

        View v = vg.getChildAt(0);
        assertSame(textView, v);

        v = vg.getChildAt(-1);
        assertNull(v);

        v = vg.getChildAt(1);
        assertNull(v);

        v = vg.getChildAt(100);
        assertNull(v);

        v = vg.getChildAt(-100);
        assertNull(v);
    }
5. How to attach view to parent

Let's see how we can attach view to parent. That parent is the ViewGroup.

    public void doAttachViewToParent() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        vg.setFocusable(true);
        assertEquals(0, vg.getChildCount());

        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(10, 10);

        TextView child = new TextView(mContext);
        child.setFocusable(true);
        vg.attachViewToParent(child, -1, param);
        assertSame(vg, child.getParent());
        assertEquals(1, vg.getChildCount());
        assertSame(child, vg.getChildAt(0));
    }
6. How to bring child to Front
    public void testBringChildToFront() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        TextView textView1 = new TextView(mContext);
        TextView textView2 = new TextView(mContext);

        assertEquals(0, vg.getChildCount());

        vg.addView(textView1);
        vg.addView(textView2);
        assertEquals(2, vg.getChildCount());

        vg.bringChildToFront(textView1);
        assertEquals(vg, textView1.getParent());
        assertEquals(2, vg.getChildCount());
        assertNotNull(vg.getChildAt(0));
        assertSame(textView2, vg.getChildAt(0));

        vg.bringChildToFront(textView2);
        assertEquals(vg, textView2.getParent());
        assertEquals(2, vg.getChildCount());
        assertNotNull(vg.getChildAt(0));
        assertSame(textView1, vg.getChildAt(0));
    }
7. How to detach view from Parent
    public void doDetachViewFromParent() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        TextView textView = new TextView(mContext);

        vg.addView(textView);
        assertEquals(1, vg.getChildCount());

        vg.detachViewFromParent(0);

        assertEquals(0, vg.getChildCount());
        assertNull(textView.getParent());
    }
8. How to detach all views from Parent
    public void doDetachAllViewsFromParent() {
        CustomViewGroup vg = new CustomViewGroup(mContext);
        TextView textView = new TextView(mContext);

        vg.addView(textView);
        assertEquals(1, vg.getChildCount());
        assertSame(vg, textView.getParent());
        vg.detachAllViewsFromParent();
        assertEquals(0, vg.getChildCount());
        assertNull(textView.getParent());
    }

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post