1

In my Android app I have an Edit Text and a button which when clicked adds a fragment to my main activity containing the message that was written in my Edit Text. The problem is that when I change the message and click the button the fragments overlap. I have already tried to add a background to my fragment's layout but when I do that the background appears on top of all messages (the text view is not even visible, only selectable for copy/pasting).

The Code:

Method in Main Activity which adds the fragment:

public void viewMessage(View view) { // Create a new Fragment to be placed in the activity layout ViewMessageFragment fragment = new ViewMessageFragment(); //Add the message to the fragment //Get the message EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); //Add it to the fragment Bundle bundle = new Bundle(); bundle.putString(EXTRA_MESSAGE, message); fragment.setArguments(bundle); // Add the fragment to the 'view_message' FrameLayout FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.view_message, fragment); transaction.commit(); } 

Fragment:

public class ViewMessageFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { //Get the message String message = getArguments().getString(MainActivity.EXTRA_MESSAGE); //Create the TextView TextView textView = new TextView(container.getContext()); textView.setTextSize(20); textView.setGravity(Gravity.CENTER); textView.setWidth(-1);//This line is the same than android:layout_width="match_parent" textView.setHeight(-1);//This line is the same than android:layout_height="match_parent" textView.setTextIsSelectable(true); textView.setText(message); //Add the TextView container.addView(textView); // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_view_message, container, false); } } 

Fragment's Layout:

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

I would really appreciate if someone could help me! Thank You!

Edit: Am I adding the textView to the fragment container(a FrameLayout) in the next line?

container.addView(textView); 

If I am, can that be causing the problem?

7
  • Sounds to me like the issue is that you're adding multiple (fragment) Views to the one activity and it's displaying them on top of each other. If this is indeed the issue, then you'll need to either delete the old fragment when you add the new one, change the existing view, or if you're wanting them to be displayed one after the other, then set your MainActivity to have a vertical layout view. If the latter, you may find that using a ListView and adding your fragments to the underlying listAdapter is what you're after. Commented Aug 4, 2014 at 2:30
  • @PeterCarpenter First of all thank you for your reply but actually I am deleting the old fragment, the transaction.replace() line deletes the old fragment and adds the new one, thats why I don't understand how the old fragment is still in the screen. Commented Aug 4, 2014 at 2:55
  • Do you need to perform transaction.addToBackStack(null) before your commit? I haven't used the fragment manager personally, but all of the code that I see has this... Commented Aug 4, 2014 at 5:31
  • Actually, this looks like it could be a likely cause also - but I'm really only guessing for you now... sluse.com/view/5293850 Commented Aug 4, 2014 at 5:35
  • @PeterCarpenter I tried adding the transaction.addToBackStack(null); sentence but it didn't change anything. Also in the link you shared the problem is the same than mine but it's caused by different things, he added the fragments to the XML file, that way they can not be removed or replaced, that's why I did not do that. And again thank you for your comment! Commented Aug 4, 2014 at 6:39

2 Answers 2

3

SOLVED:

The problem was that I was adding the TextView to the fragment's container (a FrameLayout), I solved it adding the TextView to the Fragmet's Layout and dynamically changing its text.

The Code:

Fragment:

public class ViewMessageFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { //Get the message String message = getArguments().getString(MainActivity.EXTRA_MESSAGE); //Inflate the layout in a View. This way you get the fragments layout View mContainer = inflater.inflate(R.layout.fragment_view_message, container, false); //Find the TextView contained in the fragments Layout and change its message TextView textView = (TextView) mContainer.findViewById(R.id.show_message); textView.setText(message); //Return the layout return mContainer; } 

Fragment's Layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/show_message" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="20sp" android:textIsSelectable="true"/> </LinearLayout> 
Sign up to request clarification or add additional context in comments.

Comments

1

Here i Found the better solution: Well Setting up fragment background color is not a solution because fragment will be still in the activity stack which may consume memory.

Solution would be remove all views from your framelayout before adding any new fragment.

private void changeFragment(Fragment fr){ FrameLayout fl = (FrameLayout) findViewById(R.id.mainframe); fl.removeAllViews(); FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); transaction1.add(R.id.mainframe, fr); transaction1.commit();} 

1 Comment

Doesn't work. Instead I get a blank screen which I suppose is the the parent element.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.