0

I've got a NestedScrollView inside of a ScrollView because I have an Activity with three hidden fragments that contain text, and when you click one of the fragments it opens to reveal the text. But the thing is that there is quite a bit of text so I'm using NestedScrollView so the reader can read all of it, and the first time it works fine (text doesn't overlap), and then second time when I close the fragment and re-open it the text starts overlapping...

So as mentioned, first time it doesn't happen, and then I close the fragment and reopen it and this happens:

enter image description here

So why is this happening and how can I fix it?

Fragment inside Activity

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="none"
    tools:context=".Fragment.PrivacyFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/Grey"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/Grey"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/Grey"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/button_gradient_two">

    <RelativeLayout
        android:id="@+id/relative_layout_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="10dp">

    </RelativeLayout>

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/arrow_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="25dp"
                android:padding="8dp"
                android:src="@drawable/icon_search_100" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="25dp"
                android:padding="8dp"
                android:text="Settings"
                android:textColor="@color/White"
                android:textSize="22sp"
                android:textStyle="bold" />

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/image_profile"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="100dp"
                android:elevation="16dp"
                android:src="@drawable/profile_placeholder" />

            <com.google.android.material.card.MaterialCardView
                style="@style/CustomCardViewStyle"
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:layout_marginTop="150dp"
                app:cardElevation="12dp">

                <RelativeLayout
                    android:id="@+id/relative_layout_two"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="50dp"
                        android:gravity="center_horizontal"
                        android:text="Jack Sparrow"
                        android:textColor="@color/Black"
                        android:textSize="32sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/username"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/name"
                        android:layout_centerHorizontal="true"
                        android:gravity="center_horizontal"
                        android:text="jacksparrow"
                        android:textColor="@color/Grey"
                        android:textSize="22sp"
                        android:textStyle="" />

                    <TextView
                        android:id="@+id/bio"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/username"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="4dp"
                        android:gravity="center_horizontal"
                        android:text="This is my bio!"
                        android:textColor="@color/Grey"
                        android:textStyle="" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/bio"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/privacy"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/privacy"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_privacy"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                        <TextView
                            android:id="@+id/security"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/security"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_security"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                        <TextView
                            android:id="@+id/help"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/help"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_help"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                        <TextView
                            android:id="@+id/about"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/about"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_about"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                    </LinearLayout>

                </RelativeLayout>

            </com.google.android.material.card.MaterialCardView>

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

SettingsActivity

public class SettingsActivity extends AppCompatActivity {

private ImageView mImageProfile, mBackArrow;
private TextView mName, mUsername, mBio, mPrivacy, mSecurity, mHelp, mAbout;
private FirebaseUser mFirebaseUser;
private FrameLayout mPrivacyContainer, mSecurityContainer, mHelpContainer, mAboutContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_tester);

        mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        mImageProfile = findViewById(R.id.image_profile);
        mName = findViewById(R.id.name);
        mUsername = findViewById(R.id.username);
        mBio = findViewById(R.id.bio);
        mPrivacy = findViewById(R.id.privacy);
        mSecurity = findViewById(R.id.security);
        mHelp = findViewById(R.id.help);
        mAbout = findViewById(R.id.about);

        mImageProfile.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_profile_picture));
        mName.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_name));
        mUsername.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_username));
        mBio.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_bio));

        mPrivacyContainer = findViewById(R.id.container_privacy);
        mSecurityContainer = findViewById(R.id.container_security);
        mHelpContainer = findViewById(R.id.container_help);
        mAboutContainer = findViewById(R.id.container_about);

        mPrivacy.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
            if (mPrivacyContainer.getVisibility() == View.GONE) {
                mPrivacyContainer.setVisibility(View.VISIBLE);
            } else {
                mPrivacyContainer.setVisibility(View.GONE);
            }
        });

        mSecurity.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_security, new SecurityFragment(), null).addToBackStack(null).commit();
            if (mSecurityContainer.getVisibility() == View.GONE) {
                mSecurityContainer.setVisibility(View.VISIBLE);
            } else {
                mSecurityContainer.setVisibility(View.GONE);
            }
        });

        mHelp.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_help, new HelpFragment(), null).addToBackStack(null).commit();
            if (mHelpContainer.getVisibility() == View.GONE) {
                mHelpContainer.setVisibility(View.VISIBLE);
            } else {
                mHelpContainer.setVisibility(View.GONE);
            }
        });

        mAbout.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_about, new AboutFragment(), null).addToBackStack(null).commit();
            if (mAboutContainer.getVisibility() == View.GONE) {
                mAboutContainer.setVisibility(View.VISIBLE);
            } else {
                mAboutContainer.setVisibility(View.GONE);
            }
        });

        getUserInfo();
        setBackArrow();
    }

    private void getUserInfo() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(mFirebaseUser.getUid());
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);
                if (user != null) {
                    mName.setText(user.getFullname());
                    mUsername.setText(user.getUsername());
                    mBio.setText(user.getBio());

                    if (user.getImageurl() == null) {
                        mImageProfile.setImageResource(R.drawable.profile_placeholder);
                    } else {
                        Glide.with(getApplicationContext()).load(user.getImageurl()).into(mImageProfile);
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void setBackArrow() {
        mBackArrow = findViewById(R.id.arrow_back);
        mBackArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}



  
JMB
  • 313
  • 2
  • 9

2 Answers2

1

You are adding two fragments on top of each other. Instead of add use replace when executing the fragment transaction:

From:

getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
            if (mPrivacyContainer.getVisibility() == View.GONE) {
                mPrivacyContainer.setVisibility(View.VISIBLE);
            } else {
                mPrivacyContainer.setVisibility(View.GONE);
            }

To:

getSupportFragmentManager().beginTransaction().replace(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
            if (mPrivacyContainer.getVisibility() == View.GONE) {
                mPrivacyContainer.setVisibility(View.VISIBLE);
            } else {
                mPrivacyContainer.setVisibility(View.GONE);
            }
Viktor Petrovski
  • 796
  • 5
  • 14
1

You are getting the fragment overlapped because there is a new fragment added each time.

You simply need to do a check if the fragment exits already then there is no need to add a new fragment. You can assign a 'tag' when adding fragment. Code as follows-

if (fragmentManager.findFragmentByTag("First Fragment") == null)
         fragmentManager.beginTransaction().add(R.id.fragment, new FirstFragment(), "First Fragment").commit();

OR

Rather then using .add() method use .replace() to make sure fragment got replaced each time.

According to your Activity code, you need to choose the correct way, but make sure there is no fragment getting re-created without destroying previous one.

Happy Coding !!

Nikhil Sharma
  • 897
  • 1
  • 4
  • 18
  • Okay thanks man. I did the replace method which works. But why'd you say "According to your Activity code, you need to choose the correct way", you mean the first way might not have worked? – JMB Jul 29 '20 at 10:19
  • 1
    @JMB Suppose you have FirstFragment and SecondFragment, and you are coming from Second Fragment to FistFragment then checking the fragment existence make sence, that what the first method is doing, it checking if there is fragment with a particular tag then its skipping creating one. You can find the use case on this thread https://stackoverflow.com/questions/63126785/previous-fragment-is-restarting-when-we-go-back-to-previous-fragment-using-backs/63128483#63128483 – Nikhil Sharma Jul 29 '20 at 10:29