I am a complete beginner in android. I am stuck at a point in making an application. I have images stored in hashmap and whatever line I give it as an input is broken into separate words on basis of space and its corresponding images are fetched. But I dont want these images to show up at once but these should be shown one after the other and there should be a pause of almost one second between each image to show up. But seems like I am stuck because of my inexperience. In code where and how should I place a pause? Because when I use Thread.sleep anywhere in code, it pause only in beginning everytime.
textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Speech.setText("You said " + matches_text.get(position));
selectedFromList = (matches_text.get(position));
String[] separated = selectedFromList.split(" ");
// ImageView iv;
final int[] imageViews = { R.id.imageView1,
R.id.imageView2, R.id.imageView3, R.id.imageView4,
R.id.imageView5, R.id.imageView6, R.id.imageView7,
R.id.imageView8, R.id.imageView9, R.id.imageView10,
R.id.imageView11, R.id.imageView12,
R.id.imageView13, R.id.imageView14,
R.id.imageView15, R.id.imageView16,
};
final_length = separated.length;
int b = 0;
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("apple",R.drawable.apple);
maps pol=new maps();
pol.map_A();
pol.map_B();
pol.map_C();
pol.map_D();
pol.map_E();
for (int i = 0; i < separated.length; i++) {
iv = (ImageView) findViewById(imageViews[i]);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
100, 100);
iv.setLayoutParams(layoutParams);
if(pol.map_a.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_a.get(separated[i].toLowerCase()));
}
else if(pol.map_b.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_b.get(separated[i].toLowerCase()));
}
else if(pol.map_c.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_c.get(separated[i].toLowerCase()));
}
else if(pol.map_d.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_d.get(separated[i].toLowerCase()));
}
else if(pol.map_e.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_e.get(separated[i].toLowerCase()));
}
}}
Change your for loop as following:
Handler handler1 = new Handler();
for (int i = 0; i < separated.length; i++) {
final int iDupe = i;
handler1.postDelayed(new Runnable() {
public void run() {
iv = (ImageView) findViewById(imageViews[iDupe]);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
if(pol.map_a.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_a.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_b.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_b.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_c.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_c.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_d.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_d.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_e.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_e.get(separated[iDupe].toLowerCase()));
}
}
}, i * 1000);
}
Related
I'm working on a tank-game and I have a TextView which represents the shot. Now I want to display the TextView at the specific point and remove it after a second that it looks like the shot goes further step by step. But when I add a countdown or a Thread.sleep the program stops for a second but the TextView doesn't disappear. i want to move the TextView over the screen and after every iteration of my for loop i want to wait a second and then rearrange it again?
Here is the code :
public void shot(float power, float winkel, Button button) {
if(winkel>90) {
winkel = winkel - 10;
}else if(winkel<90){
winkel = winkel +10;
}
for (double i = 0; i<100;i = i+ 1) {
final TextView textView = new TextView(context);
textView.setText(".");
double x = tanks.get(currentTank).getxPos()+(i*power*Math.cos(winkel *(Math.PI/180)));
double y = tanks.get(currentTank).getyPos()+(-1*(i*power*Math.sin(winkel *(Math.PI/180))));
double gravity = (-1*((9.81/2)*Math.pow(i,2)));
y = (y-gravity);
textView.setX((float) x);
textView.setY((float) y);
layout.addView(textView);
for (int j = 0;j<tanks.size();j++){
if(textView.getX()>tanks.get(j).getxPos()&&textView.getX()<tanks.get(j).getxPos()+100){
if(textView.getY()>tanks.get(j).getyPos()&&textView.getY()<tanks.get(j).getyPos()+100){
float k = tanks.get(j).getxPos()-textView.getX();
if(k<0){
k = k*-1;
}
makeDamage(k,tanks.get(j));
}
}
}
new CountDownTimer(2000,1000){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
layout.removeView(textView);
}
}.start();
}
newTurn();
}
I want to pause the program after adding the TextView for one second and the remove it. The program stops but the TextView doesn't disappear till the for-loop finished. Then all TextViews disappear.
Problem solved:
i've added all positions in a array and then this
public void drawShot(final Button firework, final ArrayList<TextView> toDraw){
final int[] i = {0};
final Handler mHandler = new Handler();
firework.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onClick(View v) {
firework(firework,toDraw.get(i[0]).getX(),toDraw.get(i[0]).getY());
}
});
Runnable runnable = new Runnable() {
#Override
public void run() {
layout.addView(toDraw.get(i[0]));
if(!check(toDraw.get(i[0]))) {
mHandler.postDelayed(this, (long) 1);
}
i[0]++;
}
};
// start it with:
mHandler.post(runnable);
}
probably need to run the remove command on main thread
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
#Override
public void run() {
layout.removeView(textView);
}
};
mainHandler.post(myRunnable);
I am fairly new to coding and app development(almost a month) so please bear with me. I am working on an app that uses a Barcode scanning plugin (customized as per needs).
In the app's main view (first window) I have a block, which on click triggers the Barcode plugin's scan(). This opens a scanner of the exact dimensions as the block of the main view. But if I rotate the device, the Barcode scanner view's dimensions goes haywire.
Is there a way I can adjust/change the x,y,w,h values of the Barcode scanner view so to align it with my app's block in the main view?
Upon clicking the Scan block on my app's main view, this scanner is opened as an overlay with custom buttons:
Upon rotating the device and clicking the scan block (green block on app's main view), this is how it looks:
Angular code passing the dimensions for the green scan block on app's main view:
.directive('barcodeScanner', function ($localStorage, _, $window) {
function link(scope, element, attrs){
scope.scanning = false;
scope.paused = false;
//Barcode-Scanning Green Window
var width = $window.innerWidth;
var height = width * 3/4;
if(width > 450){
width = 400;
height = 300;
}
scope.dimensionStyle = {
width: width+"px",
height: height+"px",
"margin-left" : "auto",
"margin-right" : "auto"
}
scope.$on('stop-scanner', function () {
scope.paused=false;
stopScanner();
});
scope.$on('start-scanner', function () {
scope.paused=true;
startScanner();
});
var mH = $window.innerHeight,
mW = $window.innerWidth;
var startBtn = element[0].querySelector('.barcode-start-btn');
function startScanner(){
var rect = startBtn.getBoundingClientRect();
var options = {
wPer : rect.width/mW,
hPer : rect.height/mH,
yPer : rect.top/mH,
xPer : rect.left/mW
}
scope.scanning = true;
cordova.plugins.barcodescanner.scan(function(result) {
scope.$emit('barcode-scanned',result);
},
options
);
}
My barcodescanner.java of the plugin that is triggered to open the scanner:
#Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
this.callbackContext = callbackContext;
this.requestArgs = args;
if (action.equals(SCAN)) {
// create fragment
if(!hasPermisssion()) {
requestPermissions(0);
} else {
scan(args);
}
} else if(action.equals(STOP)) {
stop(args);
} else {
return false;
}
return true;
}
public void scan(final JSONArray args) {
final CordovaPlugin that = this;
cordova.getThreadPool().execute(new Runnable() {
public void run() {
int maxHeight = webView.getView().getHeight();
int maxWidth = webView.getView().getWidth();
double xPer = 0/360;
double yPer = 82/568;
double hPer = 270/568;
double wPer = 360/360;
// add config as intent extras
if (args.length() > 0) {
JSONObject obj;
for (int i = 0; i < args.length(); i++) {
try {
obj = args.getJSONObject(i);
if(obj.has(X_PER)){
xPer = obj.getDouble(X_PER);
}
if(obj.has(Y_PER)){
yPer = obj.getDouble(Y_PER);
}
if(obj.has(H_PER)){
hPer = obj.getDouble(H_PER);
}
if(obj.has(W_PER)){
wPer = obj.getDouble(W_PER);
}
} catch (JSONException e) {
Log.i("CordovaLog", e.getLocalizedMessage());
continue;
}
}
}
Bundle bundle = new Bundle();
bundle.putDouble("x", maxWidth*xPer);
bundle.putDouble("y", maxHeight*yPer);
bundle.putDouble("w", maxWidth*wPer);
bundle.putDouble("h", maxHeight*hPer);
openCameraPopup(bundle);
}
});
}
My fragment class that implements ZxingScannerView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
if(state == null){
state = this.getArguments();
}
if(state != null) {
x = state.getDouble("x");
y = state.getDouble("y");
w = state.getDouble("w");
h = state.getDouble("h");
}
mScannerView = new ZXingScannerView(getActivity()){
#Override
public void setupCameraPreview(CameraWrapper cameraWrapper) {
super.setupCameraPreview(cameraWrapper);
buttonStop = new Button(getActivity());
buttonStop.setText("STOP");
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.addView(buttonStop);
buttonStop.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
mScannerView.stopCamera();
mScannerView.removeAllViews();
}
});
}
};
mScannerView.setZ(10);
mScannerView.setAutoFocus(true);
mScannerView.setFlash(false);
mScannerView.setX((float) x);
mScannerView.setY((float) y);
mScannerView.setLayoutParams(new ViewGroup.LayoutParams((int)w, (int)h));
return mScannerView;
}
I suggest you to lock your position using screen orientation landscape. So it will change position everytime you rotate your mobile.
currently developing an app where I need to take a number of wifi measurements, take an average of these measurements and store the averages. However I've found that I have to implement a delay between measurements otherwise there's not enough time to see any variation between measurements.
In the Handler.postDelayed() method I've tried to implement a 2000ms delay, however when I view the timestamp of the logs generated by TAKEWIFI, there appears to be no delay at all.
Any help would be greatly appreciated :)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_takewifi);
final String[] strArr = new String[60];
for (int i=0;i<60;i++)
{
strArr[i] = "EMPTY";
}
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final int state = wifi.getWifiState();
if(state == WifiManager.WIFI_STATE_ENABLED) {
RawData rD = new RawData();
Toast.makeText(TakeWifi.this,
"Taking RSS measurement, hold still!",
Toast.LENGTH_SHORT).show();
for(int a=0;a<30;a++)
{
wifi.startScan();
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
handler.postDelayed(this, 2000);
}
});
List<ScanResult> results = wifi.getScanResults();
String index = (String) results.toString();
int forCount = 0;
int ifCount = 0;
for (String retval: index.split(",")){
if (((forCount%5==1)||(forCount%5==3))&&(ifCount<60)){
strArr[ifCount] = retval;
strArr[ifCount] = strArr[ifCount].replace(" BSSID:", "BSSID:");
strArr[ifCount] = strArr[ifCount].replace(" level:", "level:");
ifCount++;
}
forCount++;
}
for(int check=0;check<60;check++)
{
Log.d("TAKEWIFI","strArr[" + check + "]: " + strArr[check]);
}
rD.setStrArr(strArr,rD);
}
final String[] temp = rD.getStrArr(rD);
for(int b=0;b<20;b++)
{
strArr[b]=temp[b];
}
for(int i=0;i<20;i++)
{Log.d("STRARR",strArr[i]);}
List<String> stringList = Arrays.asList(temp);
for (int i = 0; i < 20; i++) {
Log.d("STRLIST",stringList.get(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stringList);
ListView listview = (ListView) findViewById(R.id.listview);
if (listview==null)
{
Log.d("LISTVIEW","NULL");
}listview.setAdapter(adapter);
Toast.makeText(TakeWifi.this,"RSS measurement complete",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(TakeWifi.this,"Wifi Not Enabled",Toast.LENGTH_SHORT).show();
}
final Button commitBut=(Button)findViewById(R.id.button4);
commitBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(state == WifiManager.WIFI_STATE_ENABLED){
commit(strArr);
}
else
{
Toast.makeText(TakeWifi.this,"Wifi Not Enabled",Toast.LENGTH_SHORT).show();
}
}
});
}
Replace your handle with this and put everything you want delayed into it.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Everything you want to be delayed needs to go inside this runnable
}
}, 2000);
You are waiting in another thread (hence the run() method in your handler) :) Try removing that whole handler magic and call Thread.sleep directly.
While I am adding textviews to relative layout, at the end of first line, the textview is going wrong.
as shown in below:
.
here is my code to diplay textviews.
public void showkeyword()
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout fl = (RelativeLayout)findViewById(R.id.key_layout);
fl.removeAllViews();
RelativeLayout.LayoutParams params ;
//TextView key = (TextView) inflater.inflate(R.layout.tag_keyword,null);
i = 0;
for(String s : alist)
{
TextView textview = (TextView) inflater.inflate(R.layout.tag_keyword,null);
textview.setText(s);
textview.setId(2000 + i);
if (i == 0) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textview.setLayoutParams(rlp2);
fl.addView(textview);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// rlp2.addRule(RelativeLayout.ALIGN_BASELINE);
rlp2.setMargins(10,0, 10,0);
rlp2.addRule(RelativeLayout.RIGHT_OF, textview.getId() - 1);
textview.setLayoutParams(rlp2);
fl.addView(textview);
}
i++;
}
}
I wish to have something like this, sort of a tab implementation:
Hope the following code will help you out:
Functioning:
contactWrapper is a linear layout, we go on adding the textviews into these linear layouts one by one and before adding find whether the contactWrapper has space enough to put in the next TextView, if not a new linear layout is created and the textViews are added into it.
Take time analyzing the following code.
public void drawLayout() {
int counter = 0;
contactWrapperWidth = getResources().getDisplayMetrics().widthPixels;
contactWrapper.setOrientation(LinearLayout.VERTICAL);
// contact wrapper is a linear Layout
// use LinearLayout contactWrapper = (LinearLayout) mView
// .findViewById(R.id.yourLinearLayout);
currCounter = 0;
currWidth = 0;
isNewLine = false;
row[currCounter] = new LinearLayout(getActivity());
#SuppressWarnings("rawtypes")
Iterator it = button.iterator();
for (int i = 0; i < button.size(); i++) {
it.next();
row[currCounter].setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
currWidth += Integer
.parseInt(button.get(i).get("width").toString());
Log.i("Item width ", "i == "
+ button.get(i).get("width").toString());
// contactWrapper.getw
if (isNewLine) {
if (currWidth < contactWrapperWidth) {
row[currCounter]
.addView((View) button.get(i).get("button"));
if (!it.hasNext()) {
contactWrapper.addView(row[currCounter]);
} else {
if (contactWrapperWidth < (currWidth + Integer
.parseInt(button.get(i + 1).get("width")
.toString()))) {
isNewLine = true;
contactWrapper.addView(row[currCounter]);
currCounter += 1;
row[currCounter] = new LinearLayout(getActivity());
currWidth = 0;
} else {
isNewLine = false;
}
}
} else {
isNewLine = true;
contactWrapper.addView(row[currCounter]);
currCounter += 1;
row[currCounter] = new LinearLayout(getActivity());
currWidth = 0;
}
} else {
if (currWidth < contactWrapperWidth) {
if (!it.hasNext()) {
View view = (View) button.get(i).get("button");
row[currCounter].addView((View) button.get(i).get(
"button"));
contactWrapper.addView(row[currCounter]);
} else {
View view = (View) button.get(i).get("button");
row[currCounter].addView((View) button.get(i).get(
"button"));
if (contactWrapperWidth < (currWidth + Integer
.parseInt(button.get(i + 1).get("width")
.toString()))) {
isNewLine = true;
Logger.show(Log.INFO, "it.hasNext()",
"it.hasNext() contactWrapper");
contactWrapper.addView(row[currCounter]);
currCounter += 1;
row[currCounter] = new LinearLayout(getActivity());
currWidth = 0;
} else {
isNewLine = false;
}
}
} else {
isNewLine = true;
contactWrapper.addView(row[currCounter]);
currCounter += 1;
row[currCounter] = new LinearLayout(getActivity());
currWidth = 0;
}
}
counter++;
}
}
Finally I am able to remove that bug using idea by #kailas
Here I am posting my method:
public void showkeyword() {
int counter = 0;
int screenWidth = getResources().getDisplayMetrics().widthPixels;
final RelativeLayout contactWrapper = (RelativeLayout)findViewById(R.id.key_layout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout.LayoutParams buttonparams = new RelativeLayout.LayoutParams(
150,
80);
int i = 0;
contactWrapper.removeAllViews();
// contact wrapper is a linear Layout
// use LinearLayout contactWrapper = (LinearLayout) mView
// .findViewById(R.id.yourLinearLayout);
int currCounter = 0;
int currWidth = 0;
boolean isNewLine = false;
boolean firstLine = true;
for(final String s : alist)
{
TextView textview = new TextView(this);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp1.setMargins(7, 5, 7, 0);
textview.setText(s);
textview.setId(2000 + i);
textview.setBackgroundColor(Color.DKGRAY);
textview.setTextColor(Color.CYAN);
textview.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
contactWrapper.removeView(v);
alist.remove(s);
}
});
int width = s.length()*15;
if((currWidth+width+150)<=screenWidth)
{
currWidth += width+10;
isNewLine = false;
currCounter++;
}
else{
currWidth = width+14;
firstLine = false ;
isNewLine = true;
currCounter=1;
}
if(i==0)
{ rlp1.addRule(RelativeLayout.ALIGN_START);
textview.setLayoutParams(rlp1);
contactWrapper.addView(textview);
}
else if(isNewLine){
rlp1.addRule(RelativeLayout.ALIGN_LEFT);
rlp1.addRule(RelativeLayout.BELOW,2000-1+i );
textview.setLayoutParams(rlp1);
contactWrapper.addView(textview);
}
else if(firstLine)
{
rlp1.addRule(RelativeLayout.RIGHT_OF,2000-1+i );
textview.setLayoutParams(rlp1);
contactWrapper.addView(textview);
}
else{
rlp1.addRule(RelativeLayout.RIGHT_OF,2000-1+i );
rlp1.addRule(RelativeLayout.BELOW,2000-currCounter+i );
textview.setLayoutParams(rlp1);
contactWrapper.addView(textview);
}
i++;
}
buttonparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
buttonparams.addRule(RelativeLayout.ALIGN_BASELINE,2000-1+i);
Button clearbtn = new Button(this);
clearbtn.setText("clear");
// clearbtn.setBackgroundColor(Color.RED);
// clearbtn.setTextColor(Color.CYAN);
clearbtn.setLayoutParams(buttonparams);
clearbtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
contactWrapper.removeAllViews();
alist.clear();
}
});
contactWrapper.addView(clearbtn) ;
}
Try to remove
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
because the behavior you want is the default behavior.
Put Orientation horinzontal in the relative layout.
Try using a LinearLayout and a fixed height (using the dip unit) instead of WRAP_CONTENT
Try to put all the text you want in one String and then put all this text in only one TextView.
The best way is using StringBuilder to concat the text:
StringBuilder sb = new StringBuilder();
sb.append("str1");
sb.append("str2");
and then put the string in the textview
textview.setText(sb.toString());
i'm new to android programming. I have the following code happening on a button click
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.morse_btn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loopCode();
}
});
}
which calls this:
public void loopCode()
{
String code = "Hello There";
TextView view = (TextView) findViewById(R.id.code_txt);
String s = "";
for(int i = 0; i < code.length(); i++)
{
s+=code.charAt(i);
view.setText(s);
try {
TimeUnit.SECONDS.sleep(1);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
but when i run it on my phone, the text does not get appended until after the for loop has gone through, i.e i press the button, and after a few seconds, the whole string "Hello There" appears.
How can I make it write the text one character at a time, like a typewriter style.
Thanks
You need to use view.append("") which will append new text to the existing one.
Try this code:
int i = 0; //declare this globally
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i != 10) {
text.append(" " + i);
i++;
handler.postDelayed(this, 1000);
}
}
}, 1000);
}
This code will append a new number to the TextView every one second until it has reached the count 10. You can apply the same logic.
I had provided this solution to a question here -
[EDIT]
Try this:
String code = "Hello There"; //declare variable globally
int i = 0; //declare globally
TextView view; //declare globally
public void loopCode()
{
view = (TextView) findViewById(R.id.code_txt);
//String s = "";
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i != code.length()) {
view.append(" " + code.charAt(i));
i++;
handler.postDelayed(this, 1000);
}
}
}, 1000);
}
}
Don't forget to declare int i = 0 and String code = "Hello There" globally.
Exist 2 different method setText in TextView.
public final void setText (int resid)
public final void setText (CharSequence text)
when you put variable int in setText, the android try find String in classe R variable in same code.
To resolve this you then cast int to string using String.valueOF(...)
see more in;
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf(int)
try
public void loopCode()
{
String code = "Hello There";
TextView view = (TextView) findViewById(R.id.code_txt);
String s = "";
for(int i = 0; i < code.length(); i++)
{
view.setText(String.valueOf(i));
try {
TimeUnit.SECONDS.sleep(1);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}