I want to show images one by one neither in vertical nor horizontal.
I just created one imageview in xml layout and then i called that image by through id and then i just want to show list of images in one imageview like this.
It first show first image after some time the first image was removed and then second image will be visible with out changing position of imageview like ads in android device. I mean Every 10 seconds the imageview has been changed.
So i tried below code
class doInBack extends AsyncTask<URL, Bitmap, Long>
{
#Override
protected Long doInBackground(URL... arg0) {
for(int i=0;i<adimage.length;i++){
Bitmap bitmap=Download(adimage[i]);
publishProgress(bitmap);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
protected void onProgressUpdate(Bitmap bitmap) {
adimg.setImageBitmap(bitmap);
}
protected void onPostExecute(Long result) {
}
}
But it didn't call OnProgressUpdate method. So please tell me how to show number of images one by one in one imageview.
I used below code its working
int i=0;
final Handler handler=new Handler();
final Runnable r=new Runnable()
{
public void run()
{
if(i<adimage.length){
adimage.setImageBitmap(Download(adimage[i]));
i++;
if(i==adimage.length)
i=0;
}
else
i=0;
}
};
if(thread == null){
thread = new Thread()
{
#Override
public void run() {
try {
while(true) {
sleep(10*1000);
handler.post(r);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
Related
I am making a project in which i am using the progressdialog and i want to show this progress dialog on creation of activity and i am able to that. In the on create method i want the image to be invisible and i want image to be visible after completion of progress dialog but it is throwing exception in the line imagevisible();
The logcat is:
04-12 12:48:35.309: E/AndroidRuntime(4994): at com.example.project1.ShowPassword$waiter.run(ShowPassword.java:59)
Code
ImageView iv;
ProgressDialog p;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.showpass);
iv=(ImageView)findViewById(R.id.imageView1);
iv.setVisibility(View.INVISIBLE);
p= new ProgressDialog(this);
p.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
p.setTitle("Getting Password: ");
p.setMessage("Loading:");
p.setMax(100);
p.show();
Thread t=new Thread(new waiter());
t.start();
public class waiter extends Thread{
public void run(){
for(int i=0; i<5; i++){
p.incrementProgressBy(20);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}p.dismiss();
imagevisible();
}
}
public void imagevisible(){
iv.setVisibility(View.VISIBLE);
}
You can't change UI from non UI thread. You can use runOnUiThread method of Activity:
public class waiter extends Thread{
public void run(){
//...
runOnUiThread(new Runnable() {
#Override
public void run() {
imagevisible();
}
});
}
}
What you want is an AsyncTask. Implement doInBackground() (runs in the background) and onPostExecute() (runs on the UI thread).
https://developer.android.com/reference/android/os/AsyncTask.html
I am working on an android app in which i want to hide my image view after some interval. i am using this code but it is not hiding. can anybody tell me how i can hide it ???
showtrue1.setBackgroundResource(R.drawable.ticktrue);
showtrue1.setVisibility(View.VISIBLE);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
showtrue1.setVisibility(View.GONE);
Try CountDownTimer:
new CountDownTimer(1000, 100) {
public void onTick(long millisUntilFinished) {
// implement whatever you want for every tick
}
public void onFinish() {
showtrue1.setVisibility(View.GONE);
}
}.start();
You can use async Task also to solve your problem . In its backgound function make a thread sleep for particular second and in the post method make trhe visibility gone for your image viiew.
Call the execute method in your oncreate
new MyAsyncTask().execute();
and make an inner class as defined below:
private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
// show your progress dialog
showtrue1.setBackgroundResource(R.drawable.ticktrue);
showtrue1.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... voids){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void params)
{
showtrue1.setVisibility(View.GONE);
}
}
Create a separate thread that sleeps for 1 seconds then call runOnUiThread to hide the view.
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// Do some stuff
showtrue1.setVisibility(View.GONE);
}
});
}
};
PROBLEM
I have nearly 20 TextView and I need to have do something to make their background color change dynamically.
For example: all TextView have same default background color then after 5 sec first one's will turn red where others still same then after 5 more sec passed first TextView's background color will turn to default and second TextView's background color will be turn to red and it will go on like this.
I tried to put them in for loop in thread and handler but they changed all together. What should I do ?
I have tried this code
Thread color=new Thread() {
public void run() {
for(int i=2131230724;i<=2131230742;i++){
TextView currentText = (TextView) findViewById(i);
currentText.setBackgroundColor(Color.RED);
try {
sleep(2000);
currentText.setBackgroundColor(Color.TRANSPARENT);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
then I used
mHandler.post(color);
SOLUTION
I found a solution for my problem and sharing if someone else needed too.
Runnable myhandler=new Runnable() {
int id=2131230724;//First textView id
#Override
public void run() {
// TODO Auto-generated method stub
if(id==2131230724){
TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime1);
currentText.setBackgroundColor(Color.RED);
id++;
}
else if(id==2131230725){
TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime2);
currentText.setBackgroundColor(Color.RED);
TextView PreText=(TextView)findViewById(R.id.egzersiz01_kelime1);
PreText.setBackgroundColor(Color.TRANSPARENT);
id++;
}//And all other id's to else if as same as mine.
mHandler.postDelayed(myhandler, delay);
and in onCreate()
myhandler.run();
I have tried to use for loop to make it easy but it crashed and I had to write them all.
Thanks for helping...
You can put the params to AsynTask like this, and then
new changeColor().execute(max);
private class changeColor extends AsyncTask<Integer,Integer,Void>{
int max,cur;
#Override
protected void onPreExecute() {
max=cur=0;
super.onPreExecute();
}
#Override
protected Void doInBackground(Integer... number) {
int max=number[0];
int cur=1;
if(max>0)
while(true ){/*or other condition else*/
try {
Thread.sleep(5000);//Sleep 5000s before make change
} catch (InterruptedException e) {
Log.i("TAG","InterruptedException");
}
publishProgress(cur);
if(cur==max)
cur=1;
else
cur++;
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
changeColorTextView(values[0]);
super.onProgressUpdate(values);
}
private void changeColorTextView(int textViewId){
switch(textViewId){
case textId1:{
//do some thing here like:
//textView2.setBackGroundColor(R.color.RED);
//textView1.setBackGroundColor(default);
break;
}
//....
}
}
}
I write a Splash Screeen to run at the boot time of application
public class SplashScreen extends Activity {
ImageView imgView;
int[] imgID = new int[]{R.drawable.frame0, R.drawable.frame1, R.drawable.frame2, R.drawable.frame3,
R.drawable.frame4, R.drawable.frame5, R.drawable.frame6};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imgView = (ImageView) findViewById(R.id.imgSplash);
new Thread(new WelcomeScreen()).start();
}
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
imgView.setImageResource(imgID[i]);
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
}
}
It getting error "Sorry the application has stopped unexpectedly" . I don't know why . Somebody can help me ????
you can not set the resource for yuor ImageView inside a thread different from the UI Thread.
you can use runOnUiThread. It takes as paramter a runnable, and post it in the UI Thread queue. There, the UI thead takes it and update your ImageView. All in all your runnable will become:
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
final int resuorceId = imgID[i];
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(resuorceId);
}
});
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
You can not access your views from Thread.
You will need to put your code imgView.setImageResource(imgID[i]); in runOnUiThread
use like:
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
imgView.setImageResource(imgID[i]);
}
});
Thanks
You can not change something in UI from non-UI thread so replace this you code:
imgView.setImageResource(imgID[i]);
to:
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(imgID[i]);
}
});
//try code this way...
public class SplashScreen extends Activity {
private Intent launchIntent;
private Thread splashThread; //used for perform splash screen operation
private int splashTime = 10000, sleepTime = 50; //used for threading operation
private boolean active = true; //used for touch event
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen); //Set splashscreen.xml here
try {
splashThread = new Thread() { // Creating Thread for splash the screen
#Override
public void run() { // run method implemented to perform threading operation
try {
int waitTime = 0; //counter for threading
do {
sleep(sleepTime); //delay for specific time
if (active)
waitTime += 100;
//write your image code here that display your no. of images
} while (active && (waitTime < splashTime)); //Check touch condition and counter
} catch (Exception e) {
// to handle runtime error of run method
Validation.displayToastMessage(SplashScreen.this, e.toString()); //Call static method of class ToastMessage
}
finish(); //finish current activity
startJustCoupleActivityScreen(); //Call below defined function
}
};
splashThread.start(); //start thread here
} catch (Exception e) {
message("SplashScreen : "+ e.toString()); //Call static method of class ToastMessage
}
}
public void startJustCoupleActivityScreen() {
launchIntent=new Intent(SplashScreen.this,JustCoupleActivity.class); //call Next Screen
startActivity(launchIntent); //start new activity
}
#Override
public boolean onTouchEvent(MotionEvent event) { //onTouch Event
//on touch it immediate skip splash screen
if(event.getAction()==MotionEvent.ACTION_DOWN) active=false; //Check Touch happened or not
return true;
}
public void message(String msg)
{
Validation.displayToastMessage(SplashScreen.this, msg); //display Error Message
}
}
I want show dialog after finish Thread.
In thread I am changing TextView's Value like as 0 to 100...
When TextView Value is reach 100 then i want to show dialog..
What i do for it.
Thanks in advance...
Code Snippet:
final Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
synchronized (this)
{
try
{
for(int i=0 ; i<speed; i++)
{
final int value=i+1;
wait(3000/speed);
Test.this.runOnUiThread(new Runnable() {#Override public void run()
{
accText.setText(String.valueOf(value));
}});
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread.start();
this is asyntask code snippet...
class setTextBackgroundTask extends AsyncTask<String , Integer, Void>
{
#Override
protected void onPreExecute()
{
}
#Override
protected Void doInBackground(String... params)
{
Thread th = new Thread();
int value;
for(int i=0 ; i<speed; i++)
{
value=i+1;
publishProgress(value);
try {
th.sleep(3000/speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values)
{
accText.setText(String.valueOf(values[0]));
System.out.println("Value=="+values[0]);
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void result)
{
showShareDialog();
}
}
This document explains how you can create dialogs.
Please note that you will have to do any dialog creation code on the UI thread.
You already have code that runs something on the UI thread, just do that outside the loop,but with the dialog creation code inside.
You should use AsyncTask: subclass AsyncTask, override doInBackground() to execute your time consuming action on another thread, and then override onPostExecute() to show your dialog.
Note that you cannot change UI elements from a non-UI (background) thread. AsyncTask takes care of that for you: it calls doInBackground() on a new thread and then calls onPostExecute() on the UI thread as soon as the background task is complete.