How to make Dynamic change of TextVIew and UI in general? - android

Good afternoon everyone
So, I'm trying to dynamically change textview's properties.
basically, I defined a Duration. and I want to my handler/runnable to append text to a textView until I reach the duration.
public class Dynamic_testActivity extends Activity
{
public Context context = null;
public TextView view = null;
public Handler mHandler = null;
public long startTime = 0L;
public final int duration_millis = 10000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
view = (TextView) findViewById(R.id.textView1);
view.append("\n");
mHandler = new Handler();
}
#Override
protected void onStart() {
super.onStart();
startTime = System.currentTimeMillis();
mHandler.post(new Runnable() {
#Override
public void run() {
view.append("Hell_yeah_!\n");
// 10 character lenght
}
});
}
}
So yes, it append the text once, because the run do so.
But how could I make some kind of loop, without blocking the UI Thread, and append text until the end of the duration.
That was the first step ...
The second part now ... In fact, I Want to change the color of the text.
using
Spannable WordtoSpan = new SpannableString(view.getText());
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, view.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
I want to the color changing to be dynamic for the duration ... like a karaoke ...
So, is it possible to make that in the runnable without having the UI Thread blocked until the end of the duration ? and How ?
If anyone could explain the complet process ? or post some source code
Solved.
Here is a basic example...
There is still a little trouble ... at the very beginning of the application, the whole textview is yellow, and after a second, it updates the display as it should be .
If any one knows why, advices are welcome =)
Note : there's only two simple Textview in the layout... Duration is in milliseconds... and there is 10 character in the dynamic textview to fit the duration ... So basically, one char = one second ...
public class Dynamic_testActivity extends Activity
{
public Context context = null;
public TextView view = null;
public TextView view2 = null;
public Handler handler = null;
public long start_time, current_time, elapsed_time = 0L;
public final int duration = 10000;
public int end = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
view = (TextView) findViewById(R.id.textView1);
view2 = (TextView) findViewById(R.id.textView2);
handler = new Handler();
}
#Override
protected void onStart() {
super.onStart();
start_time = Long.valueOf( System.currentTimeMillis() );
current_time = start_time;
handler.postDelayed(new Runnable() {
#Override
public void run() {
current_time = Long.valueOf( System.currentTimeMillis() );
elapsed_time = Long.valueOf(current_time) - Long.valueOf(start_time);
if ( elapsed_time >= duration + 30 ) {
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
//finish();
} else {
end = (int) (elapsed_time / 1000);
Spannable WordtoSpan = new SpannableString(view.getText());
WordtoSpan.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
view.setText(WordtoSpan);
view2.setText("time : " + elapsed_time);
handler.postDelayed(this, 10);
}
}
}, 10);
}
}

In your run() methond, you can call mHandler.post(this) (or use postDelayed to delay it)
There is property change animation in API level 14, but if you are targetting a lower version, use postDelayed repetively to change progressively the color of the text.

Related

Handler postdelayed is unaffected for less than 10 milliseconds

I have created two TextViews and updating their contents through two different handlers, one for each TextView. I am incremented two counters in both handlers, one in each, and posting their values to respective TextView.
My first handler is ticking according to the value of long speed variable, and second handler is ticking 10 times later.
public class Main extends Activity{
private View root;
private final Context context = this;
private int count1 = 0, count2 = 0;
private TextView view1, view2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
setContentView(R.layout.s_main);
root = findViewById(R.id.root);
view1 = findViewById(R.id.txv1);
view2 = findViewById(R.id.txv2);
//
ViewTreeObserver vto = root.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
root.getViewTreeObserver().removeOnPreDrawListener(this);
startRuns();
return true;
}
});
}
private void startRuns() {
try {
final long sleep = 10;
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
view1.setText(String.valueOf(count1++));
handler1.postDelayed(this, sleep);
}
}, 1);
final Handler handler2 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
view2.setText(String.valueOf(count2++));
handler2.postDelayed(this, sleep * 10);
}
}, 1);
Threads.run = true;
} catch (Exception ex) {
Alerts.alert(context, ex.getMessage());
}
}
}
This is good till the value of speed is 10 or above, but as soon i set the value of speed to 1, both handlers work with same duration or speed. First handler should work with speed of 1, and second handler should work with speed of 10, but both works with speed of 10 ms.
EDIT
In short, minimum delay on which postDelayed is working is 10 ms, not less than that, not 1, not 5, at least 10. I need to run a runnable with 5 ms delay in a project.
Can you please suggest where I am wrong?

