Inflate a layout with ImageButton inside Fragment programmatically

Digoun picture Digoun · Apr 20, 2012 · Viewed 15.8k times · Source

I am new on android development, and i was currently creating an application using tabhost and a TabActivity to switch between different activities. This application was working fine, but I recently discover that TabActivity are deprecated and I should use fragment instead.

So, I follow this great tutorial explaining how to do so : http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

Most of it works fine, but there is my problem some of my old activities which are now fragments were inflating ImageButton using this kind of methods :

private void displayLevelButtons() {
     LayoutInflater lf = getLayoutInflater();            
        btns = lf.inflate(R.layout.mallmapactivity_level_buttons, null, false);

        mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
        mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);

        mBtnLevelDown.setOnClickListener(new OnClickListener() {
          @Override
           public void onClick(View view) {                  
              levelDown();
              updateLevelButtons();
           }
       });
       mBtnLevelUp.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View view) {
              levelUp();
              updateLevelButtons();
           }
       });

       LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
       params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;            
       addContentView(btns,params);

    }

In order to be able to compile this method inside a fragment, I changed:

LayoutInflater lf = getLayoutInflater(); 

to

LayoutInflater lf = getActivity().getLayoutInflater();

and

addContentView(btns,params);

to

getActivity().addContentView(btns,params);

But using my method displayLevelButtons() like this make my buttons appears in the right bottom of my general view, not in the right bottom of my fragment.

I also tried to specify a root when inflating my layout with image buttons, my all fragment class looking like this :

public class MyFragment extends Fragment {

    private View btns;        
    private ImageButton mBtnLevelUp;
    private ImageButton mBtnLevelDown; 
    private ViewGroup myViewGroup;

@Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        myViewGroup = container;
        v = inflater.inflate(R.layout.mallmaptile, container, false);           
        return v;
    }

 @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //Do some stuff and then :
            displayLevelButtons();        

    }

 private void displayLevelButtons() {
         LayoutInflater lf = getActivity().getLayoutInflater();          
         btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, false);

         mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
         mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);

         mBtnLevelDown.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {                     
              levelDown();
              updateLevelButtons();
            }
        });
        mBtnLevelUp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
              levelUp();
              updateLevelButtons();
            }
        });

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;            
        getActivity().addContentView(btns,params);

    }

Doing so my buttons are still display in the bottom of my general view not in the bottom of my fragment, and if I try to change the attachToRoot boolean value to true :

btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, true);

I get the error : java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I am sure, I missed something important but I don't what. Any suggestions would be really appreciated.

Thanks in advance,

Answer

Bharat Sharma picture Bharat Sharma · Apr 20, 2012

If you are facing problem in inflating layout then you can use following code:

View headerView = View.inflate(this, R.layout.layout_name, null);

this will give you complete parent layout then you can use that layout to fetch whatever view you want or you can get layout param and then set layout param to any views (In your case button I think).