0
votes

I have three progress bars that should be updated to show the progress of each subject viewed in a flashcard format. Each progress bar should progress individually. I am using shared preferences to save the progress in the flashcard activity and then load that data on the activity that handles the progress bars. I have only setup my first progressbar to adjust yet all 3 progress bars adjust the same as the first.

My progressbar xml

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/introProgress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="4"
    android:progress="0"
    android:indeterminate="false"
    android:layout_gravity="center"
    android:padding="5dp"
/>

<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/howToStudyProgress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="0"
    android:indeterminate="false"
    android:layout_gravity="center"
    android:padding="5dp"
/>

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/proceduresProgress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="0"
    android:indeterminate="false"
    android:layout_gravity="center"
    android:padding="5dp"
/>

All 3 progressbars look EXACTLY alike except for the id.

Data saved in flashcard activity

private void savePosition(){
    SharedPreferences sharedPrefs = this.getActivity().getSharedPreferences("Intro", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPrefs.edit();
    editor.putInt("Intro Progress", progress);
    editor.putInt("Intro Position", position);
    editor.putBoolean("Viewed State", viewed);
    editor.commit();
}

I define my progressbars in my progressbar activity

progressBarIntro = (ProgressBar)myView.findViewById(R.id.introProgress);
progressBarStudy = (ProgressBar)myView.findViewById(R.id.howToStudyProgress);
progressBarProcedures = (ProgressBar)myView.findViewById(R.id.proceduresProgress);

I load my data from my flashcard activity to my progressbar activity

private void loadPreferences()
{
    SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("Intro", Context.MODE_PRIVATE);
    introProgressValue = sharedPreferences.getInt("Intro Progress", 0);
    introViewed = sharedPreferences.getBoolean("Viewed State", false);
}

I setup my progressbar based on the data loaded from the flashcard activity (i have only setup the intro progress bar so there is no reason I can see for my other progressbars to be changing their progress in sync with this one)

private void setupProgressBars(){
if(!introViewed){
    progressBarIntro.setProgress(0);
}
if(introViewed){
    progressBarIntro.setProgress(introProgressValue + 1);
}
progressBarIntro.setMax(4);
}

Full .xml file showing progressbars

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_alignParentTop="true"
    >

    <!--
    this goes in the above scrollview
    android:layout_above="@+id/adView"
     -->


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!-- INTRODUCTION -->
        <LinearLayout
            android:id="@+id/introView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/button_no_border_selector"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:onClick="introduction"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="INTRODUCTION"
                android:gravity="center"
                android:padding="5dp"
                />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:id="@+id/introProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:progress="0"
                android:layout_gravity="center"
                android:padding="5dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2"
                >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="0/10"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Progress"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="--"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Last Reviewed"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layoutview_background"
            />

        <!-- HOW TO STUDY -->
        <LinearLayout
            android:id="@+id/howToStudyView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@android:drawable/list_selector_background"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:onClick="howToStudy"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="HOW TO STUDY"
                android:gravity="center"
                android:padding="5dp"
                />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:id="@+id/howToStudyProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:progress="0"
                android:layout_gravity="center"
                android:padding="5dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2"
                >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="0/10"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Progress"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="--"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Last Reviewed"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layoutview_background"
            />

        <!-- BOARD PROCEDURES -->
        <LinearLayout
            android:id="@+id/boardProceduresView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@android:drawable/list_selector_background"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:onClick="boardProcedures"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="BOARD PROCEDURES"
                android:gravity="center"
                android:padding="5dp"
                />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:id="@+id/proceduresProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:progress="0"
                android:layout_gravity="center"
                android:padding="5dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2"
                >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="0/10"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Progress"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="--"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Last Reviewed"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layoutview_background"
            />

    </LinearLayout>

</ScrollView>
<!-- ADVIEW GOES HERE-->

</RelativeLayout>

I apologize for skipping a portion of my code. I am also applying a a drawable to my Progressbars

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.progress);

progressBarIntro = (ProgressBar)myView.findViewById(R.id.introProgress);
progressBarIntro.setProgressDrawable(drawable);

temp = (ProgressBar)myView.findViewById(R.id.temp);
temp.setProgressDrawable(drawable);
1
What's changing? Also post the rest ProgressBars xmls. - Onik
All 3 progressbars are in the same .xml file and they were copied from the original one but ill paste them anways. All three progressbars are changing progress in sync with the progressBarIntro even though I have not written any code to change them. - th3ramr0d
Aren't they overlap each other because of the layout_gravity="center" attribute? - Onik
they are all nested in separate linearlayouts that are positioned vertically. when I view it in design mode i double clicked each one and each one showed the correct id - th3ramr0d
What happens if you comment out the findViewById lines for the two progress bars that you haven't written code for? Do they still show progress at run time? - AndroidDev

1 Answers

0
votes

The solution: Add a different drawable for each progressbar

Resources res = getResources();
Drawable drawableIntro = res.getDrawable(R.drawable.progress);
Drawable drawableTemp = res.getDrawable(R.drawable.progress);

progressBarIntro = (ProgressBar)myView.findViewById(R.id.introProgress);
progressBarIntro.setProgressDrawable(drawableIntro);

temp = (ProgressBar)myView.findViewById(R.id.temp);
temp.setProgressDrawable(drawableTemp);