I'm learning android app development by creating a Wordle like app. I'm currently testing if I can change the custom view Letter's properties by using a button named Set which calls the testSet method in MainActivity. The problem is the properties of the views do get changed but they don't get reflected in main_activity. I can't find a way to refresh the activity. I've tried the solutions to these questions
Android - How to refresh an activity
Programmatically relaunch/recreate an activity?
But none of them worked for me.
MainActivity.java:
package com.example.wordlepromax;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
private String letter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout layout = findViewById(R.id.attempt1);
Letter currView = (Letter) layout.getChildAt(0);
if (letter != null) {
currView.setLetter(letter);
}
}
public void testSet(View view) {
letter = "N";
recreate();
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
letter = savedInstanceState.getString("view_Letter");
}
// invoked when the activity may be temporarily destroyed, save the instance state here
@Override
public void onSaveInstanceState(Bundle outState) {
// call superclass to save any view hierarchy
outState.putString("view_Letter", letter);
super.onSaveInstanceState(outState);
}
}
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/attempt1"
android:layout_width="380dp"
android:layout_height="94dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.483"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.example.wordlepromax.Letter
android:id="@+id/letter10"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toStartOf="@+id/letter11"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="@+id/letter11"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/letter12"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/letter10"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="@+id/letter12"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/letter13"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="@+id/letter13"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/letter14"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="@+id/letter14"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="@string/set_letter_test"
android:onClick="testSet"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/attempt1" />
</androidx.constraintlayout.widget.ConstraintLayout>
UPDATE: I was going through my old questions here and found this silly question I asked when I first started android app development and just wanted to update that both Vanilil and a_local_nobody are right all you have to do is change the attributes of a view and it'll change the appearance itself.