How to automatically Click a Button in Android after a 5 second delay
I tried with the codes that are entered in the link but my application has crashed
My codes;
public class MainActivity extends AppCompatActivity {
Button button;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
Thread timer = new Thread() {
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
button.performClick();
}
}
};
timer.start();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button.performClick();
}
}, 1000);
}
public void buttonClick() {
button=(Button) findViewById(R.id.button);
text=(TextView) findViewById(R.id.text);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random s=new Random();
int number=s.nextInt(3);
switch (number)
{case 1:text.setText("1");
break;
case 2: text.setText("2");
break;
}
}
});
}
}
Logcat Error
You need to use
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
to avoid this error.
Please check Android "Only the original thread that created a view hierarchy can touch its views."
Also you can just use the Handler to perform the button click after a specified amount of time, no need to use the timer.
This is more simpler method to run every second. you dont need to trigger the button. just call the method you want to execute
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Handler handler = new Handler();
Runnable runnable = new Runnable(){
#Override
public void run() {
buttonClick();
if(handler!=null)
handler.postDelayed(runnable, 1000);
}
}
handler.postDelayed(runnable, 1000);
}
Related
public class MainActivity extends AppCompatActivity {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textview);
Thread t=new Thread(new Runnable() {
#Override
public void run()
{
textView.setText("Hello");
}
});
t.start();
}
}
I was expecting wrong thread exception. And what is meaning of "scheduleTraversals in mThread=[main,5,main] currentThread=[thread-2,5,main]", how it is different from "CalledFromWrongThreadException".
You can't. Sub thread cannot modify the ui. There is a way to do this:
Thread t=new Thread(new Runnable() {
#Override
public void run()
{
runOnUiThread(() -> {
textView.setText("Hello");
});
}
});
t.start();
In android,
for example
i want to perform a text.settext("something dynamic") until the buTTon is pressed. how to do it?
Try this:
android.os.Handler mHandler = new Handler();
Runnable rUpdateTextView = new Runnable() {
#Override
public void run () {
yourTextView.setText(returndate());
// Update your TextView every 200ms
mHandler.postDelayed(this, 200);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
[...]
mHandler.post(rUpdateTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mHandler.removeCallbacks(rUpdateTextView);
}
});
}
//click event for your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time = returndate();
//set your text to textView
test.setText(time);
}
});
}
I want write value from SeekBar to log in new Thread. I want press button and start write logs. and change seekBar value and write it in log.
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, View.OnTouchListener {
private SeekBar seekBar;
private Button button;
private TextView textView;
private EditText editText;
private Thread thread;
private int value = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
seekBar.setOnSeekBarChangeListener(this);
button.setOnTouchListener(this);
print();
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
print();
Toast.makeText(MainActivity.this, "ACTION_DOWN", Toast.LENGTH_SHORT).show();
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
thread.stop();
Toast.makeText(MainActivity.this, "ACTION_UP", Toast.LENGTH_SHORT).show();
return true; // if you want to handle the touch event
}
return false;
}
public void print() {
Runnable runnable = new Runnable() {
#Override
public void run() {
Log.d("MyLog", String.valueOf(value));
}
};
thread = new Thread(runnable);
thread.start();
}
}
but my log write once. How can I write log every second?
You could do something like this:
Handler handler = new Handler();
boolean stop = false;
Runnable runnable = new Runnable() {
#Override
public void run() {
Log.d("MyLog", String.valueOf(value));
if (!stop) {
handler.postDelayed(this, 1000);
}
}
};
// start logging by calling this method
public void print() {
handler.post(runnable);
}
// stop logging by calling this method
public void printStop() {
stop = true;
}
It works for me:
new Thread(new Runnable() {
#Override
public void run() {
while(true) {
//do whatever you want
try {
Thread.sleep(1000); //sleep time in milliseconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
good afternoon I have two classes, one that implement one progressBar this class I intend to create a public method that will run startActivity(). the main class would have a button that would call the method. However this giving error null reference.
ProgressBarUtil:
public class ProgressBarUtil extends BaseActionBarActivity {
private ProgressBar progressBar;
private Handler handler;
private int progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_bar_util);
inicializaComponentes();
}
public void inicializaComponentes() {
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setIndeterminate(true);
handler = new Handler();
executaProgressbar();
}
public void startProgressBar(){
startActivity(new Intent(getBaseContext(),ProgressBarUtil.class));
}
private void executaProgressbar() {
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
class that will call the method starProgressBar
The other class
#Override
public void onClick(View view) {
int id = view.getId();
if (btnDownload.getId() == id){
new ProgressBarUtil().startProgressBar();
}
}
I am trying to get the splash layout to appear for 5 seconds and then to switch over to the mainmenu layout. The screen is blank for about 5 seconds, and then the mainmenu layout pops up. If I just run the splash layout without sleeping, it runs just fine, so I do not think that is the problem. Any ideas?
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mainMenu();
}
private void mainMenu()
{
setContentView(R.layout.mainmenu);
}
This is beacause u are doing Thread.sleep on Main UI thread.. Thats not recommended..
Use Timer instead, Use below code..
private Timer timer;
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
handler = new Handler();
timer = new Timer();
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
YourActivity.this.setContentView(R.layout.mainmenu);
}
});
}
};
timer.schedule(timerTask, 5000);
}