I updated Gradle, Java, and Android Studio then my fragments wont inflate properly

34 Views Asked by At

I have an android application and when I tap one of the fragments from the bottom navigtion it "works" but doesn't show the recyclerview and the button that is in the xml file.

ActivityMain.java

package com.example.goalhero;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;


public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;

    private HabitFrag habitFrag = new HabitFrag();
    private DailiesFrag dailiesFrag = new DailiesFrag();
    private TodosFrag todosFrag = new TodosFrag();
    private RewardsFrag rewardsFrag = new RewardsFrag();

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }

    // Helper method to show the PopupWindow
    void showPopupWindow() {
        // Inflate the popup_layout.xml
        View popupView = LayoutInflater.from(this).inflate(R.layout.image_popup, findViewById(R.id.main_layout), false);

        // Create the PopupWindow
        PopupWindow popupWindow = new PopupWindow(
                popupView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );

        // Set background color for the popup window (optional)
        popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        // Set animation style for the popup window (optional)
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

        // Get references to the views inside the popup layout
        ImageView imageViewReward = popupView.findViewById(R.id.imageViewReward);
        TextView goodJobMessage = popupView.findViewById(R.id.goodJobMessage);
        Button closeButton = popupView.findViewById(R.id.closeButton);
        TextView congratsText = popupView.findViewById(R.id.CongratsText);

        // Set layout parameters for each view
        RelativeLayout.LayoutParams congratsTextParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        congratsTextParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        congratsTextParams.setMargins(0, 0, 0, 120);  // Adjusted top margin
        congratsText.setLayoutParams(congratsTextParams);

        RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        imageViewParams.addRule(RelativeLayout.BELOW, R.id.CongratsText);  // Positioned below Congratulations!
        imageViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        imageViewReward.setLayoutParams(imageViewParams);

        RelativeLayout.LayoutParams goodJobMessageParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        goodJobMessageParams.addRule(RelativeLayout.BELOW, R.id.imageViewReward);
        goodJobMessageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        goodJobMessageParams.setMargins(0, 120, 0, 0);  // Adjusted top margin
        goodJobMessage.setLayoutParams(goodJobMessageParams);

        RelativeLayout.LayoutParams closeButtonParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        closeButtonParams.addRule(RelativeLayout.BELOW, R.id.imageViewReward);
        closeButtonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        closeButtonParams.setMargins(0, 240, 0, 0);
        closeButton.setLayoutParams(closeButtonParams);

        // Set click listener for the close button
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Close the popup window
                popupWindow.dismiss();
            }
        });

        // Show the popup window in the center
        popupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
    }

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

        System.out.println("Setting the layout to the activity_main.xml");
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                if (item.getItemId() == R.id.action_profile) {

                    Intent intent = new Intent(MainActivity.this, ProfilePage.class);
                    startActivity(intent);

                    return true;

                } else if (item.getItemId() == R.id.action_settings){

                    return true;

                } else if (item.getItemId() == R.id.action_dashboard){

                    return true;

                } else if (item.getItemId() == R.id.action_notifications){

                    return true;

                } else if (item.getItemId() == R.id.action_logout){

                    return true;

                }

                return false;
            }
        });

        bottomNavigationView = findViewById(R.id.bottom_navigation);

        getSupportFragmentManager().beginTransaction().replace(R.id.container, habitFrag).commit();
        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                if (item.getItemId() == R.id.habits) {
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, habitFrag).commit();
                    return true;
                } else if (item.getItemId() == R.id.dailies) {
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, dailiesFrag).commit();
                    return true;
                } else if (item.getItemId() == R.id.todos) {
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, todosFrag).commit();
                    return true;
                } else if (item.getItemId() == R.id.rewards) {
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, rewardsFrag).commit();
                    return true;
                }

                return false;
            }
        });

    }

}

HabitFrag.java

package com.example.goalhero;

import static android.app.Activity.RESULT_OK;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

public class HabitFrag extends Fragment {
    private Button createNewHabitButton;
    private RecyclerView habitsRecyclerView;
    HabitsRecyclerViewAdapter adapter;
    final int requestCodeForCreateNewHabitActivityCall = 1;
    private DataBaseHelper dbHelper;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        System.out.println("Made it here. (onCreate)");
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(@NonNull Context context) {

        System.out.println("Made it here. (onAttach)");
        super.onAttach(context);
        dbHelper = new DataBaseHelper(context, "HABITS_DATABASE");
    }

    public void onResumeFragment() {

        System.out.println("Made it here. (onResumeFragment)");
        // This method will be called when the fragment's onResume is triggered
        // You can place your code here to retrieve and update data
        Context context = getContext();
        ArrayList<Habit> allHabits = dbHelper.getAll();

        // Sets the list of habits on the screen
        adapter.setHabits(allHabits);
        habitsRecyclerView.setAdapter(adapter);
        habitsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void onResume() {
        System.out.println("Made it here. (onResume)");

        super.onResume();
        onResumeFragment();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        System.out.println("Made it here. (onViewCreated)");

        habitsRecyclerView = view.findViewById(R.id.habitsRecyclerView);

        // Initialize the adapter here
        adapter = new HabitsRecyclerViewAdapter(getContext(), dbHelper);
        habitsRecyclerView.setAdapter(adapter);
        habitsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        createNewHabitButton = view.findViewById(R.id.createNewHabitBtn);
        createNewHabitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CreateNewHabitActivity.class);
                startActivityForResult(intent, requestCodeForCreateNewHabitActivityCall);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            // Retrieve habit information from the result intent and add it to the database
            assert data != null;
            String habitInfo = data.getStringExtra("habitInfo");
            boolean isEasy = data.getBooleanExtra("isEasy", true);
            String timeFrame = data.getStringExtra("habitTimeFrame");

            if (timeFrame == null) {
                dbHelper.addOne(new Habit(habitInfo, 0, isEasy));
            } else {
                dbHelper.addOne(new Habit(habitInfo, 0, isEasy, timeFrame));
            }

            // Retrieve all habits from the database after adding the new one
            ArrayList<Habit> allHabits = dbHelper.getAll();

            // Update the RecyclerView adapter with the new list of habits
            adapter.setHabits(allHabits);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("Made it here. (onCreateView)");
        return inflater.inflate(R.layout.fragment_habit, container, false);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:backgroundTint="@color/white"
    android:id="@+id/main_layout">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:title="Goal Getter"
        app:titleTextColor="@android:color/white"
        android:layout_alignParentTop="true" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">

        <!-- This is where your RecyclerView or other content goes -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize" />

    </FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>

fragment_habit.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".HabitFrag"
    android:padding="10dp"
    android:background="@color/white">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create new habit"
        android:id="@+id/createNewHabitBtn"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50sp"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:id="@+id/habitsRecyclerView"
        android:padding="10dp"

        />

I honestly have not the slightest clue of what to do, I don't even know where to start... Any help from someone in the know would be extremely appreciated. Thanks!

0

There are 0 best solutions below