Deepak
Deepak

Reputation: 1258

How to add compound control to a layout at runtime?

I created a compound control using xml, and am able to re-use it by placing in other xml layouts. But i want to add it at runtime using java code. I tried the following code but only the textview is visible and the compound-control is not shown...

I am quiet new to android so would appreciate any help or suggestions..

COMPOUND - CONTROL LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_lines"

>
    <TextView android:id="@+id/msg_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="SAMPLE MESSAGE TITLE"
    />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <Button android:id="@+id/btn_shw"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="SHOW MSG"
            android:layout_weight="1"
        />
        <Button android:id="@+id/btn_dis"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text=" DISABLE"
            android:layout_weight="1"
        />
        <Button android:id="@+id/btn_del"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text=" DELETE "
            android:layout_weight="1"
        />
    </LinearLayout>    
</LinearLayout>

COMPOUND-CONTROL CODE

public class RemainderControl extends LinearLayout
{
    Button btn1,btn2,btn3;
    TextView tv1;
    public RemainderControl(Context context)
    {
        super(context);
            LayoutInflater inflater=LayoutInflater.from(context);
        inflater.inflate(R.layout.remainder_control,this);

        loadviews();
    }

    public RemainderControl(Context context,AttributeSet attrs)
    {
        super(context,attrs);

        LayoutInflater inflater=LayoutInflater.from(context);
        inflater.inflate(R.layout.remainder_control,this);

        loadviews();


    }

    private void loadviews()
    {
        btn1=(Button)findViewById(R.id.btn_shw);
        btn2=(Button)findViewById(R.id.btn_dis);
        btn3=(Button)findViewById(R.id.btn_del);
        tv1=(TextView)findViewById(R.id.msg_title);
            btn1.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                tv1.setText("SHOW BUTTON PRESSED");     
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                tv1.setText("DISABLE BUTTON PRESSED");      
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                tv1.setText("DELETE BUTTON PRESSED");
            }
        });
        tv1.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                tv1.setText("");
            }
        });
    }
}

CODE TO ADD CONTROL

public class RemainderList extends Activity 
{
    ScrollView sv1;
    LinearLayout ll1;
    deepak.android.remainder.RemainderControl rc1;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        int ht=LayoutParams.WRAP_CONTENT;
        int wt=LayoutParams.FILL_PARENT;

        sv1=new ScrollView(this);
        ll1=new LinearLayout(this);
        ll1.setOrientation(LinearLayout.VERTICAL);
        sv1.addView(ll1);

        TextView tv1=new TextView(this);
        tv1.setText("THIS IS SAMPLE TEXT");
        ll1.addView(tv1,new LinearLayout.LayoutParams(wt,ht));

        rc1=new deepak.android.remainder.RemainderControl(this);
        ll1.addView(rc1,new LinearLayout.LayoutParams(wt,ht));

        setContentView(sv1);
    }
}

Upvotes: 0

Views: 1296

Answers (1)

ernazm
ernazm

Reputation: 9268

When your control is included in xml, public RemainderControl(Context context,AttributeSet attrs) constructor is invoked. But in your code you directly invoke constructor public RemainderControl(Context context). Move all your code that inflates the layout and sets the listeners into some method (for example init()) and call it in both constructors.

Upvotes: 3

Related Questions