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 7884387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:47:11+00:00 2026-06-03T04:47:11+00:00

I’m fairly new to Android, so this might have an obvious answer, but I

  • 0

I’m fairly new to Android, so this might have an obvious answer, but I can’t seem to find it.

I’m writing a version of a calculator app. I want to make it so that when a user clicks on one of the buttons, this starts a fragment (with more buttons on it that they can then use for more input).

The fragment code is as follows:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class VariableMenu extends Fragment {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);}

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {

         return inflater.inflate(R.layout.fragment, container, false);
}

}

with an XML layout as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<Button
        android:id="@+id/Bx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="X" 
        android:onClick="onButtonClicked"/>
    <Button
        android:id="@+id/By"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Bx"
        android:layout_alignTop="@id/Bx"
        android:text="Y" 
        android:onClick="onButtonClicked"/>
    <Button
        android:id="@+id/Bz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/By"
        android:layout_alignTop="@id/By"
        android:text="Z" 
        android:onClick="onButtonClicked"/>

</RelativeLayout>

(I know I shouldn’t be using hard-coded strings, but I’ll fix that once I get the thing running)

I tried having it added using a fragmenttransaction with the onTouch method of the activating button, like so:

public void onFragmentActivated(View v) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        VariableMenu fragment = new VariableMenu();
        fragmentTransaction.add(R.layout.main, fragment);
        fragmentTransaction.commit();
    }

but that gave an error saying there was “No view found for id:…(main) for fragment VariableMenu.”

So then I made a view for it in the main XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

(Irrelevant things)

    <fragment
        android:id="@+id/Fragment"
        android:name="calculator.app.VariableMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/B1"/>

(Irrelevant things)

</RelativeLayout>

But this resulted in the fragment being there as soon as the activity was created (and setContentView(R.layout.main); ran).

Is there a simple way to programmatically add and remove a fragment with a UI to an activity?

  • 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-03T04:47:12+00:00Added an answer on June 3, 2026 at 4:47 am

    The problem here is that the fragment needs a container with a view id to reside within. Giving it a layout id will result in the error you saw.

    You can add the fragment to your relative layout, but to do that properly you’ll need to assign it appropriate layout parameters so that it can be placed. It would be easier to just create a FrameLayout within the relative layout that wraps its contents, and then add the fragment there.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <FrameLayout
            android:id="@+id/FragmentContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/B1"/>
    
    </RelativeLayout>
    

    Then

    fragmentTransaction.add(R.id.FragmentContainer, fragment);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.