Create an Object in Android with Included View to Add to Any Activity

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

Many times we want to create an object that has an included view (referring to a layout). For example, a bottom menu that we want to display in all activities or a layer.

In this tutorial, I’m going to show you how to create a reusable object for any project and automatically add it to the desired screen.

First, we start by creating the layout that represents the view of our object. In this example, I’m going to add a very simple TextView.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/capaHelloWorld"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true">
    <TextView
        android:id="@+id/texto_helloWorld"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World" />
</RelativeLayout>

In this example, we have created a layer that displays the text “Hello World” at the top of a RelativeLayout.

Now let’s create an object that will contain the created layout and can be used to add it to any activity. Let’s call it HelloWorldObj.java.

public class HelloWorldObj{
    //To store the context of the activity that will display the object
    private Context cnt;
    //Stores the view that we will create within the object
    private View view;
}

In addition, we create two class attributes, cnt, which refers to the Context of the Activity that will invoke our object, and view, which will contain the view of the XML we created in the previous step.

Now we create the constructor and assign the previously created layout.

public class HelloWorldObj{
    //To store the context of the activity that will display the object
    private Context cnt;
    //Stores the view that we will create within the object
    private View view;
    //Constructor, receives the Context parameter
    public HelloWorldObj(Context cnt) {
        this.cnt = cnt;
        //Create the view by getting the activity of the invoking context
        //and the root view of the activity that wants to display this view
        this.view = ((Activity) cnt).findViewById(android.R.id.content).getRootView();
        //Assign the layout to be the topmost layer of the view of the activity where we want to add it
        ViewGroup viewGroup = (ViewGroup) ((ViewGroup) ((Activity) cnt).findViewById(android.R.id.content)).getChildAt(0);
        //Assign the created layout to the object
        view.inflate(cnt, R.layout.texto_modo_demo, viewGroup);
    }
}

Now let me explain the code I have added a bit.

First, we create the constructor and pass the Context parameter to be able to use and refer to elements of the Activity where we call the object public HelloWorldObj(Context cnt).

In the line this.view = ((Activity) cnt).findViewById(android.R.id.content).getRootView();, we obtain the content (layout) of the activity where we want to add this object with an included view. It’s as if we automatically obtain the layout assigned to the activity where we want to display this object.

Then ViewGroup viewGroup = (ViewGroup) ((ViewGroup) ((Activity) cnt).findViewById(android.R.id.content)).getChildAt(0);, we obtain the “space or slot” where we are going to add our object. In this case, I want to display it above all the layers, so we get the first element .getChildAt(0).

Now we assign the object’s layout to the selected “space” on the layout of the activity that invokes the object: view.inflate(cnt, R.layout.texto_modo_demo, viewGroup);

And all that’s left is to display it in our Activity.

To do that, we add the following inside the onCreate():

...
@Override
protected void onCreate(final Bundle savedInstanceState) {
    //To make our object work correctly
    //it is important to add the view before invokingit:
setContentView(R.layout.main_activity);
//Now simply by invoking the object, we will have our layout
//added to the main_activity layout (we need to pass the context by using this)
new HelloWorldObj(this);
}
...

Simply by invoking the created object, we add the view to the desired activity.

Using this tutorial, you can create Objects or Libraries that self-add to the Activity you need.

I recommend the MagicSnack library created with this technique: https://github.com/IsMMA/MagicSnackBar

Leave a Comment