3

Sorry if this redundant with the ton of questions/answers on inflate, but I could not get a solution to my problem.

I have a compound view (LinearLayout) that has a fixed part defined in XML and additional functionalities in code. I want to dynamically add views to it.

Here is the XML part (compound.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/compoundView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView android:id="@+id/myTextView" 
        android:layout_width="110dp" 
        android:layout_height="wrap_content"
        android:text="000"  />
</LinearLayout>

I have defined in code a LinearLayout to refer to the XML:

public class CompoundControlClass extends LinearLayout {
    public CompoundControlClass (Context context) {
        super(context);
        LayoutInflater li;
        li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.compound_xml,*ROOT*, *ATTACH*);
    }
    public void addAView(){
        Button dynBut = new Button();
        // buttoin def+layout info stripped for brevity
        addView(dynBut);
    }
}

I tried to programmatically add a view with addAView.

If ROOT is null and ATTACH is false, I have the following hierarchy (per HierarchyViewer):

  • CompoundControlClass>dynBut

The original TextView in the XML is gone.

If ROOT is this and ATTACH is true, I have the following hierarchy:

  • CompoundControlClass>compoundView>myTextView
  • CompoundControlClass>dynBut

I would like to have

  • CompoundControlClass>myTextView
  • CompoundControlClass>dynBut

where basically the code and XML are only one unique View. What have I grossly missed?

ANSWER BASED on feedback from D Yao ----------------------

The trick is to INCLUDE the compound component in the main layout instead of referencing it directly.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@layout/comound"
        android:id="@+id/compoundView"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

mainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CompoundControlClass c = (CompoundControlClass) this.findViewById(R.id.compoundView);
        c.addAView(this);
    }  
}

CompoundControlClass.java

public class CompoundControlClass extends LinearLayout {
    public CompoundControlClass(Context context) {
        super(context);
    }
    public CompoundControlClass(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CompoundControlClass(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void addAView(Context context){
         ImageView iv = new ImageView(context);                    
         iv.setImageResource(R.drawable.airhorn);
         addView(iv);
    }
}

compound.xml

<com.sounddisplaymodule.CompoundControlClass    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/compoundView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" > 
    <TextView
        android:layout_width="110dp" 
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="40sp"
        android:textStyle="bold"
        android:text="0:00"  />     
</com.sounddisplaymodule.CompoundControlClass>

1 Answer 1

1

Why not just call addView on the linearlayout? I don't see the need for CompoundControlClass based on the needs you have listed.

LinearLayout v = (LinearLayout)findViewById(R.id.compoundView);
v.addView(dynBut);

In this case, v will contain myTextView, then dynBut.

if you wish to have other functions added and thus really feel a need for creating the compound control class, just leave the constructor as super(etc) and remove the rest

Then your xml would look like this:

<com.yourpackage.CompoundControlClass xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/compoundView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView android:id="@+id/myTextView" 
        android:layout_width="110dp" 
        android:layout_height="wrap_content"
        android:text="000"  />
</com.yourpackage.CompoundControlClass>

you will also have to ensure your CompoundControlClass.java contains the appropriate Constructor which takes both a Context and an attribute set.

Then, in your java, after you've called setContentView, you can do the following:

CompoundControlClass c = (CompoundControlClass)findViewById(R.id.compoundView);
Button b = new Button(context);
//setup b here or inflate your button with inflater
c.addView(b);

this would give you your desired heirarchy.

Sign up to request clarification or add additional context in comments.

8 Comments

That's what I was doing initially, but after adding more logic to the LinearLayout (what you called v), I needed a way to isolate it from the activity. This is why I started to integrate that logic into the CompoundControlClass
It's fine to implement it into an extension of LinearLayout. then, in your xml simply include your extended class instead. see my edit above
That makes perfect sense. I made the suggested change, removed the layout inflater and ran. I do have the dynamic button, but the fixed XML is not there. I double checked with the HierarchyViewer and I have CompoundControlClass>dynBut and no TextView.
Same as yours for the XML and here is the code. The added View in the code is an Image, but this bit works just fine.
if you can post your xml, and the code for the activity where you inflate your xml, i can take a look.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.