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?
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>