I have two class. class A is activity where my progress bar will be use. and class B is no-activity where my progress bar will be update. but when i calling progress bar from non-activity class B . i got null pointer exception.
class A:-
ProgressBar progressBar;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);}
class B:-
new Thread(new Runnable() {
int i = 0;
int progressStatus = 0;
public void run() {
while (progressStatus < 100) {
progressStatus += doWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
((Activity) cnt).runOnUiThread(new Runnable() {
public void run() {
XMPPClient xc = new XMPPClient();
xc.progressBar = new ProgressBar(cnt);
xc.progressBar.setProgress(progressStatus);
// Toast.makeText(cnt, "ok", Toast.LENGTH_SHORT).show();
i++;
}
});
}
}
private int doWork() {
return i * 3;
}
}).start();
when i added this line :- xc.progressBar = new ProgressBar(cnt);
then i did not get nullpointerexception. But now my progress bar is **not updating.**
please any one help me.
ProgressBar progressBar;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
ClassB classb = new ClassB(this, progressBar);
}
public class ClassB{
private Context cnt;
private ProgressBar progressBar;
public ClassB(Context context, ProgressBar pBar){
cnt = context;
progressBar = pBar;
}
}
Now you can use progressBar instead of creating a new one like you did in previous code!
Just a quick overview...
in ClassA :-
ProgressBar progressBar;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
////Then Use the Reference on Progressbar
ClassB classb = new ClassB(this, progressBar);
}
Then in ClassB :-
public class ClassB{
private Context cnt;
private ProgressBar progressBar;
public ClassB(Context context, ProgressBar pBar){
cnt = context;
progressBar = pBar;
new Thread(new Runnable() {
int i = 0;
int progressStatus = 0;
public void run() {
while (progressStatus < 100) {
progressStatus += doWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
((Activity) cnt).runOnUiThread(new Runnable() {
public void run() {
XMPPClient xc = new XMPPClient();
xc.progressBar = new ProgressBar(cnt);
xc.progressBar.setProgress(progressStatus);
// Toast.makeText(cnt, "ok", Toast.LENGTH_SHORT).show();
i++;
}
});
}
}
private int doWork() {
return i * 3;
}
}).start();
}
Related
I've used a splash screen and progress bar in my Android App.But after splash disappears,then next screen becomes black before switching to my main activity. But I don't want black screen.Can anyone please explain what's going on here and how can I prevent that black screen ? This is my Splash Java class.
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
startApp();
finish();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
I think you could remove the finish() inside the Thread.
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
startApp();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
Approach 1:
If you required splash for few seconds:
new Thread((new Runnable() {
#Override
public void run() {
try { Thread.sleep(5500); }catch(Exception e) {}
startApp();
finish();
}
}
)).start();
Approach 2: Show next screen when splash finish:
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
startApp();
finish();
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
Try this one:
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
startApp();
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
finish();
}
}
I would like to update the progressBar with Handler and for loop but without success.
Code:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(progress_k, 100);
}
}, 2000);
}
}
Question:
The progress bar increase immediately to the end value instead of progressively.
Why?
Try this:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
final int curr_progress_k = progress_k;
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(curr_progress_k, 100);
}
}, progress_k * 100); // adjust "100" value to adjust speed
}
}
Repeat a task with a time delay?
#inazaruk
private ProgressBar progressBar;
private Handler mHandler;
private int progressInt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
progressBar = (ProgressBar) findViewById(R.id.pb);
progressBar.setProgress(0);
mHandler = new Handler();
runnable.run();
}
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
updateProgress();
} catch (Exception ignored) {
} finally {
mHandler.postDelayed(runnable, progressInt);
}
}
};
private void updateProgress() {
progressInt += 1;
if (progressInt > 100) {
mHandler.removeCallbacks(runnable);
} else {
progressBar.setProgress(progressInt);
}
}
try this code:
Solution 1
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler();
class Task implements Runnable {
int start,end;
Task(int a,int b) { start = a; end = b;}
#Override
public void run() {
for (int i =start ; i <= end; i++) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler1.post(new Runnable() {
#Override
public void run() {
progressBar.setProgress(value);
}
});
}
}
}
Thread t = new Thread(new Task(from, to)); //call it
t.start();
}
Solution 2: More Simple
If thread is too much to ask for this problem..
you can use the following solution to use a single Handler to update progressbar:
code
public class HandlerDemo extends Activity
{
ProgressBar bar;
Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
bar.incrementProgressBy(5);
}
};
boolean isRunning = false;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
bar = (ProgressBar) findViewById(R.id.progress);
}
public void onStart()
{
super.onStart();
bar.setProgress(0);
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
for (int i = 0; i < 20 && isRunning; i++)
{
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
}
catch (Throwable t)
{
// just end the background thread
}
}
});
isRunning = true;
background.start();
}
public void onStop()
{
super.onStop();
isRunning = false;
}
}
Hope it helps..
I have the code bellow when I click button it display a progress bar dialog.
public class MyAndroidAppActivity extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
btnStartProgress.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
}
});
}
// file download simulator... a really simple
public int doSomeTasks() {
Intent intent = new Intent(MyAndroidAppActivity.this, Accueil.class);
startActivity(intent);
}
}
But I want to display it(progress bar dialog) in start of my android application.
How can I do this ? Thanks in advance for any help.
I have the MainActivity as this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bo = new Operation(getApplicationContext());
}
public void op_perform(View v) throws Exception { //call this when a button is pressed
try {
bo.demo();
} catch (Exception e) {
// TODO: handle exception
}
The Operation class has the following lines of code:
Context con;
Operation(Context ac) {
con = ac;
barProgressDialog = new ProgressDialog(con);
updateBarHandler = new Handler();
}
public void demo() {
barProgressDialog = new ProgressDialog(con);
barProgressDialog.setTitle("Downloading Image ...");
barProgressDialog.setMessage("Download in progress ...");
barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);
barProgressDialog.setProgress(0);
barProgressDialog.setMax(20);
barProgressDialog.show();
...
But, i'm not getting the progress dialog at all in my screen. I want the progress dialog to be a determinate horizontal one. This is just a piece of code and i need to put these in the Asyn task. But the processdialog is not even showing. Recommended solutions. I'm a beginner to android.
Here is a complete example of what you need: http://turboprogramacion.blogspot.cl
private ProgressDialog barraProgreso;
private Handler handler;
private void mostrarBarraProgreso(){
barraProgreso = new ProgressDialog(MainActivity.this);
barraProgreso.setTitle("Buscando...");
barraProgreso.setMessage("Progreso...");
barraProgreso.setProgressStyle(barraProgreso.STYLE_HORIZONTAL);
barraProgreso.setProgress(0);
barraProgreso.setMax(10);
barraProgreso.show();
handler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
try {
while (barraProgreso.getProgress() <= barraProgreso.getMax()) {
Thread.sleep(1000);
handler.post(new Runnable() {
#Override
public void run() {
barraProgreso.incrementProgressBy(1);
}
});
if(barraProgreso.getProgress() == barraProgreso.getMax()){
barraProgreso.dismiss();
}
}
}catch (InterruptedException er){
er.printStackTrace();
}
}
}).start();
}
Below is my code that is not working and i dont understand why it says "Only the original thread that created a view hierarchy can touch its views." when i want to set the adapter for my listview inside LoadValues() method.. "listView.setAdapter(adapter)"
public class ProgressDialogActivity extends Activity {
private ProgressDialog progressDialog;
private ListView listView;
private ArrayList<String> lstValores;
private Handler progressBarHandler = new Handler();
private int mProgressStatus, mProgressTotal;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lstValores = new ArrayList<String>();
listView = (ListView) findViewById(R.id.listView1);
this.LoadValues();
}//onCreate
public void LoadValues(){
progressDialog = new ProgressDialog(ProgressDialogActivity.this);
progressDialog.setTitle("Aguarde..");
progressDialog.setMessage("Cargando lista..");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
progressDialog.show();
new Thread(new Runnable() {
public void run() {
mProgressTotal = (int) 1000;
try {
for (int i = 0; i < mProgressTotal; i++) {
lstValores.add("valor " + i);
mProgressStatus = (int) ((i * 100) / mProgressTotal);
progressBarHandler.post(new Runnable() {
public void run() {
progressDialog.setProgress(mProgressStatus);
}
});
}// end for
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ProgressDialogActivity.this,
R.layout.textviewlist_layout,
lstValores);
listView.setAdapter(adapter);
progressDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}// end run
}).start();// end Thread
}// end LoadValues
} // Activity
In android, Only the UI Thread can touch the View.
So
progressBarHandler.post(new Runnable() {
public void run(){
listView.setAdapter(adapter);
}
}