Is it possible to use a progress bar in Crouton? - android

I want to show custom Crouton alert with a progress bar and I want to update the progress dynamicly. This is my code but it doesn't work. How can i do that? Or how can show progress bar while image uploading
private void showCustomViewCrouton() {
// try {
View view = getLayoutInflater().inflate(R.layout.crouton_custom_view,null);
pB = (ProgressBar) view.findViewById(R.id.progressBar1);
final Crouton crouton;
final Configuration croutonConfig = new Configuration.Builder()
.setDuration(Configuration.DURATION_INFINITE)
.setInAnimation(android.R.anim.fade_in)
.setOutAnimation(android.R.anim.fade_out).build();
crouton = Crouton.make(this, view, R.id.croutonContainer, croutonConfig);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
crouton.hide(crouton);
}
});
crouton.show();
try{
do{
Thread.sleep(100);
deger+=1;
pB.setProgress(deger);
}while(deger<101);
}catch(Exception e){
}
}

Here is how I did it
final ProgressBar progressBar = new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal);
progressBar.setIndeterminate(false);
Crouton ct = Crouton.make(this,progressBar);
ct.show();
/* Update it While Showing */
Thread progressUpdateThread = new Thread(new Runnable()
{
#Override
public void run()
{
for(int i=0;i<100;i++)
{
final int progress = i;
runOnUiThread(new Runnable()
{
#Override
public void run()
{
progressBar.setProgress(progress);
}
});
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
});
progressUpdateThread.start();

i want to share my new code. I inflate a custom Crouton. I wrote this with using #Kirtan Patel ' s code. thanks him again.
private void showCustomViewCrouton() {
// try {
View view = getLayoutInflater().inflate(R.layout.crouton_custom_view,null);
pB = (ProgressBar) view.findViewById(R.id.progressBar1);
final Crouton crouton;
final Configuration croutonConfig = new Configuration.Builder()
.setDuration(Configuration.DURATION_INFINITE)
.setInAnimation(android.R.anim.fade_in)
.setOutAnimation(android.R.anim.fade_out).build();
crouton = Crouton.make(this, view, R.id.croutonContainer, croutonConfig);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
crouton.hide(crouton);
}
});
crouton.show();
try{
Thread progressUpdateThread = new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < 100; i++) {
final int progress = i;
runOnUiThread(new Runnable() {
#Override
public void run() {
pB.setProgress(progress);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
progressUpdateThread.start();
}catch(Exception e){
}
}

Related

Progressbar update causing a black screen

I am trying to update the progressbar, but it is showing a black screen whenever I use progressbar. As soon as I comment the part of progressbar increment, my code works fine. Could someone please help me out with what am I doing wrong?
new Runnable() {
#Override
public void run() {
for(int i = 0; i < timeout; i++) {
handler.post(new Runnable() {
#Override
public void run() {
progressbar.incrementProgressBy(1);
}
});
try {
Thread.sleep(1000);
}
catch(Exception e) {}
}
}
}.run();
using thread you can do like this...
public void loadProgressbar()
{
progressBar.setMax(100);
progressBar.setProgress(0);
new Thread(new Runnable() {
#Override
public void run() {
for (progress = 0; progress <= 100; progress++) {
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progress);
}
});
try {
Thread.sleep(50);
} catch (Exception ex) {
}
}
}
}).start();
}
Just use this code on your main thread .
new CountDownTimer(3000, 300) {
#Override
public void onTick(long l) {
progress = progress + 10;
pb.setProgress(progress);
}
#Override
public void onFinish() {
pb.setProgress(100);
}
}.start();

Android update ProgressBar with Handler

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..

Android update TextView inside dialog in runOnUiThread

