2

I'm trying to add a button to a LinearLayout dynamically. Here is my code:

JAVA

    LayoutInflater inflater = mMainActivity.getLayoutInflater();
    View layout = inflater.inflate(R.layout.browse_list_fragment, (ViewGroup) getView(), false);
    LinearLayout breadcrumb = (LinearLayout) layout.findViewById(R.id.browse_list_fragment_previous);

    Button button = new Button(mMainActivity);
    button.setText(name);
    button.setTextColor(mMainActivity.getResources().getColor(R.color.action_bar_breadcrumb));
    button.setTextSize(22);
    button.setTypeface(Typeface.createFromAsset(mMainActivity.getAssets(), "HelveticaNeueBold.ttf"));
    button.setTag(mHashMap);

    breadcrumb.addView(button, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));


XML

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

<LinearLayout android:id="@+id/browse_list_fragment_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/white">

    <Button android:id="@+id/browse_list_fragment_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="@string/button_menu"
        android:textColor="@color/grey"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/carrot_grey"
        android:onClick="buttonClick"/>
</LinearLayout>

<LinearLayout android:id="@+id/browse_list_fragment_previous"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical">

</LinearLayout>

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingLeft="5dp"/>
</LinearLayout>


The Problem
I don't have any exceptions thrown and when I connect the debugger and step through the code I can see that the button is in fact added to the layout. I have tried inflating a button and adding it, different combinations of Java and XML code, stubbing out lines of code, using RelativeLayout as the root layout, removing different parts of the layout and using different widths and heights but I can't ge this button to show up on the screen. Can someone can see what I'm doing wrong or at least point me in the right direction? I'd greatly appreciate it.

2
  • have you tried: breadcrumb.addView(button); just like that? Also, have you tried to put the button in the XML file and check that it actually is showing and not being blocked by some other element? Commented Jun 10, 2013 at 20:39
  • the xml you are showing is the same you put inside setCOntentView? Commented Jun 10, 2013 at 20:46

1 Answer 1

2

You called inflater.inflate() with false as the last argument:

View layout = inflater.inflate(R.layout.browse_list_fragment,
    (ViewGroup) getView(), false);

So you are not adding the layout to any view and thus can't see the button you added to this layout. Try to call inflate() with true or add the layout later to the view.

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

4 Comments

Awesome thanks a lot! So why when I'm in the onCreateView() I can call inflate(layout.id, container, false) and it works there?
I don't know, it shouldn't work... Maybe you missed setContentView(R.layout.browse_list_fragment); there?
I'm in a fragment so there isn't a setContentView... I inflate the layout in onCreateView but I pass in false and amazingly it has always worked.
Oh ok... I never worked with fragments. Returning a view in onCreateView() always shows that view. But if you later want to change the GUI, you will have to attach the created view to another view, either by calling inflate with true or by adding it later manually.

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.