Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:15:53+00:00 2026-06-02T11:15:53+00:00

I am new on android development, and i was currently creating an application using

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:15:54+00:00Added an answer on June 2, 2026 at 11:15 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to android development. Currently i am working on an application which
I am new in this field. I am working on Android application development. I
I'm new to Android application development and I'm currently experimenting with various UI ideas.
Im new to this android development, and im currently playing around with the HelloM4A
I'm new to android game development. I currently working on a simple android app
I'm currently working my way through the book Teach Yourself Android Application Development in
Hi I'm new to android development and working on a game. I currently have
I am new to Android development. I am working on application where Sql-lite is
I'm new to Android Development and am currently trying to develop my first application
I am new to android development and am currently working my way through the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.