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

  • Home
  • SEARCH
  • 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 8799797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:25:09+00:00 2026-06-14T00:25:09+00:00

I’m having problems working with fragments. The problem is: I have a layout in

  • 0

I’m having problems working with fragments. The problem is: I have a layout in my activity with a ListView. When i select an item in this ListView, instantiates a fragment replacing the activity layout. My result is both fragment and activity contents are showing on screen, overlaying each other. What can I fix that (the fragment just replacing the activity content)? Here is the codes:

activityLayout.xml

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

   <FrameLayout android:id="@+id/fragment_to_replace"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:orientation="vertical"
        android:layout_gravity="center" >

       <TextView android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            android:text="@string/textView_select_table"
            android:textSize="30dp"/>

       <ListView android:layout_marginTop="20dp"
         android:layout_width="fill_parent"
         android:layout_height="200dp"
         android:id="@+id/listView"
         android:visibility="visible" 
         android:paddingTop="40dp" />

   </FrameLayout>

</LinearLayout>

Activity.java

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity);

    View view = findViewById(R.id.activity);    

    //Getting the ListView
    ListView listView = (ListView) view.findViewById(R.id.listView);
    listView.setTextFilterEnabled(true);

    //Setting the adapter
    listView.setAdapter(new Adapter(this));

    listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

              //Creates a fragment 
              addFragment();
        }

    });

}

 public void addFragment(){

     MyFragment newFragment;

     FragmentTransaction newFragmentTransaction = getSupportFragmentManager().beginTransaction();

     newFragment = (MyFragment) getSupportFragmentManager()
            .findFragmentById(R.id.fragment);

      if (newFragment == null){
               newFragment = new MyFragment(this, getSupportFragmentManager());
           newFragmentTransaction.replace(R.id.fragment_to_replace, 
                new SelectDrinkFragment(this,   getSupportFragmentManager()));
           newFragmentTransaction.addToBackStack(null);
           newFragmentTransaction.commit();

      }
 }

fragmentLayout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/fragment"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView  
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:visibility="visible"
     android:text="@string/textView_two"
     android:textSize="20dp"/>

     <LinearLayout android:layout_marginTop="10dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

        <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:text="@string/textView" />

        <TextView android:id="@+id/textView_two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"/>

    </LinearLayout>

    <TextView 
      android:id="@+id/textView_three"
      android:layout_marginTop="10dp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:visibility="visible"
      android:text="@string/textView_three"/>

     <ListView 
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_weight="1" 
   android:id="@+id/listView_two"
   android:visibility="visible" 
   android:paddingTop="40dp" />

 </LinearLayout>

Fragment.java

 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,      Bundle saveInstanceState){


    View view = inflater.inflate(R.layout.fragment, container, false);  

    final ListView listView = (ListView) view.findViewById(R.id.listView_two); 

    CharSequence [] list = getResources().getStringArray(R.array.array);

    listView.setAdapter(new ListAdapter(getContext(), list));

    return view;

 }

ListAdapter.java

 public class ListAdapter extends BaseAdapter{

      private Context context;
      private CharSequence[] drinkArraySize;

      public ListAdapter(Context context, CharSequence[] array){
          super();
          this.context = context;
          this.array= array;
      }

      @Override
      public int getCount() {
        return array.length;
      }

      @Override
      public Object getItem(int position) {
        return null;
      }

     @Override
     public long getItemId(int position) {
       return 0;
     }

     public Context getContext(){
          return context;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout linearLayout = new LinearLayout(getContext());

        RadioButton radioButton = new RadioButton(getContext());
        radioButton.setText(array[position]);

        linearLayout.addView(radioButton);

        return linearLayout;
     }

}
  • 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-14T00:25:10+00:00Added an answer on June 14, 2026 at 12:25 am

    FragmentTransaction.replace() looks like it only replaces another fragment (and not just any View), so I’m pretty sure this is expected behavior (or at least, I know I’ve had this happen to me before). I suppose you have a few options:

    1. I don’t see in your code anything that would make the fragment’s background opaque. You could try that first and see if it helps. If it does, that might be the simplest.

    2. You could just set the other items’ visibilities to View.GONE at the same time you perform your transaction, but that has some issues as far as backstack goes.

    3. You could replace the ListView and TextView with a ListFragment instead. This is probably the “best” way to do it, but also is probably the most work.

    • 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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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'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
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.