Inflate a layout with ImageButton inside Fragment programmatically
Asked Answered
W

2

7

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,

Weigand answered 20/4, 2012 at 4:11 Comment(0)
S
12

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).

Sochor answered 20/4, 2012 at 4:23 Comment(3)
in my method i tried : btns = View.inflate(getActivity(), R.layout.mallmapactivity_level_buttons, null); I cannont use "this" because my class extends a fragment so I use "getActivity()" instead but I am not sure its the good method to get my fragment context. Anyway doing so still inflate my view according to my general view not according to my fragmentWeigand
Ok actually, using btns = View.inflate(v.getContext(), R.layout.mallmapactivity_level_buttons, myViewGroup); it's working know. ThanksWeigand
Why -2 on this answer ?Sochor
S
0

I had same problem and find my answer in this link: problem with inflater

you can do it without any dependency

LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.view_with_merge_tag, this);
Stibine answered 19/12, 2016 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.