How to make loading screen in android - android

I am a newbie in android. I want to make an application that show dialog loading when I get data from server. I want my loading screen such as this picture below:
I don't know any key to searching to make this loading :(. Is This application show in the picture using progress bar with animation?. I have some picture when I extract apk file of this application and I have some pictures as below:

try this.
in run() put code which rewrite your progressbar...
if you want stop timer. use timer.cancel();
and for example start faster timer for finish progress..
imer timer= new Timer();
timer.schedule(
new TimerTask() {
#Override
public void run() {
runOnUiThread(
new Runnable() {
#Override
public void run() {
**thise code run every 100ms**
}
}
);
}
}, 0, 100);
it's simply, if you want correctly resolve thise problem use Threats.

Have you tried using AsyncTask?
You should look on Android Threading concepts. and as mentioned remember runOnUiThread for calls that updates your UI.
You should extend a progressbar and handle the images position on it...
Here is the AsyncTask example code:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}

Related

Android: How to have a background and UI thread run at the same time?

I need to have a network process running on a non-UI Thread and a UI thread running at the same time to inform the user that the process is running, and that the app isn't frozen, and won't allow the next block of code to be executed until the network connection gives its response.
What would the best way to go about doing this?
AsyncTasks are the way to go.
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}

Android doesn't show ProgressDialog

I'm trying to show a ProgressDialog when loading an Activity, but it doesn't show up.
Here is the method called on Activity onCreate
private void loadBuilding(String[] b) {
ProgressDialog pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(6);
pd.setTitle(R.string.loading);
pd.show();
LoadBuilding lb = new LoadBuilding();
lb.initialize(this, pd);
lb.execute(b);
try {
lb.get();
} catch (Exception e) {
e.printStackTrace();
}
pd.dismiss();
if (building == null)
showError();
}
The LoadBuilding is an AsyncTask in which I load the building and set the progress.
Thanks to all.
Problem was that the progressDialog.dismiss() must be in:
The catch of the try/catch showed in the code
The onPostExecute method of AsyncTask.
Also I'm using too few data for testing, so it comes up and down too fast.
Use AsyncTask for Background processing and Updating Progressing in Foreground. I think that best suited your task.
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Try to add your progressDismiss() in try and catch block
try
{
pg.dismiss();
}
Catch()
{
}

Determine that thread is running or not if using runnable in android

I have created a program in android for multithreading.
When I hit one of the button its thread starts and print value to EditText now I want to determine that thread is running or not so that I can stop the thread on click if it is running and start a new thread if it is not running here is mu code:
public void startProgress(View view) {
final String v;
if(view == b1)
{
v = "b1";
}
else
{
v = "b2";
}
// Do something long
Runnable runnable = new Runnable() {
#Override
public void run() {
//for (int i = 0; i <= 10; i++) {
while(true){
if(v.equals("b1"))
{
i++;
}
else if(v.equals("b2"))
{
j++;
}
try {
if(v.equals("b1"))
{
Thread.sleep(3000);
}
else if(v.equals("b2"))
{
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
// progress.setProgress(value);
if(v.equals("b1"))
{
String strValue = ""+i;
t1.setText(strValue);
}
else
{
String strValue = ""+j;
t2.setText(strValue);
}
//t1.setText(value);
}
});
}
}
};
new Thread(runnable).start();
}
#Override
public void onClick(View v) {
if(v == b1)
{
startProgress(b1);
}
else if(v == b2)
{
startProgress(b2);
}
}
Instead of that messy code, an AsyncTask would do the job you need with added readability ...
It even has a getStatus() function to tell you if it is still running.
You'll find tons of examples by looking around a bit (not gonna write one more here). I'll simply copy the one from the documentation linked above:
Usage
AsyncTask must be subclassed to be used. The subclass will override at least one method (doInBackground(Params...)), and most often will override a second one (onPostExecute(Result).)
Here is an example of subclassing:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Once created, a task is executed very simply:
new DownloadFilesTask().execute(url1, url2, url3);
Use a static AtomicBoolean in your thread and flip its value accordingly. If the value of the boolean is true, your thread is already running. Exit the thread if it is true. Before exiting the thread set the value back to false.
There are some way can check the Thread properties
You able to check Thread is Alive() by
Thread.isAlive() method it return boolean.
You able to found runing thread run by
Thread.currentThread().getName()
Thanks

How to wait for code executing in Android? Runnable failed for me

I want to wait inside my while loop for like 1 second each time it is inside the loop.
I tryed with Runnable:
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
int i=0;
while(i<10){
if(i%2 ==0){
myTextView.setText("true");
}
else{
myTextView.setText("false");
}
i++;
}
}
}, 1000);
If i ran this code i got a blank display for minutes and then a crash.
Logcat:
ActivityManager Reason: keyDispacthingTimeot
What is the problem?
I would like to make this as simple as it can be...
Please help!
If you want to make sthg very simple, then just use
Thread.currentThread();
Thread.sleep(1000);
Note: as Adeel said, do not sleep in a UI thread =)
EDIT: you should implement sthg like this:
I think you want to use a asyncTask, and publishprogress each one sec. So make your own asyncTask<Void, Integer, Void>, and on the doInbackground method:
protected Void doInBackground(Void... params) {
int i=0;
while(i<10){
Thread.currentThread();
try{
Thread.sleep(1000);
}catch(Exception e){
return null;
}
publishProgress(new Integer(i));
i++;
}
}
Then implement the onProgressUpdate method which runs on UI thread, and:
protected void onProgressUpdate(Integer... progress) {
int i = progress[0].intValue();
if(i%2 ==0){
myTextView.setText("true");
}
else{
myTextView.setText("false");
}
}
Here is the doc about asynctasks
Try adding this in the while loop
Thread.sleep(1000);

How to make a text animated in android?

In my android application I am trying to show a "Loading..." text which will change every 100 ms. After every 100 milliseconds it will increase one dot. So first it will be like "Loading." and the after another 100 ms it will be "Loading.." When it will be "Loading..." , this process will terminate and again start from the first on words. It will continue till 3500 ms. It will be pretty like progress bar.
I hope I am able to explain the problem.
How to resolve this problem? Please help.
Not the best answer, but it works.
Handler handler = new Handler();
for (int i = 100; i <= 3500; i=i+100) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i%300 == 0){
textView.setText("Loading.");
}else if(i%200 == 0){
textView.setText("Loading..");
}else if(i%100 == 0){
textView.setText("Loading...");
}
}
}, i);
}
Kotlin Snippet for Animating (Loading...):
ValueAnimator.ofInt(0, 4).apply {
repeatCount = 10
duration = 1000
addUpdateListener { valueAnimator ->
val dotsCount = valueAnimator.getAnimatedValue() as Int
if (dotsCount < 4) {
spannable.setSpan(transparentColorSpan, 7 + dotsCount, 10,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textView.invalidate()
}
}
}.start()
A CountdownTimer seems to fit, change the text in onTick.
You should use an AsyncTask. This class is meant to do long-running background tasks that will publish updates and run on the UI thread automatically.
In doInBackground, have your loop that will then call publishProgress which will call onProgressUpdate every 100ms with the new values.
This will be done in the background and onProgressUpdate will run on the UI thread for you automatically.
Something on the lines of:
private class ShowLoading extends AsyncTask<Void, Integer, Void> {
#Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < 3500; i++){
publishProgress(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
if (values[0]%3 == 0){
textview.setText ("Loading.");
} else if (values[0]%3 == 1){
textview.setText ("Loading..");
} else if (values[0]%3 == 2){
textview.setText ("Loading..");
}
}
}

Categories

Resources