When I load image and text, block click tab several seconds, why? - android

I do this in the create method on the activity for the second tab
public void infoTabs(){
//tab1: it works well
.......
// tab 2:
image.setImageResource(panel.getPhoto());
String infoCasilla = panel.getInfoText();
informacionCasilla.setText(infoCasilla);
}
Then when I press the second tab, it takes 3-4 seconds to load the image and text and is locked up showing this content tab.
Can you think of any idea as to prevent it from being blocked those seconds?
thank you very much.

If your photo large , it could take a lot of time (i.e. 3-4 sec) to load image into imageButton(or ImageView,whatever you using). You should load it in worker(background) thread, not in ui thread (like you do it for now). So use Separate Thread like this:
new Thread(new Runnable() {
public void run() {
image.post(new Runnable(){
public void run() {
image.setImageResource(panel.getPhoto());
}
}
}
}).start();
Or use Picasso or Fresco libs, that load image in separate thread for you.

Related

ProgressBar running faster after each onclick in android

I am Stuck here with this application in android. In my application i am trying to implement a progressbar which shows timer for certain seconds. When the Button is clicked the timer should refresh and again start from 0 in progressBar. For this I am using Thread.
The Problem is, When I Click the button the Thread calls the timer function and each time the thread is getting faster and faster. I couldn't resolve it and not having any idea what is going in background.
This is my code for Timerfunction
public void setTimer()
{
prog=0;
progress.setProgress(prog);
if(flag){
t= new Thread(new Runnable(){
public void run()
{
while(prog<100)
{
prog+=1;
handle.post(new Runnable(){
public void run()
{
progress.setProgress(prog);
if(prog==progress.getMax()&& flag){
call_fun();
}
}
});
try
{
Thread.sleep(time);
}
catch(InterruptedException e)
{
Log.i("Error", null);
}
}
}
});
t.start();
}
}
I called this function in another function called RandomGeneration. If the button is clicked the randomgeneration is called and the set timer is activated everytime. But the progressbar is running faster after every click. It is constantly running in the same specific time. For example if it runs for 3 seconds in the first click, its running 2 seconds in the second click and getting faster considerably.
Can anyone please try to find what is happening in this code.
Thanks in advance..!!
From what I see a new Thread is being created everytime you click the button.
Maybe try to check if t is already running and if so update it's logic to set progress to 0?
Also, what does if(flag) do?

Android Development- Change gridview images continously after 2 sec- How can I use handler to achieve this task?

I have a question regarding grid views in android.
I have a grid of 5x5 images. And I want to change these images continously. Loads new set of images from the array every time.
I have a random generator function which changes the value of the mThumbIds array after it loads every time.
But I am unable to apply the new images before it needs some event to render the new set of images. Here I do not have any event. I want them to change continuously.
Can you please me.
Unable to find any solution for this.
Did you try using an AsyncTask to change your images ? The AsyncTask could change one image, then launch a new AsyncTask.
An other option is to use a Handler. That may be your best option actually. http://mobileorchard.com/android-app-developmentthreading-part-1-handlers/
Do you mean a timer to trigger the image changing function every 2 seconds?. If so try a Handler. Something like
private Handler mHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {
// init grid and set first batch of images
}
protected void onResume() {
mHandler.postDelayed(mUpdateImagesTask , 2000);
}
private Runnable mUpdateImagesTask = new Runnable() {
public void run() {
// Code to change the images
// call me in 2 seconds...
mHandler.postDelayed(this, 2000); // 2 seconds
}
};
If you want to stop the loop just put some conditions to call Handler.postDelayer() inside mUpdateImagesTask or call mHandler.removeCallbacks(mUpdateImagesTask) from outside the Runnable (at a button click, i.e).

set new content view after running thread

I'm creating a startup activity for an application that downloads and parses some data. While it is loading i want to view my startup_loading.xml file, which contains a progressbar and a textview which is set to "loading".
Everything i need to do in the startup is put in a thread, the startUp thread. After running this thread the data is loaded, and then i want to show a new view: startup_loaded.xml which contains two buttons which send you to different activities in the application.
But i have troubles using two views and displaying them at the time i want to.
I thought that if i put the
setContentView(R.layout.startup_loaded);
at the end of the runnable of my thread it would work, but i get an error that that is impossible
CalledFromWrongThreadException: Only the original thread that created
a view hierarchy can touch its views.
I thought i could fix it like this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
startUp.start();
String language = Locale.getDefault().getISO3Language();
if (startUp.isAlive()) {
setContentView(R.layout.startup_loading);
tv1=(TextView)findViewById(R.id.tvloading);
if(language.equals("nld")) {
tv1.setText("bijwerken");
} else {
tv1.setText("loading");
}
pbar=(ProgressBar)findViewById(R.id.progressBar1);
pbar.setVisibility(1);
} else {
setContentView(R.layout.startup_loaded);
}
}
Thread startUp= new Thread() {
public void run() {
// all the loading
}
};
But this didn't work either: my startup_loaded never appeared.
I tried finishing my thread (finish(); at the end of the runnable), but it finished by total app. Anyone knows what to do?
Use Handler object. Or, use AsyncTask and set new contrent in onPostExecute() method.

