I'm trying to implement a traffic signal which will change color form red to green then to yellow. For this I used a button and I'm changing the background of the button to respected color. I'm using CountDownTimer for this purpose. Here is my code:
public class MainActivity extends Activity {
Button button1 = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setBackgroundColor(Color.RED);
while(true){
change(Color.GREEN);
change(Color.BLUE);
change(Color.RED);
}
}
void change(final int color)
{
CountDownTimer ctd = new CountDownTimer(3000, 3000)
{
#Override
public void onTick(long arg0) {}
#Override
public void onFinish() {
button1.setBackgroundColor(color);
}
};
ctd.start();
}
}
But the above code doesn't seems to work, the color of the button is not at all changing. What's the problem in this code?
This worked for me:
public class TestActivity extends Activity {
Button button1 = null;
long timeout = Long.MAX_VALUE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setBackgroundColor(Color.RED);
change();
}
void change() {
final int[] colors = {Color.GREEN, Color.BLUE, Color.RED};
CountDownTimer ctd = new CountDownTimer(timeout, 3000) {
int current = 0;
#Override
public void onTick(long arg0) {
Log.d("TEST", "Current color index: " + current);
button1.setBackgroundColor(colors[current++]);
if (current == 3)
current = 0;
}
#Override
public void onFinish() {
}
};
ctd.start();
}
}
Related
I've a horizontal progressbar which shows progress from 0 to 100. What I want is to restart the progressbar again from 0 when it reaches 100. I'll be showing this in a Fragmentdialog. For now I'm checking it in activity itself. How to do that?
public class MainActivity extends AppCompatActivity{
private Button startbtn, stopbtn;
ProgressBar pb;
private TextView progressTxt;
int progressBarValue = 0;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = new CShowProgress(MainActivity.this);
btn = (Button)findViewById(R.id.btn);
startbtn = (Button)findViewById(R.id.startbtn);
stopbtn = (Button)findViewById(R.id.stopbtn);
pb = (ProgressBar)findViewById(R.id.progressBar);
progressTxt = (TextView)findViewById(R.id.progressTxt);
startbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myprogress(true);
}
});
stopbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myprogress(false);
}
});
}
private void myprogress(final Boolean isStart){
handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
if(isStart && progressBarValue<100)
{
progressBarValue+=1;
pb.setProgress(progressBarValue);
progressTxt.setText(String.valueOf(progressBarValue));
handler.sendEmptyMessageDelayed(0, 100);
}
}
};
handler.sendEmptyMessage(0);
}
}
I assume this chunk of code will work for your use case:
private void myprogress(final Boolean isStart) {
handler = new Handler() {
public void handleMessage(Message msg) {
if (isStart && progressBarValue < 100) {
...
} else if (isStart) {
// progressBarValue is equal to 100
// make it zero and do the same thing again
progressBarValue = 0;
progressBarValue += 1;
pb.setProgress(progressBarValue);
progressTxt.setText(String.valueOf(progressBarValue));
handler.sendEmptyMessageDelayed(0, 100);
}
}
};
handler.sendEmptyMessage(0);
}
First, you should use a counter mechanizm (run a delayed handler, timer, thread or something else)to check current progress periodically.
(progressTxt.getProgress == 100)
will do the trick.
Another way is using this answer.
in this app the Content of the TextView should change/update every second with a sleep thread.
The whole process starts when the button is clicked.
Firstable here is the normal code without the threads:
public class MainActivity extends ActionBarActivity {
Button btn;
TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
tw = (TextView) findViewById(R.id.tw);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText("1"); //This is the TextView Content, it should update every second with a sleep thread
tw.setText("2");
tw.setText("3");
}
});
}
}
This is the code added ( not working ) sleep threads:
public class MainActivity extends ActionBarActivity {
Button btn;
TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
tw = (TextView) findViewById(R.id.tw);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText("1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tw.setText("2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tw.setText("3");
}
});
}
}
ThankĀ“s
The problem is that you are block the main UI(sleep) thread thus giving you unexpected result.
You need to use handler for this if you want to update this each second
sample:
public class MainActivity extends ActionBarActivity {
Button btn;
TextView tw;
int incre = 1;
Handler handler;
Runnable run;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
tw = (TextView) findViewById(R.id.tw);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText(incre++ + "");
handler = new Handler();
run = new Runnable() {
#Override
public void run() {
tw.setText(incre++ + ""); //set the textview text HERE every 1 second
if(incre != 3) //checks if it is not already 3 second
handler.postDelayed(run, 1000); //run the method again
else
incre -= 2;
}
};
handler.postDelayed(run, 1000); //will call the runnable every 1 second
}
});
}
}
I got some problems with some of my code. Basiclly, it's a countdown program, but whenever a button is clicked, it should refresh the countdown.
I hope that makes sense. Below is some of my code, however, it's not working as intended.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView mTextField = (TextView) findViewById(R.id.mTextField);
final Button refresh = (Button) findViewById(R.id.button1);
final int j = 30000;
int i = j;
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
j = 30000;
}
});
new CountDownTimer(i, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
My problem is, that I need to make a variable final to access it in a onClickListener, but I can't change it once it's final.
Thanks in advance
Make j a field variable and don't make it final, that will solve your problem
public class MainActivity extends Activity {
int j;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView mTextField = (TextView) findViewById(R.id.mTextField);
final Button refresh = (Button) findViewById(R.id.button1);
j = 3000;
int i = j;
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
j = 30000;
}
});
new CountDownTimer(i, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
What you can do is just make j a member variable then you can use it anywhere in the class
public class MainActivity extends Activity {
int j = 3000; // declare it here as a member variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView mTextField = (TextView) findViewById(R.id.mTextField);
final Button refresh = (Button) findViewById(R.id.button1);
This way you don't have to make it final.
i'm working on an Android application in which i want the background image to be changed after every 5 seconds. i have all the images in my drawable folder.
i am giving the code which i am using but i am not getting the output. As an output i am getting a still image which is not changing.
Please help
Thanks
[CODE]
public class Home extends Activity {
public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
new Handler().postDelayed(new Runnable() {
public void run() {
if(count<drawablearray.length){
Home.this.getWindow().
setBackgroundDrawableResource(drawablearray[count]);
count++; //<<< increment counter here
}
else{
// reset counter here
count=0;
}
}
}, 5000);
}
}
You can achieve this using Timer
public class Home extends Activity {
public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};
Timer _t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
lnMain = (LinearLayout) findViewById(R.id.lnMain);
_t = new Timer();
_t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() // run on ui thread
{
public void run() {
if (count < drawablearray.length) {
lnMain.setBackgroundDrawable(drawablearray[count]);
count = (count + 1) % drawablearray.length;
}
}
});
}
}, 5000, 5000);
}
}
Why don't you have a look at the ViewFlipper class
http://developer.android.com/reference/android/widget/ViewFlipper.html
final Handler h = new Handler();
Runnable r = new Runnable() {
public void run() {
Home.this.getWindow().setBackgroundDrawableResource(drawablearray[count]);
count += (count+1)%drawablearray.length; //<<< increment counter here
h.postDelayed(this, 5000);
}
};
now call like
h.postDelayed(r, 5000);
I think that would be easier to work with the xmls. You can change the background of the main layout of the activity.
Try something like this:
public class Home extends Activity {
public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ll = (LinearLayout) findViewByID(R.id.mainlayout) //It depends of the name that you gave to it
new Handler().postDelayed(new Runnable() {
public void run() {
ll.setBackgroundDrawable(drawablearray[count]);
// or ll.setBackgroundResource(resid) if you want.
count += (count+1)%drawablearray.length;
}
}, 5000);
}
}
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() ;
}
};