Android Program to Create and use a Compound Control

This Android Program demonstrates Simple Compound Control.

Here is source code of the Program demonstrates a Simple Compound Control using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Compound controls are atomic, self-contained View Groups that contain multiple child Views laid out and connected together. When you create a compound control, you define the layout, appearance, and interaction of the Views it contains. You create compound controls by extending a ViewGroup (usually a layout). To create a new compound control, choose the layout class that’s most suitable for positioning the child controls and extend it.

Main Activity

package com.example.clerable_edit_text;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(new Clearable_Edit_Text(this));
        Log.v("oncreate()", "finished");
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

Clearable Edit Text

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
package com.example.clerable_edit_text;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class Clearable_Edit_Text extends LinearLayout {
    // Define the elements or the child controls of our compound view
    private EditText edittext;
    private Button clear_buuton;
    public Clearable_Edit_Text(Context context) {
        // TODO Auto-generated constructor stub
        String inflatorservice = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;
        li = (LayoutInflater) getContext().getSystemService(inflatorservice);
         * inflate the resource R.layout.clearable_edit_text in the context of
         * this view and automatically attach to this view(true is to
         * automatically attach to this view)
        li.inflate(R.layout.clerable_edit_text, this, true);
        edittext = (EditText) findViewById(;
        clear_buuton = (Button) findViewById(;
    private void hookbutton(){
        clear_buuton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v("clearButton", "clicked");


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:text="Clear" />


Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.


Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.