Android code about timer - android

I have change code to below and my purpose is to set a button
when I click the timer run,how to change the code below? thanks
new code below
I have change code to below and my purpose is to set a button
when I click the timer run,how to change the code below? thanks
new code below
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Handler aHandler;
TextView aTextView;
Button aButton;
EditText aEditText;
Handler aHandler01;
TextView aTextView01;
Button aButton01;
EditText aEditText01;
Boolean checker=false;
int count = 11;
int count01 = 11;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
aEditText = (EditText)findViewById(R.id.editText01);
aTextView = (TextView)findViewById(R.id.TextView01);
aEditText01 = (EditText)findViewById(R.id.editText02);
aTextView01 = (TextView)findViewById(R.id.TextView02);
aButton=(Button)findViewById(R.id.button01);
aButton01=(Button)findViewById(R.id.button02);
//第一個計時器
aButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){ if(!aEditText.getText().toString().equalsIgnoreCase(""))
{count=Integer.parseInt(aEditText.getText().toString());}
aHandler = new Handler();
if(checker==false){
aHandler.post(runnable);
checker=true;
}
}
});
//第二個計時器
aButton01.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
if (!aEditText01.getText().toString().equalsIgnoreCase("")) {
count01 = Integer.parseInt(aEditText01.getText().toString());
}
aHandler01 = new Handler();
aHandler01.post(runnable01);
}
});
}
//第一個計時器
final Runnable runnable = new Runnable() {
public void run() { // TODO Auto-generated method stub
if (count> 0) {
aTextView.setText(Integer.toString(count-1));
count--;
aHandler.postDelayed(runnable, 1000);
}else{
aTextView.setText("boom");
}
}
};
//第二個計時器
final Runnable runnable01 = new Runnable() {
public void run() { // TODO Auto-generated method stub
if (count01> 0) {
aTextView01.setText(Integer.toString(count01-1));
count01--;
aHandler01.postDelayed(runnable01, 1000);
}else{
aTextView01.setText("boom");
}
}
};
#Override
protected void onPause() { // TODO Auto-generated method stub
super.onPause();
if (aHandler != null) {
aHandler.removeCallbacks(runnable);
}
}
}

try this
private Handler handler = new Handler();
Boolean checker=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
aEditText = (EditText)findViewById(R.id.editText01);
aTextView = (TextView)findViewById(R.id.TextView01);
aButton=(Button)findViewById(R.id.button01);
aButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){ if(!aEditText.getText().toString().equalsIgnoreCase(""))
{count=Integer.parseInt(aEditText.getText().toString());}
if(!checker){
checker=true;
handler.post(runnable);//to start timer
}
}
});
}
final Runnable runnable = new Runnable() {
public void run() {
// TODO Auto-generated method stub
if(checker)
{
checker=false;
return;
}
if (count> 0) {
aTextView.setText(""+Integer.toString(count-1));
count--;
aHandler.postDelayed(runnable, 1000);
}else{
aTextView.setText("boom");
}
}
};

as you are assigning the value to 'count' variable in OnCreate from editText when editText is empty, it is returning null string which then you are parsing for int value which returns NumberFormatException
try to do it as follows
if (!aEditText.getText().toString().equalsIgnoreCase(""))
{
count = Integer.parseInt(aEditText.getText().toString());
}
OR you can assign a value to 'count' from editText on any button's click or using TextChangedListener(TextWatcher) to monitor and get text from edittext at run time.

Related

What is the difference between a UIHandler and a Handler

