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:
publicclassViewMessageFragmentextendsFragment
{
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Get the messageStringmessage= getArguments().getString(MainActivity.EXTRA_MESSAGE);
//Inflate the layout in a View. This way you get the fragments layoutViewmContainer= inflater.inflate(R.layout.fragment_view_message, container, false);
//Find the TextView contained in the fragments Layout and change its messageTextViewtextView= (TextView) mContainer.findViewById(R.id.show_message);
textView.setText(message);
//Return the layoutreturn mContainer;
}
Fragment's Layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid: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.
privatevoidchangeFragment(Fragment fr){
FrameLayoutfl= (FrameLayout) findViewById(R.id.mainframe);
fl.removeAllViews();
FragmentTransactiontransaction1= getSupportFragmentManager().beginTransaction();
transaction1.add(R.id.mainframe, fr);
transaction1.commit();}
Post a Comment for "Android Fragments Overlapping"