Making a simple counter that increments every second on android. What wrong am I doing?

Im writing code to display a counter that increments by 1 every second.
The app is getting forced closed before running with the error -
thread exiting with uncaught exception and No package identifier when getting value for resource number
public class MainActivity extends Activity {
Handler hand = new Handler();
TextView ti;
int i =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ti = (TextView) findViewById(R.id.textView);
hand.postDelayed(run, 1000);
}
Runnable run = new Runnable() {
#Override
public void run() {
i++;
ti.setText(i);
}
};
}
What wrong am I doing ?
The method textView.setText(int id) takes an int as parameter. For e.g. R.string.app-name. When you pass i as parameter, it will check for id i in strings.xml. TextView also provides an overloaded method setText(CharSequence text), which you can use here.
textView.setText(""+i); or textView.setText(String.valueOf(i));
ti.setText(i) is trying to resolve a resource by the value of i.
Try ti.setText(String.valueOf(i)); instead.
If you need a precise timer, you should calculate a difference, from when started a timer, increment a variable in every call is not a accurate solution, here is the code, if you want a precise timer
private long startTime;
private final Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
startTime = System.currentTimeMillis();
handler.post(new Runnable() {
#Override
public void run() {
long diff = System.currentTimeMillis() - startTime;
textView.setText(Long.toString(diff / 1000));
handler.postDelayed(this, 1000);
}
});
}

How can I refresh TextView during Digital Clock in Android?

I want to refresh a TextView's text per minute when Digital Clock refresh the time with the format hh/mm every minute. I put a TextView named txtView1 in the Activity and create a class Digital Clock.When I run the app,However,the app exits with error.I really don't konw why
here is the important function onAttachedToWindow() about the Digital Clock:
protected void onAttachedToWindow() {
mTickerStopped = false;
super.onAttachedToWindow();
mHandler = new Handler();
/**
* requests a tick on the next hard-second boundary
*/
mTicker = new Runnable() {
public void run() {
if (mTickerStopped) return;
mCalendar.setTimeInMillis(System.currentTimeMillis());
String content = (String) DateFormat.format(mFormat, mCalendar);
if(content.split(" ").length > 1){
content = content.split(" ")[0] + content.split(" ")[1];
}
setText(android.text.Html.fromHtml(content));
//-----Here is the TextView I want to refresh
TextView txtV1 = (TextView)findViewById(R.id.txtView1);
txtV1.setText("Now Fresh");//Just for try,so set a constant string
invalidate();
long now = SystemClock.uptimeMillis();
//refresh each minute
long next = now + (60*1000 - now % 1000);
mHandler.postAtTime(mTicker, next);
}
};
mTicker.run();
}
The system sends a broadcast event at the exact beginning of every minutes based on system clock. The most reliable way is to do it like this :
BroadcastReceiver _broadcastReceiver;
private final SimpleDateFormat _sdfWatchTime = new SimpleDateFormat("HH:mm");
private TextView _tvTime;
#Override
public void onStart() {
super.onStart();
_broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
_tvTime.setText(_sdfWatchTime.format(new Date()));
}
};
registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}
#Override
public void onStop() {
super.onStop();
if (_broadcastReceiver != null)
unregisterReceiver(_broadcastReceiver);
}
Don't forget however to initialize your TextView beforehand (to current system time) since it is likely you will pop your UI in the middle of a minute and the TextView won't be updated until the next minute happens.

Created clock on Android is not updating

