Progressbar in for loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Progressbar in for loop



I'm trying this code but, just 3rd progress Bar updated.
i want to know what is my mistake in the code?




package com.yasi.testview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity
LayoutInflater inflater;
LinearLayout scrollContent;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

scrollContent = findViewById(R.id.scrollContent);

for (int i=1; i<4; i++)
View view = inflater.inflate(R.layout.test_item,scrollContent);
ProgressBar progressBar = findViewById(R.id.prgDownload);
TextView textView = findViewById(R.id.txtDownload);

progressBar.setProgress((int) (Math.random() * 100));
textView.setText("Download # " + i);







and this a picture of my project:
Project screen Shot



a scroll view is in main activity layout by this code:




<?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=".MainActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/scrollContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>



and my test_item layout is code below that contains a progress bar and a text view:




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ProgressBar
android:id="@+id/prgDownload"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="50dp" />

<TextView
android:id="@+id/txtDownload"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="..." />
</RelativeLayout>



thank you for your answers.





Post your activity layout file. I can see the issue but would explain better with the xml.
– varunkr
7 hours ago





You're only changing one ProgressBar in your code.
– TheWanderer
7 hours ago




2 Answers
2



First of all don't get new instances of elements in loop again and again it is a poor approach as resolved in snippet below, secondly you are just using one progress bar over and over again and updating it on same thread, u need to have more progress bars in xml and also you can generate dynamically and then u can run the loop and update accordingly.


package com.yasi.testview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity
LayoutInflater inflater;
LinearLayout scrollContent;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
**View view = inflater.inflate(R.layout.test_item,scrollContent);
ProgressBar progressBar = findViewById(R.id.prgDownload);
TextView textView = findViewById(R.id.txtDownload);**
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

scrollContent = findViewById(R.id.scrollContent);

for (int i=1; i<4; i++)
progressBar.setProgress((int) (Math.random() * 100));
textView.setText("Download # " + i);








Declare widgets-Views outside of loop and use this code:


@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_item, scrollContent);

scrollContent = findViewById(R.id.scrollContent);
ProgressBar progressBar = findViewById(R.id.prgDownload);
TextView textView = findViewById(R.id.txtDownload);

for (int i=0; i < 3; i++)

progressBar.setProgress((int) (Math.random() * 100));
textView.setText("Download # " + i);






As you can see, you're making changes to only ProgressBar with prgDownload id so, you'll probably need to declare more ProgressBars in your project.


ProgressBar


prgDownload


ProgressBar






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS