package View; import static android.util.Log.println; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.habittracker.R; import java.util.List; import Database.DatabaseHelper; import Model.Habit; public class HomeActivity extends AppCompatActivity { String username; String password; public static String userId; TextView user_greeting_tv; Button addHabitButton; DatabaseHelper db; List habitList; LinearLayout parentLayout; ImageButton refreshBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_home); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); user_greeting_tv = findViewById(R.id.user_greating_tv); addHabitButton = findViewById(R.id.add_habit); refreshBtn = findViewById(R.id.refresh_btn); userId = getIntent().getStringExtra("UserId"); username = getIntent().getStringExtra("Username"); password = getIntent().getStringExtra("Password"); db = new DatabaseHelper(HomeActivity.this); habitList = db.getAllHabits(userId); parentLayout = findViewById(R.id.habits_list); user_greeting_tv.setText(String.format("Hello, %s", username)); addHabitButton.setOnClickListener(v -> { startActivity(new Intent(this, AddHabit.class)); }); refreshBtn.setOnClickListener(v -> { habitList = db.getAllHabits(userId); parentLayout.removeAllViews(); displayHabits(); }); displayHabits(); } private void displayHabits() { for (Habit habit : habitList) { println(Log.DEBUG, "Habit: ", habit.getHabitName()); LinearLayout linearLayout = getLinearLayout(habit); parentLayout.addView(linearLayout); } } private @NonNull LinearLayout getLinearLayout(Habit habit) { LinearLayout linearLayout = new LinearLayout(this); // Create LayoutParams with MATCH_PARENT for width and WRAP_CONTENT for height LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ); linearLayout.setLayoutParams(params); linearLayout.setOrientation(LinearLayout.HORIZONTAL); linearLayout.setPadding(25, 25, 25, 25); CheckBox checkBox = new CheckBox(this); checkBox.setPadding(40, 40, 40, 40); TextView habitTime = new TextView(this); habitTime.setPadding(40, 40, 40, 40); habitTime.setTextSize(20); habitTime.setText(habit.getTime()); habitTime.setTextColor(Color.parseColor("#e2e2e2")); TextView habitName = new TextView(this); habitName.setPadding(40, 40, 40, 40); habitName.setTextSize(20); habitName.setText(habit.getHabitName()); habitName.setTextColor(Color.parseColor("#e2e2e2")); linearLayout.addView(checkBox); linearLayout.addView(habitTime); linearLayout.addView(habitName); return linearLayout; } }