Android Fragments Overlapping
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 t
Solution 1:
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>
Solution 2:
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();}
Post a Comment for "Android Fragments Overlapping"