I have been spending couple hours to try to update the textview inside the dialog, but failed.
When the option is clicked, there are new dialog is shown, and inside the dialog, there are textviews and button, when I click the button, the textview will be update.
Here is the code related to the button onClick listener:
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (int i = 0; i < 50 ; i ++){
final String currentNum = String.valueOf(i + 1);
Thread t = new Thread() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(currentNum); //it is fine
currentNum.setText(currentNum); //it is the problem, the setText only work when the for loop is finished.
}
});
}
};
t.start();
}
}
});
Please let me know if you need more information. Thanks a lot in advance!
//it is a optionmenu
case R.id.action_refresh:
final TextView currentNum;
final ImageButton start;
String currentNum = Integer.toString(songList.size());
final Dialog lyricsAnalysis = new Dialog(this,R.style.cust_dialog);
lyricsAnalysis.requestWindowFeature(Window.FEATURE_NO_TITLE);
lyricsAnalysis.setContentView(R.layout.analysis);
lyricsAnalysis.setCancelable(true); //back button to cancel
lyricsAnalysis.setCanceledOnTouchOutside(true);
start = (ImageButton) lyricsAnalysis.findViewById(R.id.start);
//first value
currentNum.setText(String.valueOf(currentNum));
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (int i = 0; i < 50 ; i ++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateTextView(lyricsAnalysis,i);
}
}
});
lyricsAnalysis.show();
lyricsAnalysis.getWindow().setLayout(600, 1000);
break;
}
return super.onOptionsItemSelected(item); }
public void updateTextView(Dialog dialog, int i) {
final TextView currentNum = (TextView) dialog.findViewById(R.id.currentNum);
currentNum.setText(Stri`enter code here`ng.valueOf(i));
//return;
}
Try this method. This may helps you. It's work for me.(But I am not use this in dialog)
public void updateTextView(String toThis) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
//return;
}
try like this
int elapsedtime=0;
boolean isTimerRunning=false;
Timer timerr;
inside onCreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//declare your textview here;
timerr=new Timer();
startTimer();
}
/*
* timer for displaying message bubble
*/
protected static void startTimer() {
isTimerRunning = true;
elapsedtime = 0;
// recordingseek.setProgress(0);
timerr.scheduleAtFixedRate(new TimerTask() {
public void run() {
// increase every sec
elapsedtime++;
mmHandler.obtainMessage(1).sendToTarget();
System.out.println("recording time" + elapsedtime);
if(elapsedtime==50)
timerr.cancel();
}
}, 1000, 2000);
};
public static Handler mmHandler = new Handler() {
public void handleMessage(Message msg) {
textview.setText(elapsedtime);
}
};
}
};

Horizontal ProgressDialog not shown in android

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();
}

Custom Adapter Listview Android

