Android Imageview Switching Problem
i have imageview and i have to display different image in that with time interval, but when i change the ImageResource the last image is displayed public void onCreate(Bundle sav
Solution 1:
Perhaps this thread is what you are looking for:
Solution 2:
Whenever you want to update the user interface without performing any action or event, handlers should be used.
This is the sample code
Main.java
package com.balaji.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
publicclassMainextendsActivity {
private ImageView txtStatus;
int i=0;
int imgid[]={R.drawable.icon,R.drawable.back,R.drawable.slider,R.drawable.forward};
privateRefreshHandlermRedrawHandler=newRefreshHandler();
classRefreshHandlerextendsHandler {
@OverridepublicvoidhandleMessage(Message msg) {
Main.this.updateUI();
}
publicvoidsleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
privatevoidupdateUI(){
//int currentInt = Integer.parseInt((String) txtStatus.getText()) + 10;if(i<imgid.length){
mRedrawHandler.sleep(1000);
txtStatus.setBackgroundResource(imgid[i]);
i++;
}
}
@OverridepublicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
this.txtStatus = (ImageView)this.findViewById(R.id.txtStatus);
updateUI();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/txtStatus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_centerHorizontal="true"></ImageView></RelativeLayout>
Post a Comment for "Android Imageview Switching Problem"