Search code examples
javaandroidimageviewandroid-linearlayouthorizontalscrollview

Horizontalscrollview: imageView disappear when coming back from another Activity


I'm developing my very first app and I have an issue with ImageViews and Horizontalscrollview: I want to collect all the goals reached by the user adding images inside an Horizontalscrollview (goal reached --> new image displayed).

how 4 completed goals are displayed

They show properly and everything seems to work but when I open another activity and then I come back, they disappear. I tried to invalidate the layout but nothing, am I missing something?

Thank you in advance!

This is my XML file

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
                <androidx.coordinatorlayout.widget.CoordinatorLayout
                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/layout3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".Main3Activity">

                <HorizontalScrollView
                    android:id="@+id/horizontal_scroll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="300dp">

                    <LinearLayout
                        android:id="@+id/linear"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >

                    </LinearLayout>

                </HorizontalScrollView>

            </androidx.coordinatorlayout.widget.CoordinatorLayout> 

This is my Java code The function I use to check if a goal is completed:

<!-- language: lang-java -->   
 protected void checkGoal(){

            int progress = calculateProgress();

            if(goalPref.getInt("Goal",0)!=0){

                if(progress >= goalPref.getInt("Goal", 0)) {
                    int id = layout.getChildCount();
                    addNewImage(id); // <-- here I add the image

                    question.setText(R.string.setgoal);

                    updateGoalPref(0);

                    if (list.size() != 0) {
                        list = new ArrayList<>();
                    }

                    updateProgress();


                }

            }
        }

The function I use to add images:

<!-- language: lang-java -->   
            protected void addNewImage(int id){

                ImageView imageView = new ImageView(this);
                imageView.setId(id);
                imageView.setPadding(2, 2, 2, 2);
                imageView.setBackgroundResource(R.drawable.check);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                layout.addView(imageView, id);

            }

Solution

  • Since I'm pretty stubborn, I managed to find another way to do it and this is what I did: using the code I posted I added a counter to keep track of how many goals the user reached and onResume() I display as many images as the counter value.

    In this way, the user can see immediately the effect of reaching a goal and the activity doesn't lose anything.

    Then, I implemented it to show a description of the goal, changing the ImageViews in TextViews with those images as backgrounds. Instead of using a counter, I saved the strings I wanted to display in an ArrayList and used its size as a counter.

    Hope it can be useful for someone!

    <!-- language: lang-java -->
     protected void checkGoal(){
    
            int progress = calculateProgress();
    
            if(goalPref.getInt("Goal",0)!=0){
    
                if(progress >= goalPref.getInt("Goal", 0)) {
    
                    String newString = goalPref.getInt("Goal", 0) + "€, " + MainActivity.currentDate();
                    goalsCompleted.add(newString);
                    updateGoalCompletedList();
    
                    //Here I clean the layout and display all the TextViews
                    layout.removeAllViews();
                    for(int i=goalsCompleted.size()-1; i>0; i--){
                        addNewTextView(i);
                    }
    
                    question.setText(R.string.setgoal);
    
                    updateGoalPref(0);
    
                    if (list.size() != 0) {
                        list = new ArrayList<>();
                    }
                    updateProgress();
                }
            }
        }
    
    //The function used to display the TextViews
     protected void addNewTextView(int id){
    
            TextView textView = new TextView(this);
            textView.setBackgroundResource(R.drawable.check1);
            textView.setId(id);
            textView.setPadding(25,260,0,0);
            textView.setTextSize(12);
            textView.setText(goalsCompleted.get(id));
            layout.addView(textView);
        }
    
    //The function used to update and save the array
      public void updateGoalCompletedList(){
    
            Gson gson3 = new Gson();
            jsonItems3 = gson3.toJson(goalsCompleted);
            SharedPreferences.Editor listEd = goalsCompletedPref.edit();
            listEd.putString("GoalsCompleted", jsonItems3).apply();
    
        }
    
    //The function used to resume the array, you can call it on onResume() or onCreate()
      public void resumeGoalsCompleted(){
    
            Gson gson3 = new Gson();
            String savedList = goalsCompletedPref.getString("GoalsCompleted", "");
            goalsCompleted = gson3.fromJson(savedList, new TypeToken<ArrayList<String>>() {
            }.getType());
    
            if(goalsCompleted==null)
                goalsCompleted = new ArrayList<>();
    
            //From the most recent to the least recent
            for(int i=goalsCompleted.size()-1; i>0; i--){
                addNewTextView(i);
            }
        }