I hit my head against the wall, trying to solve this problem for some time. I'm not an experienced Android guy, so its solution might be really simple. That being said, I have the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.adminusers);
super.onCreate(savedInstanceState);
// set user_type for Viewall
mUserType = 0;
lv_adminusersListContent.addFooterView(footerView);
mUsersAdapter = new AdminUsersAdapter(mUsersList, AdminUsers.this);
lv_adminusersListContent.setAdapter(mUsersAdapter);
getUsersList();
}
private void getUsersList() {
if (UsefulFcts.isOnline(AdminUsers.this)) {
if (mThread != null && mThread.isAlive()) {
mThread.interrupt();
}
mThread = new Thread() {
#Override
public void run() {
mHandler.post(new Runnable() {
#Override
public void run() {
pDialog = new ProgressDialog(AdminUsers.this);
pDialog.setMessage("Loading.. Please wait!");
pDialog.setCancelable(false);
pDialog.show();
}
});
mUsersList.clear();
mUsersList.addAll(mServiceManager.getUsersList(mUserType,
0 + "", AdminServiceManager.sHowManyToLoad));
mArrayLength = mUsersList.size();
Log.d("arrayLength", String.valueOf(mArrayLength));
Log.d("total_results",
String.valueOf(ParserJSON.total_results));
mHandler.post(new Runnable() {
#Override
public void run() {
// refresh listview
mUsersAdapter.notifyDataSetChanged();
if (mArrayLength < ParserJSON.total_results) {
lv_adminusersListContent
.addFooterView(footerView);
} else if (mArrayLength >= ParserJSON.total_results) {
lv_adminusersListContent
.removeFooterView(footerView);
}
if (0 == mArrayLength) {
Toast.makeText(AdminUsers.this,
"No username found!",
Toast.LENGTH_SHORT).show();
}
pDialog.dismiss();
}
});
}
};
mThread.start();
} else {
Toast.makeText(AdminUsers.this, "Internet connection required!",
Toast.LENGTH_LONG).show();
}
}
#Override
protected void linkUI() {
// begin load_more view
footerView = AdminUsers.this.getLayoutInflater().inflate(
R.layout.loadmore, null);
btn_loadmore = (Button) footerView.findViewById(R.id.btn_loadmore);
// end load_more view
btn_adminusersViewall = (Button) findViewById(R.id.btn_adminusersViewall);
btn_adminusersAdmins = (Button) findViewById(R.id.btn_adminusersAdmins);
btn_adminusersClients = (Button) findViewById(R.id.btn_adminusersClients);
btn_adminusersDevs = (Button) findViewById(R.id.btn_adminusersDevs);
btn_adminAdd = (ImageButton) findViewById(R.id.btn_adminAdd);
lv_adminusersListContent = (ListView) findViewById(R.id.lv_adminusersListContent);
}
#Override
protected void setAction() {
// begin Buttons UnderHeader
btn_adminusersViewall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
UsefulFcts.setButtonClicked(btn_adminusersViewall,
btn_adminusersClients, btn_adminusersAdmins,
btn_adminusersDevs);
mUserType = 0;
mArrayLength = -1;
lv_adminusersListContent.removeFooterView(footerView);
getUsersList();
}
});
btn_adminusersClients.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
UsefulFcts.setButtonClicked(btn_adminusersClients,
btn_adminusersDevs, btn_adminusersAdmins,
btn_adminusersViewall);
mUserType = 1;
mArrayLength = -1;
// view.setVisibility(View.GONE);
getUsersList();
}
});
btn_adminusersDevs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
UsefulFcts.setButtonClicked(btn_adminusersDevs,
btn_adminusersClients, btn_adminusersAdmins,
btn_adminusersViewall);
mUserType = 2;
mArrayLength = -1;
lv_adminusersListContent.removeFooterView(footerView);
getUsersList();
}
});
btn_adminusersAdmins.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mUserType = 3;
mArrayLength = -1;
lv_adminusersListContent.removeFooterView(footerView);
getUsersList();
}
});
// end Buttons UnderHeader
btn_loadmore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mThread2 = new Thread() {
#Override
public void run() {
mHandler.post(new Runnable() {
#Override
public void run() {
progressBar = new ProgressDialog(
AdminUsers.this);
progressBar
.setMessage("Loading more projects.. ");
progressBar.setCancelable(true);
progressBar.show();
}
});
mUsersList.addAll(mServiceManager.getUsersList(
mUserType, String.valueOf(mArrayLength),
AdminServiceManager.sHowManyToLoad));
mArrayLength = mUsersList.size();
mHandler.post(new Runnable() {
#Override
public void run() {
mUsersAdapter.notifyDataSetChanged();
if (mArrayLength >= ParserJSON.total_results) {
lv_adminusersListContent.removeFooterView(footerView);
}
progressBar.dismiss();
}
});
};
};
mThread2.start();
}
});
Note: I basically have a listview which content is requested from a server depending on which button (looking like a tab) I'm pressing (mUserType = 0/1/2/3). I also use a Load More button (footerView = simple layout with a button).
Everything seems to work just fine, but after "playing" with the buttons a few times (random number!) it crashes, with the following exception thrown:
02-21 11:00:49.252: E/AndroidRuntime(11728): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131296461, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]
So, I looked back at the code, but couldn't see any "not on UI thread" listview notification.
1. What could the problem be?
2. Is it necessary to use 2 threads (mThread and mThread2), or could I use the same one, as one's work should start when the other ends (or isn't it ?) and should I declare them static or not ?
Also pointing me to any "Good practices" tricks I could use in the future, or "Wrong practices" that I did would be much appreciated!
PS. linkUI() and setAction() are called in super().onCreate().
The error message seems understandable : you are modifying the adapter from mThread:
mUsersList.clear();
mUsersList.addAll(mServiceManager.getUsersList(mUserType,
0 + "", AdminServiceManager.sHowManyToLoad));
You can try something like this:
final List newUserList = mServiceManager.getUsersList(mUserType,
0 + "", AdminServiceManager.sHowManyToLoad);
mArrayLength = newUserList.size();
Log.d("arrayLength", String.valueOf(mArrayLength));
Log.d("total_results",
String.valueOf(ParserJSON.total_results));
mHandler.post(new Runnable() {
#Override
public void run() {
mUsersList.clear();
mUsersList.addAll(newUserList);
// refresh listview
mUsersAdapter.notifyDataSetChanged();

Categories

Resources