I want to show numbers from 1 to 100 in sequel order in the TextView and to wait 1 second after printing each number. I also want to implement it using Android services.
I don't know the difference between UIHandler and Handler. When I google about this issue, all I am getting is the difference between handler and a thread.
Please help me out of this,
Thanks in advance
private static final int SHOW_MESSAGE = 1;
private static final int m_cdelay = 1000;
private UIHandler m_cUIHandler;
public int m_cI= 0;
TextView m_cTextShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_cTextShow = (TextView) findViewById(R.id.textview1);
for(m_cI=1; m_cI <= 100; m_cI++){
//m_cUIHandler = new UIHandler();
//m_cUIHandler.sendEmptyMessageDelayed(SHOW_MESSAGE, 1000);
showMessage(m_cI);
}
}
private void showMessage(int m_cI2) {
for(m_cI=1; m_cI <= 100; m_cI++){
m_cTextShow.setText(""+m_cI);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(m_cdelay);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}).start();
}
}
#Override
protected void onResume() {
super.onResume();
startService(new Intent(this, NumberService.class));
}
public final class UIHandler extends Handler {
#Override
public void handleMessage(Message pObjMessage) {
switch(pObjMessage.what) {
case SHOW_MESSAGE:
m_cTextShow.setText(""+m_cI);
break;
}
}
}
You can actually rewrite your code like that to make it probably work.
Pls test it and respond.
public void showMessage(int number){
runOnUiThread(new Runnable(){
public void run() {
//Write your number onto the screen
}
});
}
protected void onCreate(Bundle savedInstanceState) {
//Blablabla...
for(m_cI=1; m_cI <= 100; m_cI++){
//m_cUIHandler = new UIHandler();
//m_cUIHandler.sendEmptyMessageDelayed(SHOW_MESSAGE, 1000);
showMessage(m_cI);
Thread.sleep(1000);
}
}

Android update TextView inside dialog in runOnUiThread

I have been spending couple hours to try to update the textview inside the dialog, but failed.
When the option is clicked, there are new dialog is shown, and inside the dialog, there are textviews and button, when I click the button, the textview will be update.
Here is the code related to the button onClick listener:
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (int i = 0; i < 50 ; i ++){
final String currentNum = String.valueOf(i + 1);
Thread t = new Thread() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(currentNum); //it is fine
currentNum.setText(currentNum); //it is the problem, the setText only work when the for loop is finished.
}
});
}
};
t.start();
}
}
});
Please let me know if you need more information. Thanks a lot in advance!
//it is a optionmenu
case R.id.action_refresh:
final TextView currentNum;
final ImageButton start;
String currentNum = Integer.toString(songList.size());
final Dialog lyricsAnalysis = new Dialog(this,R.style.cust_dialog);
lyricsAnalysis.requestWindowFeature(Window.FEATURE_NO_TITLE);
lyricsAnalysis.setContentView(R.layout.analysis);
lyricsAnalysis.setCancelable(true); //back button to cancel
lyricsAnalysis.setCanceledOnTouchOutside(true);
start = (ImageButton) lyricsAnalysis.findViewById(R.id.start);
//first value
currentNum.setText(String.valueOf(currentNum));
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (int i = 0; i < 50 ; i ++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateTextView(lyricsAnalysis,i);
}
}
});
lyricsAnalysis.show();
lyricsAnalysis.getWindow().setLayout(600, 1000);
break;
}
return super.onOptionsItemSelected(item); }
public void updateTextView(Dialog dialog, int i) {
final TextView currentNum = (TextView) dialog.findViewById(R.id.currentNum);
currentNum.setText(Stri`enter code here`ng.valueOf(i));
//return;
}
Try this method. This may helps you. It's work for me.(But I am not use this in dialog)
public void updateTextView(String toThis) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
//return;
}
try like this
int elapsedtime=0;
boolean isTimerRunning=false;
Timer timerr;
inside onCreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//declare your textview here;
timerr=new Timer();
startTimer();
}
/*
* timer for displaying message bubble
*/
protected static void startTimer() {
isTimerRunning = true;
elapsedtime = 0;
// recordingseek.setProgress(0);
timerr.scheduleAtFixedRate(new TimerTask() {
public void run() {
// increase every sec
elapsedtime++;
mmHandler.obtainMessage(1).sendToTarget();
System.out.println("recording time" + elapsedtime);
if(elapsedtime==50)
timerr.cancel();
}
}, 1000, 2000);
};
public static Handler mmHandler = new Handler() {
public void handleMessage(Message msg) {
textview.setText(elapsedtime);
}
};
}
};

How to use Timer inside Handler?

