Uthpala Bandara
Uthpala Bandara

Reputation: 47

Nothing happens when i try to change TextView value dynamically in a fragment

I tried everything on the internet but still couldn't update the value. but however i can update the progress bar value. i wonder why i can update progress but not the textview value

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ProgressBar prograss_bar;
        int prograss = 28;
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        TextView prograss_perc = (TextView) view.findViewById(R.id.sale_prog_pres);
        prograss_perc.setText(String.valueOf(prograss));
        prograss_bar = (ProgressBar) view.findViewById(R.id.sales_prog);
        prograss_bar.setProgress(prograss);


        return inflater.inflate(R.layout.fragment_tab1, container, false);


    }

I get empty value for the Textview but progress bar updates. what can i do?

xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".tab1">
    <TextView
        android:id="@+id/sale_prog_pres"
        android:layout_width="84dp"
        android:layout_height="29dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@+id/sales_prog"
        app:layout_constraintEnd_toEndOf="@+id/sales_prog"
        app:layout_constraintStart_toStartOf="@+id/sales_prog"
        app:layout_constraintTop_toTopOf="@+id/sales_prog" />
    <ProgressBar
        android:id="@+id/sales_prog"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="247dp"
        android:layout_height="212dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:progressDrawable="@drawable/circle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.396" />

    <TextView
        android:id="@+id/rights"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="Powered by Sinewavesoft "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

Upvotes: 0

Views: 51

Answers (2)

Son Truong
Son Truong

Reputation: 14173

There are 2 problems with your code.

First, make sure your text color is different from background color by adding android:textColor="#FFF".

<TextView
      android:id="@+id/sale_prog_pres"
      android:layout_width="84dp"
      android:layout_height="29dp"
      android:gravity="center"
      android:background="@android:color/black"
      android:textColor="#FFF" 
      app:layout_constraintBottom_toBottomOf="@+id/sales_prog"
      app:layout_constraintEnd_toEndOf="@+id/sales_prog"
      app:layout_constraintStart_toStartOf="@+id/sales_prog"
      app:layout_constraintTop_toTopOf="@+id/sales_prog" />

Second, change onCreateView method code to

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_main, container, false);

      ProgressBar prograss_bar;
      int prograss = 28;
      TextView prograss_perc = (TextView) view.findViewById(R.id.sale_prog_pres);
      prograss_perc.setText(String.valueOf(prograss));
      prograss_bar = (ProgressBar) view.findViewById(R.id.sales_prog);
      prograss_bar.setProgress(prograss);

      // This line is IMPORTANT!!!
      return view;
}

Upvotes: 2

Tobi Daada
Tobi Daada

Reputation: 533

From the code snippet you provided, you're setting the text value of TextView prograss_perc to the string value of the object itself. It should be:

int progress = 28;
prograss_perc.setText(String.valueOf(progress));

Edit: The TextView is behind the ProgressBar. Use this instead.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
                                             android:layout_height="match_parent"
tools:context=".tab1">
    <ProgressBar
            android:id="@+id/sales_prog"
            android:background="@android:color/holo_red_dark"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="247dp"
            android:layout_height="212dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:progressDrawable="@drawable/circle"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.396" />

    <TextView
            android:id="@+id/sale_prog_pres"
            android:layout_width="84dp"
            android:layout_height="29dp"
            android:gravity="center"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="@+id/sales_prog"
            app:layout_constraintEnd_toEndOf="@+id/sales_prog"
            app:layout_constraintStart_toStartOf="@+id/sales_prog"
            app:layout_constraintTop_toTopOf="@+id/sales_prog" />

    <TextView
            android:id="@+id/rights"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="Powered by Sinewavesoft "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

Upvotes: 1

Related Questions