I´m trying to create a fullscreen clock. I managed to set text for hours, minutes and seconds. Well, but when I start the app, it shows the time but the UI is not updating... I dont know how to do it, I read this tutorial but i dont understand it... any one can explain me how to consantly update the UI?
public class Clock1Activity extends Activity {
/** Called when the activity is first created. */
private Timer timer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txtHour = (TextView)findViewById(R.id.TxtHour);
final TextView txtMinutes = (TextView)findViewById(R.id.TxtMinute);
final TextView txtSeconds = (TextView)findViewById(R.id.TxtSeconds);
final TextView txtMilliseconds = (TextView)findViewById(R.id.TxtMilliseconds);
final Integer hora = new Integer(Calendar.HOUR_OF_DAY);
final Integer minutos = new Integer(Calendar.MINUTE);
final Integer segundos = new Integer(Calendar.SECOND);
final Long milisegundos = new Long (System.currentTimeMillis());
timer = new Timer("DigitalClock");
Calendar calendar = Calendar.getInstance();
// Get the Current Time
final Runnable updateTask = new Runnable() {
public void run() {
/** txtHour.setText(hora.toString());
txtMinutes.setText(minutos.toString());
txtSeconds.setText(segundos.toString()); */
txtMilliseconds.setText(milisegundos.toString());
Toast toast1 = Toast.makeText(getApplicationContext(), milisegundos.toString(), Toast.LENGTH_SHORT);
toast1.show();
}
};
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(updateTask);
}
}, 1, 1000);
}
}
Just tell me how to complete it to update UI,please...
You didn't mention which tutorial you're working on, so just in case, you'll probably want to use AsyncTask.
see this example for Create a Apps to Show Digital Time in Android .And in your case use
runOnUiThread for Upadting time on UI.as
CurrentActivity.this.runOnUiThread(new Runnable() {
public void run() {
//UPDATE TIME HERE
}
});
and your code look like:
public class Clock1Activity extends Activity {
/** Called when the activity is first created. */
private Timer timer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txtHour = (TextView)findViewById(R.id.TxtHour);
final TextView txtMinutes = (TextView)findViewById(R.id.TxtMinute);
final TextView txtSeconds = (TextView)findViewById(R.id.TxtSeconds);
final TextView txtMilliseconds = (TextView)findViewById(R.id.TxtMilliseconds);
timer = new Timer("DigitalClock");
Calendar calendar = Calendar.getInstance();
// Get the Current Time
final Runnable updateTask = new Runnable() {
public void run() {
final Integer hora = new Integer(Calendar.HOUR_OF_DAY);
final Integer minutos = new Integer(Calendar.MINUTE);
final Integer segundos = new Integer(Calendar.SECOND);
final Integer milisegundos = new Integer(Calendar.MILLISECOND);
txtHour.setText(hora.toString());
txtMinutes.setText(minutos.toString());
txtSeconds.setText(segundos.toString());
txtMilliseconds.setText(milisegundos.toString());
}
};
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(updateTask);
}
}, 1, 1000);
}
}

TextSwitcher not updating

So I have a TextSwitcher that I want to update every second with the number of seconds it has been since the activity opened. Here is my code
public class SecondActivity extends Activity implements ViewFactory
{
private TextSwitcher counter;
private Timer secondCounter;
int elapsedTime = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{
// Create the layout
super.onCreate(savedInstanceState);
setContentView(R.layout.event);
// Timer that keeps track of elapsed time
counter = (TextSwitcher) findViewById(R.id.timeswitcher);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
counter.setFactory(this);
counter.setInAnimation(in);
counter.setOutAnimation(out);
secondCounter = new Timer();
secondCounter.schedule(new TimerUpdate(), 0, 1000);
}
/**
* Updates the clock timer every second
*/
public void updateClock()
{
//Update time
elapsedTime++;
int hours = elapsedTime/360;
int minutes = elapsedTime/60;
int seconds = elapsedTime%60;
// Format the string based on the number of hours, minutes and seconds
String time = "";
if (!hours >= 10)
{
time += "0";
}
time += hours + ":";
if (!minutes >= 10)
{
time += "0";
}
time += minutes + ":";
if (!seconds >= 10)
{
time += "0";
}
time += seconds;
// Set the text to the textview
counter.setText(time);
}
private class TimerUpdate extends TimerTask
{
#Override
public void run()
{
updateClock();
}
}
#Override
public View makeView()
{
Log.d("MakeView");
TextView t = new TextView(this);
t.setTextSize(40);
return t;
}
}
So basically, I have a Timer that every second adds another second and them formats the way I want to be displayed and set the text of the TextSwitcher, which I thought called makeView, but makeView only gets called once and the time stays as 00:00:01. Did I miss a step, I dont think this UI object is very well documented.
Thanks, Jake
You can only update the UI in the UI thread. So in your example you could do something like this.
private Handler mHandler = new Handler() {
void handleMessage(Message msg) {
switch(msg.what) {
CASE UPDATE_TIME:
// set text to whatever, value can be put in the Message
}
}
}
And call
mHandler.sendMessage(msg);
in the run() method of the TimerTask.
This is a solution to your current issue but there is probably a better way to do it without using TimerTasks.

Categories

Resources