import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_apple = (Button) findViewById(R.id.button_apple);
Button btn_cherry = (Button) findViewById(R.id.button_cherry);
Button btn_orange = (Button) findViewById(R.id.button_orange);
Button btn_waterLemon = (Button) findViewById(R.id.button_waterlemon);
btn_apple.setOnClickListener(new View.OnClickListener() {
boolean action = false;
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_apple:
if (!action) {
action = true;
btn_apple.setText("1");
}
else {
int i = Integer.parseInt(btn_apple.getText().toString());
btn_apple.setText(String.valueOf(i + 1));
}
break;
case R.id.button_cherry:
action = false;
if (!action) {
action = true;
btn_cherry.setText("1");
}
else {
int i = Integer.parseInt(btn_cherry.getText().toString());
btn_cherry.setText(String.valueOf(i + 1));
}
break;
}
}
});
}
}
I need to make it so that if a user clicks on button_apple and doesn't click on it for 3 seconds, its text becomes so superfluous. And if anyone knows why my text changes in button_apple, but not in button_cherry. Tell me, please.
You're setting the listener on your apple button. The cherry button doesn't have a listener on it.
Reconsider what you're trying to achieve and simplify it.
If the user clicks the apple button you need to do something.
If the user clicks the cherry button you need to do something (maybe something else).
If the user clicks the watermellon button, ... and so on.
// define your listeners
View.OnClickListener appleListener = new View.OnClickcListener() {
#Override
public void onClick(View v) {
// do whatever you need to do when the apple button is clicked
}
};
// same thing for cherry listener, make a listener to handle the click action
View.OnClickListener cherryListener = ...
// register the listeners
btnApple.setOnClickListener(appleListener);
btnCherry.setOnClickListener(cherryListener);
...
EDIT
To make something happen after a set amount of time you have to consider:
the inputs: which objects / variables influence the action
the output: what's supposed to happpen if all inputs are valid
the duration
With android you could use a handler. See the postDelayed method.
long delayMillis = 3000L; // duration after which to run your task
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after the delay in milliseconds
}
}, delayMillis);
All of this stuff has to be inside the click listener with the logic you need to implement.
"If a user clicks on button apple and doesn't click on it for three seconds"
You could do something like
View.OnClickListener appleListener = new View.OnClickcListener() {
AtomicInteger clicks = new AtomicInteger(0);
#Override
public void onClick(View v) {
int numClicks = clicks.incrementAndGet();
if (numClicks == 1) {
long delayMillis = 3000L; // duration after which to run your task
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (clicks.get() == 1) {
// they only clicked once, do whatever you need to do to make the text superfluous
}
// put the number of clicks back to 0
clicks.set(0);
}
}, delayMillis);
}
} else {
// TODO
// it's been clicked more than once
// show a toast if you need to or do something else
}
}
I didn't test this so you'll probably have to modify it a bit but that's the general idea.
Related
I making an app and I would run in to a problem that I can't fix.
I am using Android Studio and I need to make a Button that I have already made. Do more then it already does. I want it to after display a text also close the app. Do you know how I should program that. It would be very helpful. Thanks in advance.
Here is the Java code:
package test.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonOnClick(View v) {
Button button=(Button) v;
((Button) v).setText("Correct");
}
public void buttonAnClick(View q) {
Button button=(Button) q;
((Button) q).setText("Niet goed");
}
}
So I have already made the button in XML and told it:
android:onClick="buttonOnClick"
Hope you can help me out!
If you're looking to have something happen afterwards what you're looking for is this:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//What you want to happen later
}
}, 1500); //1500 = 1.5 seconds, time in milli before it happens.
Put that in your button's method.
If you want to close your app you should call
finish();
Finish closes out the current activity. If you have other activities on the stack you'd return to them but since you likely only have 1 it should take you out.
Try this.
public void buttonOnClick(View v) {
Button button=(Button) v;
((Button) v).setText("Correct");
new android.os.Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
},1000); // milliseconds: 1 seg.
}
Check out CountDownTimer
CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
If you are trying to get something done with a delay when a button is pressed then do it something like this:
public void buttonOnClick(View v) {
Button button=(Button) v;
button.setText("Correct");
button.postDelayed(new Runnable(){
public void run() {
//runs after 1 second delay on UI thread
}
}, 1000L);
}
If you want to pause then use java.util.concurrent.TimeUnit.
For example:
TimeUnit.SECONDS.sleep(1);
Just put method into your button click function
public void buttonOnClick(View v) {
//call method here
}
If you want to use RxJava
Disposable di = Observable.timer(2, TimeUnit.SECONDS)
.subscribe(timer->{
//next action after 2 seconds
},throwable->{
// throw exception
});
To destroy disposable use, di.dispose();
if Use Runnable and Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//next action after 2 seconds
}
}, 2*1000);
to stop the handler use, handler.removeCallbacksAndMessages(null);
Always in my apps I added buttons in void onCreate, but now I'm trying to do app with more buttons (about 10). I would like to all buttons active on start app.
In my opinion it is too much buttons to add in this onCreate and app will be starting to long.
I tried to put this:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
})
out of onCreate
but AndroidStudio underlines setOnClickListener and view
I don't have ideas, how and where can i add button out of onCreate.
If you don't want to overcrowd your oncreate method, then create a clicklistener outside onCreate anywhere in activity and in onCreate just set it.
onCreate :
edit_a_member = (Button) findViewById(R.id.edit_member);
delete_a_member = (Button) findViewById(R.id.delete_member);
edit_a_member.setOnClickListener(handleClick);
delete_a_member.setOnClickListener(handleClick);
clickListener:
private View.OnClickListener handleClick = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.edit_member:
member_selected = EDIT_MEMBER_SELECTED;
callDialog();
break;
case R.id.delete_member:
callDeleteAlert();
break;
}
}
};
You can simply add a separate method for your buttons in the same class, e.g.:
public void onCreate(...){
//Standard setup of views or whatever you want to do here
this.addButtons();
}
private void addButtons(){
Button b1 = new Button("Hi");
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Button b2 = new Button("Hi to you too");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
}
This is an example. You can do this in soooo many ways. I feel like you should thoroughly learn Java's fundamental Object Oriented programming, because that's really what your question suggests you don't understand. Go follow a youtube tutorial. I always like "The New Boston"'s Java tutorial series on youtube.
PS: You can make code like this beautiful under the 'Words of wisdom': Don't repeat yourself
If you have to do a lot of work in your onCreate but you are worried that the UI will take too long to load you can always post a delayed runnable to a handler so in the onCreate method put :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add your code here
}
},10);
what this will do is your UI will load then the code in your Runnable will be executed 10 milliseconds after your UI loads thus your app will not take too long to load the UI, even though in your case I doubt it would be necessary.
If you are declaring the buttons in xml file :
Add these properties in each button Declaration in your Xml :
android:clickable="true"
android:onClick="onClick"
And now in Activity Class create a method like this :
public void onClick(View v){
switch(v.getId){
case R.id.{buttons_id_in_xml}
(Your Code)
break;
(Like for others)
}
}
If you want to add buttons dynamically :
Create a method to add the button like this:
void addButton(String buttonName, int button id){
Button button = new Button(this);
button.setText("Push Me");
(add it to parent Layout of xml)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(id){
case id1:
(handle )
break;
(like for others)
}
}
});
}
The best way to do this is:
add implements View.OnClickListener to
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// declare variables
private Button mBtn1;
private Button mBtn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// make an instance to the btns
mBtn1 = findViewById(R.id.btn1);
mBtn2 = findViewById(R.id.btn2);
// set onClickListener
mBtn1.setOnClickListener(this); // with "this" you are passing the view
mBtn2.setOnClickListener(this);
}
// implement onClick
#Override
public void onClick(View view) {
// check which btn was clicked by id
switch (view.getId()) {
case R.id.btn1:
btn1Clicked();
break;
case R.id.btn2:
btn2Clicked();
break;
}
}
private void btn1Clicked() {
// your code btn1 clicked
}
private void btn2Clicked() {
// your code btn2 clicked
}
Hope this helped. Cheers!
Im trying to build easy app that the user need to remmber on which buttons the computer pressed ..
Each button that clicked make a sound .. so in order to make each sound ,sound correctly I tried to give a little bit of delay between each click that the computer pressed...
So I make int[] that include the sequence of the way the computer click the buttons ( for 1 click on button1 , for 2 click on button 2 .... )
and now im trying to acually pressed the button haha
so I made this :
private void ClickButtons(int[] sequence) {
Button btcomp1 = (Button) findViewById(R.id.btComp1);
Button btcomp2 = (Button) findViewById(R.id.btComp2);
Button btcomp3 = (Button) findViewById(R.id.btComp3);
Button btcomp4 = (Button) findViewById(R.id.btComp4);
TextView check=(TextView)findViewById(R.id.textViewKq);
Handler handler = new Handler();
for (int i = 0; i < sequence.length; i++)
{
switch (sequence[i]) {
case 1:
btcomp1.performClick();
check.setText("4..");
timedelay();
break;
case 2:
btcomp2.performClick();
check.setText("3..");
timedelay();
break;
case 3:
btcomp3.performClick();
check.setText("2..");
timedelay();
break;
case 4:
btcomp4.performClick();
check.setText("1..");
timedelay();
break;
}
}
}
it didnt work well , I even thought that their is may be problem with the button so I set it for changing some Textview but its seems that its only enter to one case ... and the only delay was when I mover from the FirstActivity to the GameActivity
I really have nothing in the FirstActivity just field for UserName ...
Well I hopes you helped me guys and soory for grammer ><
This is the delay .. I tried to do something but all that do is to delay me a few seconds after I gor from actity 1 to another but do nothing in the main ..
private void timedelay() {
try{
Thread.sleep(2500);
}
catch (Exception e){
}
}
For this kind of thing you should definitely use the android.os.Handler to make a timer. And in the callback method, click the buttons. The method you are using now will probably block the main thread. This is really bad.
Luckily, I have already written a Timer class does this! Here is the whole class:
import android.os.Handler;
public class Timer {
private Handler handler;
private boolean paused;
private int interval;
private Runnable task = new Runnable () {
#Override
public void run() {
if (!paused) {
runnable.run ();
Timer.this.handler.postDelayed (this, interval);
}
}
};
private Runnable runnable;
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
public void startTimer () {
paused = false;
handler.postDelayed (task, interval);
}
public void stopTimer () {
paused = true;
}
public Timer (Runnable runnable, int interval, boolean started) {
handler = new Handler ();
this.runnable = runnable;
this.interval = interval;
if (started)
startTimer ();
}
}
Try to understand it. After you do that, you can use this timer like this:
final Timer t = new Timer(new Runnable() {
private int i = 1;
Button btcomp1 = (Button) MainActivity.this.findViewById(R.id.btComp1);
Button btcomp2 = (Button) MainActivity.this.findViewById(R.id.btComp2);
Button btcomp3 = (Button) MainActivity.this.findViewById(R.id.btComp3);
Button btcomp4 = (Button) MainActivity.this.findViewById(R.id.btComp4);
TextView check=(TextView)MainActivity.this.findViewById(R.id.textViewKq);
public void run() {
switch (i) {
case 1:
btcomp1.performClick();
check.setText("4..");
break;
case 2:
btcomp2.performClick();
check.setText("3..");
break;
case 3:
btcomp3.performClick();
check.setText("2..");
break;
case 4:
btcomp4.performClick();
check.setText("1..");
t.stopTimer();
break;
}
}
}, 1000, true);
Once this timer is created, it basically presses a button once a second. And set the text of check. When it presses it the fourth time, the timer stops.
Easy! (if you have the timer class)
I have 2 buttons as well as a progressbar.
Now I want to count from 0 - 100 and update the progressbar with the counter. Unfortunately this count is so fast, that I will not see a progress (I can only see the progressbar jump from 0 to 100 within a 'millisecond'.).
This is what I tried so far, however the same thing happens as before.
I click the button to start the counter, the app becomes unresponsive (I calculate within the mainThread, so that is OKAY (and exactly what I want to accomplish here(!))) and shortly after sets the progressbar to 100. Nothing in between.
Now if you could head me into the correct direction, that'd be highly appreciated! :)
public class MainActivity extends ActionBarActivity {
public String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonListeners();
}
public void addButtonListeners() {
Button firstButton, secondButton;
final ProgressBar pgBar;
firstButton = (Button) findViewById(R.id.first_button);
secondButton = (Button) findViewById(R.id.second_button);
pgBar = (ProgressBar) findViewById(R.id.progressBar);
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "First Button was pressed.");
for (int i = 0; i <= 100; i++) {
countProgBarUp(pgBar, i);
}
}
});
secondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Second Button was pressed.");
}
});
}
public void countProgBarUp(ProgressBar pgBar, int counter) {
try {
Thread.sleep(100);
pgBar.setProgress(counter);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
...
}
The UI becomes unresponsive because you are doing this on the main thread and sleeping. If you do not return from the onClick() then the main thread cannot handle UI changes, such as displaying your new progress. Only the main thread can update UI components and draw them. It is often called the UI thread because of this.
You could try creating a Runnable which updates the progress bar to the new count. Create a Handler which is bound to the UI thread and call postDelayed() for the Handler and give it an instance of the Runnable. Then in the Runnable just post it again as long as you haven't reached you max progress.
This has been asked in previous posts, but I've tried many and none of them seem to work. I have a boolean flag that should in theory prevent my dialog from being closed. Here is what I have so far:
boolean start_match = false;
public void WarmupDialog()
{
if(use_warmup == true)
{
final ProgressDialog spinner = new ProgressDialog(this);
spinner.setTitle("Warmup");
spinner.setCancelable(false);
spinner.setCanceledOnTouchOutside(false);
timer = new CountDownTimer(300000, 1000)//5 minutes
{
#Override
public void onFinish()
{
spinner.cancel();
}
#Override
public void onTick(long l)
{
spinner.setMessage(((int)Math.round(l/1000.0)-1)+"secs remaining of warmup");
}
};
spinner.setButton(DialogInterface.BUTTON_POSITIVE, "Start Warmup", (DialogInterface.OnClickListener)null) ;
spinner.show();
Button button = spinner.getButton(DialogInterface.BUTTON_NEUTRAL);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(start_match)
{
spinner.dismiss();
}
else
{
start_match = true;
Button button = spinner.getButton(DialogInterface.BUTTON_POSITIVE);
button.setText("Start Match");
timer.start();
}
}
});
spinner.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
timer.cancel();
ChooseServer();
}
});
}
else
{
ChooseServer();
}
}
This open a warm up timer for a tennis match. When the button is pressed for the first time the timer should start. When the button is pressed again the dailog should then cancel. At the moment the dialog is dismissed immediately. Any ideas?
Thanks in advance for any help!
As far as my knowledge about Progress dialogs goes, it should be the normal behavior.
As ProgressDialog inherits from AlertDialog -> Dialog and by default they get dismissed when user press a button on the dialog, regardless of which button it is (positive/negative etc).
Suggestion: intercepting the touch event & detecting the source position of the touch event (i.e on which button user has touched), we should be able to override this behavior.