Search code examples
androidxmlviewinflateviewgroup

cannot add inflated view to custom viewgroup


I have been trying to inflate views from xml to their corresponding view objects, then add these view objects to a programmed viewgroup. However, whenever I call myviewgroup.addView(childview), the childview seems to lose all the buttons and things the xml code should have given it. However, the one thing the childview does have is the background color which I programmed in to ensure the childview was given the correct dimensions. The childview is given the correct dimensions, but none of its components are visible

     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    viewchanger= new ViewChanger(this); //my viewgroup
    View mainmenu= inflater.inflate(R.layout.main, null); //the xml code
    mainmenu.setBackgroundColor(Color.rgb(0, 0, 255));
    viewchanger.addView(mainmenu);
    setContentView(viewchanger); 

the above code simply sets the background of my screen to blue, but none of the buttons show up. also, when I setContentView(mainmenu) everything shows up from that xml code perfectly as well as the background color. The point of this viewgroup is so I can switch between any of multiple views regardless of their order as childviews.

Here is the code for viewchanger. I want it to have 7 child views, and when I call a method setView(int v) i want all the other views to be gone, and I want only that one view to be visible.

public class ViewChanger extends ViewGroup{

/**Represent which view to show, only one can show at a time*/
public static final int MainMenu=0, ChooseMap=1, MapOptions=2, 
        SetLocation=3, view=4, EditMap=5, CreateMap=6;
private int current; //the current view
private View[] views= new View[7];

public ViewChanger(Context context) {
    super(context);
            //load 7 views into view array here
            //LayoutInflater inflater =    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           /*views[0]= inflater.inflate(R.layout.main, null);
            * views[1]= inflater.inflate(R.layout.choose, null);
            * views[2]= inflater.inflate(R.layout.mapoptions, null);
        * views[0].setBackgroundColor(Color.rgb(255, 0, 0));
    * views[1].setBackgroundColor(Color.rgb(255, 255, 0));
    * views[2].setBackgroundColor(Color.rgb(0, 255, 0));*/
    * addView(views[0]);
    * addView(views[1]);
    * addView(views[2]);
            * this is how i want to load the childviews*/

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    for(int i = 0; i < getChildCount(); i++){
                getChildAt(i).layout(0, 0, getWidth(), getHeight());
            }
}
    /**Sets the view of the view group, only one view can be viewed at a time*/
public void setView(int v){
            current=v;
    for (int i=0; i<views.length; i++){
        if (v==i){
            views[i].setVisibility(View.VISIBLE);
        } else{
            views[i].setVisibility(View.GONE);
        }
    }
}

When I load the child views in the ViewChanger constructor, the viewgroup only shows me the background colors of whichever view I set through setView(int v), and not the buttons.

Here is the xml code for main, but all the others look very similar to this:

 <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:gravity="center"
   android:orientation="vertical" >


 <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="The title"
    android:textAppearance="?android:attr/textAppearanceLarge" />


 <TextView
    android:id="@+id/welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome [...]"
    android:textAppearance="?android:attr/textAppearanceLarge" />




 <Button
    android:id="@+id/load"
    android:layout_width="122dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Load Map" />




 <Button
    android:id="@+id/create"
    android:layout_width="122dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Create Map" />






 <Button
    android:id="@+id/users"
    android:layout_width="122dp"
    android:layout_height="wrap_content"
    android:text="Other User" />

</LinearLayout>

Solution

  • After running mentally through everything I could think of, I created a dummy project identical to this to check out a couple of possibilities. For some reason, having ViewChanger extend from FrameLayout instead of ViewGroup makes this work - most likely because ViewGroup lacks something needed to render it's child views (I tried overriding onDraw, but it still didn't work). Since you're only displaying one view at a time - the usual use of FrameLayout - I'd recommend just making this change; it's likely a lot easier than isolating and fixing the missing draw functionality in ViewGroup.

    public class ViewChanger extends FrameLayout {
    

    should be the only change you need to make to get the original design (adding views from the constructor) to work properly.