I want to refresh an adapter every 10 seconds. I'm loading my ListView Adapter inside Handler, so what do I do? How do I use Timer? I want that whenever the value changes, the Adapter automatically refreshes ListView.
#SuppressLint("HandlerLeak")
Handler handle = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
CommonObjects.hideProgress();
if (msg.what == 1) {
plotadapter = new PlotsAdapter(Plots.this, arrayplot);
plotslist.setAdapter(plotadapter);
}
if (msg.what == 2) {
Toast.makeText(Plots.this, "Server, Not Available!",
Toast.LENGTH_SHORT).show();
finish();
}
}
};
#Override
protected void onRestart() {
if(CommonObjects.getLogoutreject().equals("1") && CommonObjects.logout){
// CommonObjects.logout=false;
finish();
}
super.onRestart();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.plots);
plotslist = (ListView) findViewById(R.id.plotslist);
back = (ImageView) findViewById(R.id.plotback);
bottomlayout = (RelativeLayout) findViewById(R.id.bottom_layout);
scroll_down = (ImageView) findViewById(R.id.down);
scroll_up = (ImageView) findViewById(R.id.up);
plotadapter = new PlotsAdapter(Plots.this, arrayplot);
plotslist.setAdapter(plotadapter);
final Handler handler = new Handler();
handler.postDelayed( new Runnable() {
#Override
public void run() {
if(condition)
{
// your code to check
plotadapter.notifyDataSetChanged();
}
handler.postDelayed(this, 1000);
}
}, 1000);

Showing duration time using stopwatch in android?

I need to toast the stopwatch's value
ie.,time taken between start and stop
If i click the stop button it should toast that time duration.
How to do this?
Here i have tried some code
chrono_meter.java
public class Chrono_meter extends Activity {
Chronometer chr;
Button btn_stop_travel;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chronometer_layout);
chr = (Chronometer)findViewById(R.id.chronometer1);
chr.start();
btn_stop_travel = (Button)findViewById(R.id.btn_stop_inspection);
btn_stop_travel.setOnClickListener(mStopListener);
}
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
chr.stop();
}
};
}
Try this
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
chr.stop();
Toast.makeText(Chrono_meter.this, chr.getText(), Toast.LENGTH_LONG).show() ;
}
};

Android Automatic Refresh Button

I'm having trouble with something that seems like it should be very simple. I have a frame layout with 2 Buttons (one on top of the other naturally). When I click on the top button, it automatically takes me to a website, and the button beneath it replaces it to be the visible one. I want to set up an automatic refresh so that a few seconds later, the button that was originally on top becomes on top again. Thank you for any help you can give! Here is the Java, and with my attempt at creating an automatic refresh:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
final WebView webview1= (WebView)this.findViewById(R.id.webView1);
final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);
final Handler handler = new Handler();
Refresh = new Runnable() {
public void run() {
bJava1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
webview1.loadUrl("http://www.google.com");
if(sound.isPlaying()){
bJava1.setVisibility(ImageButton.VISIBLE);
bJava2.setVisibility(ImageButton.GONE);
}else {
sound.start();
bJava1.setVisibility(ImageButton.GONE);
bJava2.setVisibility(ImageButton.VISIBLE);
}
}
});
handler.postDelayed(Refresh, 10000);
}
};
handler.post(Refresh);
Problem is, that your Refresh runnable only registers a onClickListener on the first button and calls itself every 10 seconds, you should register the onClickListener only once outside the Runnable and only call the if block within your Refresh.run() Method:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
final WebView webview1= (WebView)this.findViewById(R.id.webView1);
final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);
final Handler handler = new Handler();
Refresh = new Runnable() {
public void run() {
if(sound.isPlaying()){
bJava1.setVisibility(ImageButton.VISIBLE);
bJava2.setVisibility(ImageButton.GONE);
}else {
sound.start();
bJava1.setVisibility(ImageButton.GONE);
bJava2.setVisibility(ImageButton.VISIBLE);
}
}
};
bJava1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
webview1.loadUrl("http://www.google.com");
handler.postDelayed(Refresh, 10000);
}
});

Categories

Resources