Dynamic ImageView in android

I'm a newbie in android so please don't judge me hard.
I'm trying to create a screen with two areas, an image area and a text area.
In the image area several images should be changed within a periodical time, 3-4 seconds. (Please see the image: )
Can you please give me an example how can I accomplish this?
Thank you.
You can use CountDownTimer class for this.
Or you can use a Gallery with Thread and Handler to achieve this.
Here is a link to the project called AutoSlideGallery,
https://github.com/nixit28/AutoSlideGallery
The core logic is here,
(new Thread() {
public void run() {
myslideshow();
handler.postDelayed(this, 2000); // execute every two second.
}
}
).start();
And the method which performs the action,
private void myslideshow() {
PicPosition = gallery.getSelectedItemPosition() + 1;
if (PicPosition >= pics.length)
gallery.setSelection(0); // stop
else
gallery.setSelection(PicPosition);// move to the next gallery
// element.
}

execute the runnable in a continuous loop

My code mainly decodes a series of bitmap objects to be displayed in an ImageView later. To get more control over the duration to display each image. So i implemented the code in a Runnable. Here is a snippet of that runnable.
final File file[]=dir.listFiles();
iv.postDelayed(
new Runnable() {
#Override
public void run() {
i++;
Bitmap bitmap = decodeFile(file[i]);
iv.setImageBitmap(bitmap);
iv.postDelayed(this, 5000);
}
}, 5000);
However, I don't seem to have a good control over the looping here. I am not able to use a for loop since "i" cant be assigned to zero in this case. This code works as long as the "file[i]" has a File reference to it. As soon as it displays all the images once, the app just force closes do to arrayindexoutofbounds exception. I just want to continue the loop infinitely. Any pointers?
Here decodeFile() calls another function to rescale the size of the bitmap.
I'm not really sure what you are doing, but why not use a while loop instead? Something like while (true) { } would cause the infinite loop.
sense you want to do some work that might take a while and you want do repeat it, try looking at an android component called service and send it the images by intent.
http://developer.android.com/guide/topics/fundamentals/services.html
probably an IntentService would be fine, and help alot
http://developer.android.com/reference/android/app/IntentService.html
I suggest an AsyncTask
File means a file array is given the task to be processed in the background.
Bitmap means you call publishProgress with your bitmap, you fetch it on the UI thread in onProgressUpdate
Void means you simply stop once it is done.
The good thing here is that the decoding is done on a background thread, but the iv.setBitmap is still called on the UI thread, which is mandatory.
Just reset to zero once you reach the end:
final File file[]=dir.listFiles();
iv.postDelayed(
new Runnable() {
#Override
public void run() {
if (i == file.length) {
i = 0;
}
else {
i++;
}
Bitmap bitmap = decodeFile(file[i]);
iv.setImageBitmap(bitmap);
iv.postDelayed(this, 5000);
}
}, 5000);

Categories

Resources