Impossible to change a fragment's TextView

34 Views Asked by At

I'm a total beginner in Java and XML, and I started after watching some tutorials to create my own calendar app. This app has a calendar fragment that contains a TextView that shows "Welcome back @USERNAME".

I created an activity that opens at the first launch that asks for the name of the user, and that stocks the name in a GlobalVariable class as a "String first_name". But when I launch the app, after giving the name, it doesn't change on the welcome text. I checked if it was a problem on my global variable by replacing in the setText() with a random string, and it still doesn't work. Could you guys help me please ?

CalendarFragment.java

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CalendarFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_calendar, container, false);
        TextView welcomeMessage = (TextView) view.findViewById(R.id.welcomeBackTitle);
        welcomeMessage.setText(GlobalVariable.first_name);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_calendar, container, false);

    }
}

fragment_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/calendar"
    tools:context=".CalendarFragment">
    <com.airbnb.lottie.LottieAnimationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        app:lottie_rawRes="@raw/wavybackground"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"/>

    <TextView
        android:id="@+id/welcomeBackTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome back Username !"
        android:textColor="@color/grey"
        android:fontFamily="@font/tommy"
        android:textSize="32sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="20dp"
        />
    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lottieCloud"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:lottie_rawRes="@raw/cloud"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="100dp"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"/>
    <TextView
        android:id="@+id/currentTaskTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current Task :"
        android:textColor="@color/darkblue"
        android:fontFamily="@font/tommy"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="200dp"
        android:layout_marginRight="50dp"/>
    <TextView
        android:id="@+id/currentTaskName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Signal Treatment (Exercices)"
        android:textColor="@color/purple_700"
        android:fontFamily="@font/tommy"
        android:textSize="24sp"
        app:layout_constraintTop_toTopOf="@id/currentTaskTitle"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="50dp"/>
    <TextView
        android:id="@+id/currentTaskHour"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="From 4pm. to 5pm."
        android:textColor="@color/purple_700"
        android:fontFamily="@font/tommy"
        android:textSize="24sp"
        app:layout_constraintTop_toTopOf="@id/currentTaskName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="40dp"/>

    <TextView
        android:id="@+id/nextTask"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next task : 15 minutes pause."
        android:textColor="@color/purple_500"
        android:fontFamily="@font/tommy"
        android:textSize="24sp"
        app:layout_constraintTop_toTopOf="@id/currentTaskHour"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="80dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import android.app.Dialog;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.airbnb.lottie.LottieAnimationView;
import com.example.tasklins.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    //Alert Dialog for the new calendar creation
    Dialog dialog;
    Button btnDialogCancel, btnDialogYes;
    TextView titleDialog, subTitleDialog;
    // Keep the ID of the current page, on calendar by default
    int currentPage = R.id.calendar, newPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //multiple windows binding
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        fragmentSelection(currentPage);
        binding.bottomNavigationView.setBackground(null);

        //initialisation of dialog box and buttons
        dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.dialog_box_new_calendar);
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawable(getDrawable(R.drawable.new_calendar_dialog_bg));
        dialog.setCancelable(false); //impossible to quit the dialog without clicking yes or no
        titleDialog = dialog.findViewById(R.id.title);
        subTitleDialog = dialog.findViewById(R.id.alertInformation);
        btnDialogYes = dialog.findViewById(R.id.btnDialogYes);
        btnDialogCancel = dialog.findViewById(R.id.btnDialogCancel);

        btnDialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btnDialogYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPage = newPage;
                fragmentSelection(currentPage);
                dialog.dismiss();
            }
        });

        binding.bottomNavigationView.setOnItemSelectedListener(item-> {
            //TODO : ADD A CLEAR PAGE FEATURE FOR THE CALENDAR
            newPage = item.getItemId();
            if(currentPage == R.id.addCalendar)
            {
                newDialog("Are you sure ?", "You are about to leave the add calendar page. You will lose all your modifications. Do you really want to leave?", "Yes", "Stay Here");
                dialog.show();
            }
            else {
                currentPage = newPage;
                fragmentSelection(item.getItemId());
            }
            return true;
        });
        binding.addCalendar.setBackground(null);
        binding.addCalendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newDialog("Confirm Changement ?", "Are you sure you want to create a new calendar? Your weekly progress will be deleted.", "Yes", "Cancel");
                newPage = R.id.addCalendar;
                dialog.show();
            }
        });
    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
        fragmentTransaction.replace(R.id.frame_layout, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.commit();
    }

    public void fragmentSelection(int destinationID)
    {
        if(destinationID == R.id.calendar) {
            replaceFragment(new CalendarFragment());
        } else if (destinationID == R.id.progress) {
            replaceFragment(new ProgressFragment());
        } else if (destinationID == R.id.addCalendar) {
            replaceFragment(new NewCalendarFragment());
        } else if (destinationID == R.id.tasklin) {
            replaceFragment(new TasklinFragment());
        } else {
            replaceFragment(new ProfileFragment());
        }
    }
    // Method that helps creating dynamic dialogs
    public void newDialog(String title, String content, String btnYes, String btnNo) {

        titleDialog.setText(title);
        subTitleDialog.setText(content);
        btnDialogYes.setText(btnYes);
        btnDialogCancel.setText(btnNo);
    }
}````
0

There are 0 best solutions below